Adds Cleanup method to safe.Pool
This commit is contained in:
parent
8ae9607d9b
commit
66e914a8ab
2 changed files with 15 additions and 5 deletions
|
@ -21,16 +21,18 @@ type Pool struct {
|
||||||
waitGroup sync.WaitGroup
|
waitGroup sync.WaitGroup
|
||||||
lock sync.Mutex
|
lock sync.Mutex
|
||||||
baseCtx context.Context
|
baseCtx context.Context
|
||||||
|
baseCancel context.CancelFunc
|
||||||
ctx context.Context
|
ctx context.Context
|
||||||
cancel context.CancelFunc
|
cancel context.CancelFunc
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewPool creates a Pool
|
// NewPool creates a Pool
|
||||||
func NewPool(parentCtx context.Context) *Pool {
|
func NewPool(parentCtx context.Context) *Pool {
|
||||||
baseCtx, _ := context.WithCancel(parentCtx)
|
baseCtx, baseCancel := context.WithCancel(parentCtx)
|
||||||
ctx, cancel := context.WithCancel(baseCtx)
|
ctx, cancel := context.WithCancel(baseCtx)
|
||||||
return &Pool{
|
return &Pool{
|
||||||
baseCtx: baseCtx,
|
baseCtx: baseCtx,
|
||||||
|
baseCancel: baseCancel,
|
||||||
ctx: ctx,
|
ctx: ctx,
|
||||||
cancel: cancel,
|
cancel: cancel,
|
||||||
}
|
}
|
||||||
|
@ -90,6 +92,14 @@ func (p *Pool) 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()
|
||||||
|
}
|
||||||
|
|
||||||
// Start starts all stopped routines
|
// Start starts all stopped routines
|
||||||
func (p *Pool) Start() {
|
func (p *Pool) Start() {
|
||||||
p.lock.Lock()
|
p.lock.Lock()
|
||||||
|
|
|
@ -133,7 +133,7 @@ func (server *Server) Close() {
|
||||||
}
|
}
|
||||||
}(ctx)
|
}(ctx)
|
||||||
server.stopLeadership()
|
server.stopLeadership()
|
||||||
server.routinesPool.Stop()
|
server.routinesPool.Cleanup()
|
||||||
close(server.configurationChan)
|
close(server.configurationChan)
|
||||||
close(server.configurationValidatedChan)
|
close(server.configurationValidatedChan)
|
||||||
signal.Stop(server.signals)
|
signal.Stop(server.signals)
|
||||||
|
|
Loading…
Reference in a new issue