diff options
author | Wolfy-J <[email protected]> | 2018-03-31 18:05:58 +0300 |
---|---|---|
committer | Wolfy-J <[email protected]> | 2018-03-31 18:05:58 +0300 |
commit | 93680a356e2fd30f2502d150b4ef0ad61fcc8a74 (patch) | |
tree | b032f7f9b17771b4e15a4fe6435f71d9b2540671 /static_pool.go | |
parent | e0c6b54b5b7dc3b9e7fc417f87f176fdbdb723f9 (diff) |
worker destruction made public
Diffstat (limited to 'static_pool.go')
-rw-r--r-- | static_pool.go | 58 |
1 files changed, 29 insertions, 29 deletions
diff --git a/static_pool.go b/static_pool.go index b0f50c6f..3d036946 100644 --- a/static_pool.go +++ b/static_pool.go @@ -140,13 +140,40 @@ func (p *StaticPool) Destroy() { go func(w *Worker) { defer wg.Done() - p.destroyWorker(w) + p.DestroyWorker(w) }(w) } wg.Wait() } +// DestroyWorker destroys workers and removes it from the pool. +func (p *StaticPool) DestroyWorker(w *Worker) { + p.throw(EventDestruct, w, nil) + + // detaching + p.muw.Lock() + for i, wc := range p.workers { + if wc == w { + p.workers = p.workers[:i+1] + break + } + } + p.muw.Unlock() + + go w.Stop() + + select { + case <-w.waitDone: + // worker is dead + case <-time.NewTimer(p.cfg.DestroyTimeout).C: + // failed to stop process + if err := w.Kill(); err != nil { + p.throw(EventError, w, err) + } + } +} + // finds free worker in a given time interval or creates new if allowed. func (p *StaticPool) allocateWorker() (w *Worker, err error) { select { @@ -168,7 +195,7 @@ func (p *StaticPool) allocateWorker() (w *Worker, err error) { // replaces dead or expired worker with new instance func (p *StaticPool) replaceWorker(w *Worker, caused interface{}) { - go p.destroyWorker(w) + go p.DestroyWorker(w) if nw, err := p.createWorker(); err != nil { p.throw(EventError, w, err) @@ -182,33 +209,6 @@ func (p *StaticPool) replaceWorker(w *Worker, caused interface{}) { } } -// destroy and remove worker from the pool. -func (p *StaticPool) destroyWorker(w *Worker) { - p.throw(EventDestruct, w, nil) - - // detaching - p.muw.Lock() - for i, wc := range p.workers { - if wc == w { - p.workers = p.workers[:i+1] - break - } - } - p.muw.Unlock() - - go w.Stop() - - select { - case <-w.waitDone: - // worker is dead - case <-time.NewTimer(p.cfg.DestroyTimeout).C: - // failed to stop process - if err := w.Kill(); err != nil { - p.throw(EventError, w, err) - } - } -} - // creates new worker using associated factory. automatically // adds worker to the worker list (background) func (p *StaticPool) createWorker() (*Worker, error) { |