Fix kubernetes providers shutdown and clean safe.Pool
This commit is contained in:
parent
c80d53e7e5
commit
1b63c95c4e
11 changed files with 73 additions and 190 deletions
|
@ -10,88 +10,37 @@ import (
|
|||
"github.com/containous/traefik/v2/pkg/log"
|
||||
)
|
||||
|
||||
type routine struct {
|
||||
goroutine func(chan bool)
|
||||
stop chan bool
|
||||
}
|
||||
|
||||
type routineCtx func(ctx context.Context)
|
||||
|
||||
// Pool is a pool of go routines
|
||||
type Pool struct {
|
||||
routines []routine
|
||||
waitGroup sync.WaitGroup
|
||||
lock sync.Mutex
|
||||
baseCtx context.Context
|
||||
baseCancel context.CancelFunc
|
||||
ctx context.Context
|
||||
cancel context.CancelFunc
|
||||
waitGroup sync.WaitGroup
|
||||
ctx context.Context
|
||||
cancel context.CancelFunc
|
||||
}
|
||||
|
||||
// NewPool creates a Pool
|
||||
func NewPool(parentCtx context.Context) *Pool {
|
||||
baseCtx, baseCancel := context.WithCancel(parentCtx)
|
||||
ctx, cancel := context.WithCancel(baseCtx)
|
||||
ctx, cancel := context.WithCancel(parentCtx)
|
||||
return &Pool{
|
||||
baseCtx: baseCtx,
|
||||
baseCancel: baseCancel,
|
||||
ctx: ctx,
|
||||
cancel: cancel,
|
||||
ctx: ctx,
|
||||
cancel: cancel,
|
||||
}
|
||||
}
|
||||
|
||||
// Ctx returns main context
|
||||
func (p *Pool) Ctx() context.Context {
|
||||
return p.baseCtx
|
||||
}
|
||||
|
||||
// GoCtx starts a recoverable goroutine with a context
|
||||
func (p *Pool) GoCtx(goroutine routineCtx) {
|
||||
p.lock.Lock()
|
||||
p.waitGroup.Add(1)
|
||||
Go(func() {
|
||||
defer p.waitGroup.Done()
|
||||
goroutine(p.ctx)
|
||||
})
|
||||
p.lock.Unlock()
|
||||
}
|
||||
|
||||
// Go starts a recoverable goroutine, and can be stopped with stop chan
|
||||
func (p *Pool) Go(goroutine func(stop chan bool)) {
|
||||
p.lock.Lock()
|
||||
newRoutine := routine{
|
||||
goroutine: goroutine,
|
||||
stop: make(chan bool, 1),
|
||||
}
|
||||
p.routines = append(p.routines, newRoutine)
|
||||
p.waitGroup.Add(1)
|
||||
Go(func() {
|
||||
defer p.waitGroup.Done()
|
||||
goroutine(newRoutine.stop)
|
||||
})
|
||||
p.lock.Unlock()
|
||||
}
|
||||
|
||||
// Stop stops all started routines, waiting for their termination
|
||||
func (p *Pool) Stop() {
|
||||
p.lock.Lock()
|
||||
defer p.lock.Unlock()
|
||||
p.cancel()
|
||||
for _, routine := range p.routines {
|
||||
routine.stop <- true
|
||||
}
|
||||
p.waitGroup.Wait()
|
||||
for _, routine := range p.routines {
|
||||
close(routine.stop)
|
||||
}
|
||||
}
|
||||
|
||||
// Cleanup releases resources used by the pool, and should be called when the pool will no longer be used
|
||||
func (p *Pool) Cleanup() {
|
||||
p.Stop()
|
||||
p.lock.Lock()
|
||||
defer p.lock.Unlock()
|
||||
p.baseCancel()
|
||||
}
|
||||
|
||||
// Go starts a recoverable goroutine
|
||||
|
|
|
@ -18,12 +18,13 @@ func TestNewPoolContext(t *testing.T) {
|
|||
ctx := context.WithValue(context.Background(), testKey, "test")
|
||||
p := NewPool(ctx)
|
||||
|
||||
retCtx := p.Ctx()
|
||||
|
||||
retCtxVal, ok := retCtx.Value(testKey).(string)
|
||||
if !ok || retCtxVal != "test" {
|
||||
t.Errorf("Pool.Ctx() did not return a derived context, got %#v, expected context with test value", retCtx)
|
||||
}
|
||||
p.GoCtx(func(ctx context.Context) {
|
||||
retCtxVal, ok := ctx.Value(testKey).(string)
|
||||
if !ok || retCtxVal != "test" {
|
||||
t.Errorf("Pool.Ctx() did not return a derived context, got %#v, expected context with test value", ctx)
|
||||
}
|
||||
})
|
||||
p.Stop()
|
||||
}
|
||||
|
||||
type fakeRoutine struct {
|
||||
|
@ -46,14 +47,6 @@ func (tr *fakeRoutine) routineCtx(ctx context.Context) {
|
|||
<-ctx.Done()
|
||||
}
|
||||
|
||||
func (tr *fakeRoutine) routine(stop chan bool) {
|
||||
tr.Lock()
|
||||
tr.started = true
|
||||
tr.Unlock()
|
||||
tr.startSig <- true
|
||||
<-stop
|
||||
}
|
||||
|
||||
func TestPoolWithCtx(t *testing.T) {
|
||||
testRoutine := newFakeRoutine()
|
||||
|
||||
|
@ -79,12 +72,12 @@ func TestPoolWithCtx(t *testing.T) {
|
|||
defer timer.Stop()
|
||||
|
||||
test.fn(p)
|
||||
defer p.Cleanup()
|
||||
defer p.Stop()
|
||||
|
||||
testDone := make(chan bool, 1)
|
||||
go func() {
|
||||
<-testRoutine.startSig
|
||||
p.Cleanup()
|
||||
p.Stop()
|
||||
testDone <- true
|
||||
}()
|
||||
|
||||
|
@ -100,89 +93,30 @@ func TestPoolWithCtx(t *testing.T) {
|
|||
}
|
||||
}
|
||||
|
||||
func TestPoolWithStopChan(t *testing.T) {
|
||||
testRoutine := newFakeRoutine()
|
||||
|
||||
func TestPoolCleanupWithGoPanicking(t *testing.T) {
|
||||
p := NewPool(context.Background())
|
||||
|
||||
timer := time.NewTimer(500 * time.Millisecond)
|
||||
defer timer.Stop()
|
||||
|
||||
p.Go(testRoutine.routine)
|
||||
if len(p.routines) != 1 {
|
||||
t.Fatalf("After Pool.Go(func), Pool did have %d goroutines, expected 1", len(p.routines))
|
||||
}
|
||||
p.GoCtx(func(ctx context.Context) {
|
||||
panic("BOOM")
|
||||
})
|
||||
|
||||
testDone := make(chan bool, 1)
|
||||
go func() {
|
||||
<-testRoutine.startSig
|
||||
p.Cleanup()
|
||||
p.Stop()
|
||||
testDone <- true
|
||||
}()
|
||||
|
||||
select {
|
||||
case <-timer.C:
|
||||
testRoutine.Lock()
|
||||
defer testRoutine.Unlock()
|
||||
t.Fatalf("Pool test did not complete in time, goroutine started equals '%t'", testRoutine.started)
|
||||
t.Fatalf("Pool.Cleanup() did not complete in time with a panicking goroutine")
|
||||
case <-testDone:
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
func TestPoolCleanupWithGoPanicking(t *testing.T) {
|
||||
testRoutine := func(stop chan bool) {
|
||||
panic("BOOM")
|
||||
}
|
||||
|
||||
testCtxRoutine := func(ctx context.Context) {
|
||||
panic("BOOM")
|
||||
}
|
||||
|
||||
testCases := []struct {
|
||||
desc string
|
||||
fn func(*Pool)
|
||||
}{
|
||||
{
|
||||
desc: "Go()",
|
||||
fn: func(p *Pool) {
|
||||
p.Go(testRoutine)
|
||||
},
|
||||
},
|
||||
{
|
||||
desc: "GoCtx()",
|
||||
fn: func(p *Pool) {
|
||||
p.GoCtx(testCtxRoutine)
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
for _, test := range testCases {
|
||||
test := test
|
||||
t.Run(test.desc, func(t *testing.T) {
|
||||
p := NewPool(context.Background())
|
||||
|
||||
timer := time.NewTimer(500 * time.Millisecond)
|
||||
defer timer.Stop()
|
||||
|
||||
test.fn(p)
|
||||
|
||||
testDone := make(chan bool, 1)
|
||||
go func() {
|
||||
p.Cleanup()
|
||||
testDone <- true
|
||||
}()
|
||||
|
||||
select {
|
||||
case <-timer.C:
|
||||
t.Fatalf("Pool.Cleanup() did not complete in time with a panicking goroutine")
|
||||
case <-testDone:
|
||||
return
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestGoroutineRecover(t *testing.T) {
|
||||
// if recover fails the test will panic
|
||||
Go(func() {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue