diff options
author | Wolfy-J <[email protected]> | 2018-06-05 23:18:55 +0300 |
---|---|---|
committer | Wolfy-J <[email protected]> | 2018-06-05 23:18:55 +0300 |
commit | c08c5c0e3d09a8ceec59dc4e9d48884de45096b4 (patch) | |
tree | 3fe005735cd6905cfabb7fdd291fac3769d9a6d9 /static_pool.go | |
parent | e594c7070aad609c4caeda760671aca00e638561 (diff) |
fixing controlled descruction
Diffstat (limited to 'static_pool.go')
-rw-r--r-- | static_pool.go | 61 |
1 files changed, 30 insertions, 31 deletions
diff --git a/static_pool.go b/static_pool.go index b6bb6efa..c0075c8f 100644 --- a/static_pool.go +++ b/static_pool.go @@ -189,35 +189,6 @@ func (p *StaticPool) replaceWorker(w *Worker, caused interface{}) error { return nil } -// destroyWorker destroys workers and removes it from the pool. -func (p *StaticPool) destroyWorker(w *Worker) { - // detaching - p.muw.Lock() - for i, wc := range p.workers { - if wc == w { - p.workers = append(p.workers[:i], p.workers[i+1:]...) - break - } - } - p.muw.Unlock() - - go w.Stop() - - select { - case <-w.waitDone: - // worker is dead - p.throw(EventWorkerDestruct, w) - - case <-time.NewTimer(p.cfg.DestroyTimeout).C: - // failed to stop process in given time - if err := w.Kill(); err != nil { - p.throw(EventWorkerError, WorkerError{Worker: w, Caused: err}) - } - - p.throw(EventWorkerKill, w) - } -} - // creates new worker using associated factory. automatically // adds worker to the worker list (background) func (p *StaticPool) createWorker() (*Worker, error) { @@ -231,8 +202,7 @@ func (p *StaticPool) createWorker() (*Worker, error) { go func(w *Worker) { err := w.Wait() - // worker have died unexpectedly, - // pool should attempt to replace it with alive version safely + // worker have died unexpectedly, pool should attempt to replace it with alive version safely if w.state.Value() != StateStopped { if err != nil { p.throw(EventWorkerError, WorkerError{Worker: w, Caused: err}) @@ -253,6 +223,35 @@ func (p *StaticPool) createWorker() (*Worker, error) { return w, nil } +// destroyWorker destroys workers and removes it from the pool. +func (p *StaticPool) destroyWorker(w *Worker) { + // detaching + p.muw.Lock() + for i, wc := range p.workers { + if wc == w { + p.workers = append(p.workers[:i], p.workers[i+1:]...) + break + } + } + p.muw.Unlock() + + go w.Stop() + + select { + case <-w.waitDone: + // worker is dead + p.throw(EventWorkerDestruct, w) + + case <-time.NewTimer(p.cfg.DestroyTimeout).C: + // failed to stop process in given time + if err := w.Kill(); err != nil { + p.throw(EventWorkerError, WorkerError{Worker: w, Caused: err}) + } + + p.throw(EventWorkerKill, w) + } +} + // throw invokes event handler if any. func (p *StaticPool) throw(event int, ctx interface{}) { if p.observer != nil { |