Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
K
k3s
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Registry
Registry
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Jacklull
k3s
Commits
6464774a
Unverified
Commit
6464774a
authored
Jun 07, 2017
by
Mikhail Mazurskiy
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Refactor Start functions into an object
parent
d11a9973
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
28 additions
and
24 deletions
+28
-24
wait.go
staging/src/k8s.io/apimachinery/pkg/util/wait/wait.go
+18
-14
controller.go
staging/src/k8s.io/client-go/tools/cache/controller.go
+2
-2
shared_informer.go
staging/src/k8s.io/client-go/tools/cache/shared_informer.go
+8
-8
No files found.
staging/src/k8s.io/apimachinery/pkg/util/wait/wait.go
View file @
6464774a
...
...
@@ -20,6 +20,7 @@ import (
"context"
"errors"
"math/rand"
"sync"
"time"
"k8s.io/apimachinery/pkg/util/runtime"
...
...
@@ -37,33 +38,36 @@ var ForeverTestTimeout = time.Second * 30
// NeverStop may be passed to Until to make it never stop.
var
NeverStop
<-
chan
struct
{}
=
make
(
chan
struct
{})
// Group is an interface to decouple code from sync.WaitGroup.
type
Group
interface
{
Add
(
delta
int
)
Done
()
// Group allows to start a group of goroutines and wait for their completion.
type
Group
struct
{
wg
sync
.
WaitGroup
}
// StartWithChannelWithinGroup adds 1 to the group, starts f in a new goroutine and calls g.Done once f has finished.
func
(
g
*
Group
)
Wait
()
{
g
.
wg
.
Wait
()
}
// StartWithChannel starts f in a new goroutine in the group.
// stopCh is passed to f as an argument. f should stop when stopCh is available.
func
StartWithChannelWithinGroup
(
stopCh
<-
chan
struct
{},
g
Group
,
f
func
(
stopCh
<-
chan
struct
{}))
{
StartWithinGroup
(
g
,
func
()
{
func
(
g
*
Group
)
StartWithChannel
(
stopCh
<-
chan
struct
{}
,
f
func
(
stopCh
<-
chan
struct
{}))
{
g
.
Start
(
func
()
{
f
(
stopCh
)
})
}
// StartWithContext
WithinGroup adds 1 to the group, starts f in a new goroutine and calls g.Done once f has finished
.
// StartWithContext
starts f in a new goroutine in the group
.
// ctx is passed to f as an argument. f should stop when ctx.Done() is available.
func
StartWithContextWithinGroup
(
ctx
context
.
Context
,
g
Group
,
f
func
(
context
.
Context
))
{
StartWithinGroup
(
g
,
func
()
{
func
(
g
*
Group
)
StartWithContext
(
ctx
context
.
Context
,
f
func
(
context
.
Context
))
{
g
.
Start
(
func
()
{
f
(
ctx
)
})
}
// Start
WithinGroup adds 1 to the group, starts f in a new goroutine and calls g.Done once f has finished
.
func
StartWithinGroup
(
g
Group
,
f
func
())
{
g
.
Add
(
1
)
// Start
starts f in a new goroutine in the group
.
func
(
g
*
Group
)
Start
(
f
func
())
{
g
.
wg
.
Add
(
1
)
go
func
()
{
defer
g
.
Done
()
defer
g
.
wg
.
Done
()
f
()
}()
}
...
...
staging/src/k8s.io/client-go/tools/cache/controller.go
View file @
6464774a
...
...
@@ -116,10 +116,10 @@ func (c *controller) Run(stopCh <-chan struct{}) {
c
.
reflector
=
r
c
.
reflectorMutex
.
Unlock
()
var
wg
sync
.
Wait
Group
var
wg
wait
.
Group
defer
wg
.
Wait
()
w
ait
.
StartWithChannelWithinGroup
(
stopCh
,
&
wg
,
r
.
Run
)
w
g
.
StartWithChannel
(
stopCh
,
r
.
Run
)
wait
.
Until
(
c
.
processLoop
,
time
.
Second
,
stopCh
)
}
...
...
staging/src/k8s.io/client-go/tools/cache/shared_informer.go
View file @
6464774a
...
...
@@ -147,7 +147,7 @@ type sharedIndexInformer struct {
// stopCh is the channel used to stop the main Run process. We have to track it so that
// late joiners can have a proper stop
stopCh
<-
chan
struct
{}
wg
sync
.
Wait
Group
wg
wait
.
Group
}
// dummyController hides the fact that a SharedInformer is different from a dedicated one
...
...
@@ -211,8 +211,8 @@ func (s *sharedIndexInformer) Run(stopCh <-chan struct{}) {
defer
s
.
wg
.
Wait
()
wait
.
StartWithChannelWithinGroup
(
stopCh
,
&
s
.
wg
,
s
.
cacheMutationDetector
.
Run
)
wait
.
StartWithChannelWithinGroup
(
stopCh
,
&
s
.
wg
,
s
.
processor
.
run
)
s
.
wg
.
StartWithChannel
(
stopCh
,
s
.
cacheMutationDetector
.
Run
)
s
.
wg
.
StartWithChannel
(
stopCh
,
s
.
processor
.
run
)
s
.
controller
.
Run
(
stopCh
)
}
...
...
@@ -327,8 +327,8 @@ func (s *sharedIndexInformer) AddEventHandlerWithResyncPeriod(handler ResourceEv
s
.
processor
.
addListener
(
listener
)
wait
.
StartWithChannelWithinGroup
(
s
.
stopCh
,
&
s
.
wg
,
listener
.
run
)
wait
.
StartWithChannelWithinGroup
(
s
.
stopCh
,
&
s
.
wg
,
listener
.
pop
)
s
.
wg
.
StartWithChannel
(
s
.
stopCh
,
listener
.
run
)
s
.
wg
.
StartWithChannel
(
s
.
stopCh
,
listener
.
pop
)
items
:=
s
.
indexer
.
List
()
for
i
:=
range
items
{
...
...
@@ -398,13 +398,13 @@ func (p *sharedProcessor) distribute(obj interface{}, sync bool) {
}
func
(
p
*
sharedProcessor
)
run
(
stopCh
<-
chan
struct
{})
{
var
wg
sync
.
Wait
Group
var
wg
wait
.
Group
func
()
{
p
.
listenersLock
.
RLock
()
defer
p
.
listenersLock
.
RUnlock
()
for
_
,
listener
:=
range
p
.
listeners
{
w
ait
.
StartWithChannelWithinGroup
(
stopCh
,
&
wg
,
listener
.
run
)
w
ait
.
StartWithChannelWithinGroup
(
stopCh
,
&
wg
,
listener
.
pop
)
w
g
.
StartWithChannel
(
stopCh
,
listener
.
run
)
w
g
.
StartWithChannel
(
stopCh
,
listener
.
pop
)
}
}()
wg
.
Wait
()
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment