forked from Ivasoft/traefik
@@ -1,8 +1,8 @@
|
||||
package safe
|
||||
|
||||
import (
|
||||
"github.com/containous/traefik/log"
|
||||
"golang.org/x/net/context"
|
||||
"log"
|
||||
"runtime/debug"
|
||||
"sync"
|
||||
)
|
||||
@@ -26,18 +26,26 @@ type Pool struct {
|
||||
}
|
||||
|
||||
// NewPool creates a Pool
|
||||
func NewPool(baseCtx context.Context) *Pool {
|
||||
func NewPool(parentCtx context.Context) *Pool {
|
||||
baseCtx, _ := context.WithCancel(parentCtx)
|
||||
ctx, cancel := context.WithCancel(baseCtx)
|
||||
return &Pool{
|
||||
baseCtx: baseCtx,
|
||||
ctx: ctx,
|
||||
cancel: cancel,
|
||||
baseCtx: baseCtx,
|
||||
}
|
||||
}
|
||||
|
||||
// Ctx returns main context
|
||||
func (p *Pool) Ctx() context.Context {
|
||||
return p.ctx
|
||||
return p.baseCtx
|
||||
}
|
||||
|
||||
//AddGoCtx adds a recoverable goroutine with a context without starting it
|
||||
func (p *Pool) AddGoCtx(goroutine routineCtx) {
|
||||
p.lock.Lock()
|
||||
p.routinesCtx = append(p.routinesCtx, goroutine)
|
||||
p.lock.Unlock()
|
||||
}
|
||||
|
||||
//GoCtx starts a recoverable goroutine with a context
|
||||
@@ -71,6 +79,7 @@ func (p *Pool) Go(goroutine func(stop chan bool)) {
|
||||
// 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
|
||||
@@ -79,12 +88,12 @@ func (p *Pool) Stop() {
|
||||
for _, routine := range p.routines {
|
||||
close(routine.stop)
|
||||
}
|
||||
p.lock.Unlock()
|
||||
}
|
||||
|
||||
// Start starts all stoped routines
|
||||
// Start starts all stopped routines
|
||||
func (p *Pool) Start() {
|
||||
p.lock.Lock()
|
||||
defer p.lock.Unlock()
|
||||
p.ctx, p.cancel = context.WithCancel(p.baseCtx)
|
||||
for _, routine := range p.routines {
|
||||
p.waitGroup.Add(1)
|
||||
@@ -102,7 +111,6 @@ func (p *Pool) Start() {
|
||||
p.waitGroup.Done()
|
||||
})
|
||||
}
|
||||
p.lock.Unlock()
|
||||
}
|
||||
|
||||
// Go starts a recoverable goroutine
|
||||
@@ -123,6 +131,6 @@ func GoWithRecover(goroutine func(), customRecover func(err interface{})) {
|
||||
}
|
||||
|
||||
func defaultRecoverGoroutine(err interface{}) {
|
||||
log.Println(err)
|
||||
log.Errorf("Error in Go routine: %s", err)
|
||||
debug.PrintStack()
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user