2016-04-13 20:36:23 +02:00
|
|
|
package safe
|
|
|
|
|
|
|
|
import (
|
2017-02-02 21:07:44 +01:00
|
|
|
"context"
|
2016-12-08 13:32:12 +01:00
|
|
|
"fmt"
|
2016-04-13 20:36:23 +02:00
|
|
|
"runtime/debug"
|
|
|
|
"sync"
|
2017-04-30 14:39:49 +02:00
|
|
|
|
2020-02-26 10:36:05 +01:00
|
|
|
"github.com/cenkalti/backoff/v4"
|
2020-09-16 15:46:04 +02:00
|
|
|
"github.com/traefik/traefik/v2/pkg/log"
|
2016-04-13 20:36:23 +02:00
|
|
|
)
|
|
|
|
|
2016-08-18 13:03:10 +02:00
|
|
|
type routineCtx func(ctx context.Context)
|
|
|
|
|
2020-05-11 12:06:07 +02:00
|
|
|
// Pool is a pool of go routines.
|
2016-04-13 20:36:23 +02:00
|
|
|
type Pool struct {
|
2020-02-03 17:56:04 +01:00
|
|
|
waitGroup sync.WaitGroup
|
|
|
|
ctx context.Context
|
|
|
|
cancel context.CancelFunc
|
2016-08-18 13:03:10 +02:00
|
|
|
}
|
|
|
|
|
2020-05-11 12:06:07 +02:00
|
|
|
// NewPool creates a Pool.
|
2016-08-18 14:20:11 +02:00
|
|
|
func NewPool(parentCtx context.Context) *Pool {
|
2020-02-03 17:56:04 +01:00
|
|
|
ctx, cancel := context.WithCancel(parentCtx)
|
2016-08-18 13:03:10 +02:00
|
|
|
return &Pool{
|
2020-02-03 17:56:04 +01:00
|
|
|
ctx: ctx,
|
|
|
|
cancel: cancel,
|
2016-08-18 13:03:10 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-11 12:06:07 +02:00
|
|
|
// GoCtx starts a recoverable goroutine with a context.
|
2016-08-18 13:03:10 +02:00
|
|
|
func (p *Pool) GoCtx(goroutine routineCtx) {
|
|
|
|
p.waitGroup.Add(1)
|
|
|
|
Go(func() {
|
2019-08-26 16:54:05 +08:00
|
|
|
defer p.waitGroup.Done()
|
2016-08-18 13:03:10 +02:00
|
|
|
goroutine(p.ctx)
|
|
|
|
})
|
2016-04-13 20:36:23 +02:00
|
|
|
}
|
|
|
|
|
2020-05-11 12:06:07 +02:00
|
|
|
// Stop stops all started routines, waiting for their termination.
|
2016-04-13 20:36:23 +02:00
|
|
|
func (p *Pool) Stop() {
|
2016-08-18 13:03:10 +02:00
|
|
|
p.cancel()
|
2016-04-13 20:36:23 +02:00
|
|
|
p.waitGroup.Wait()
|
2016-11-15 19:14:11 +00:00
|
|
|
}
|
|
|
|
|
2020-05-11 12:06:07 +02:00
|
|
|
// Go starts a recoverable goroutine.
|
2016-04-13 20:36:23 +02:00
|
|
|
func Go(goroutine func()) {
|
|
|
|
GoWithRecover(goroutine, defaultRecoverGoroutine)
|
|
|
|
}
|
|
|
|
|
2020-05-11 12:06:07 +02:00
|
|
|
// GoWithRecover starts a recoverable goroutine using given customRecover() function.
|
2016-04-13 20:36:23 +02:00
|
|
|
func GoWithRecover(goroutine func(), customRecover func(err interface{})) {
|
|
|
|
go func() {
|
|
|
|
defer func() {
|
|
|
|
if err := recover(); err != nil {
|
|
|
|
customRecover(err)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
goroutine()
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
|
|
|
|
func defaultRecoverGoroutine(err interface{}) {
|
2019-09-13 19:28:04 +02:00
|
|
|
logger := log.WithoutContext()
|
|
|
|
logger.Errorf("Error in Go routine: %s", err)
|
|
|
|
logger.Errorf("Stack: %s", debug.Stack())
|
2016-04-13 20:36:23 +02:00
|
|
|
}
|
2016-12-08 13:32:12 +01:00
|
|
|
|
2020-05-11 12:06:07 +02:00
|
|
|
// OperationWithRecover wrap a backoff operation in a Recover.
|
2016-12-08 13:32:12 +01:00
|
|
|
func OperationWithRecover(operation backoff.Operation) backoff.Operation {
|
|
|
|
return func() (err error) {
|
|
|
|
defer func() {
|
|
|
|
if res := recover(); res != nil {
|
2016-12-08 13:32:52 +01:00
|
|
|
defaultRecoverGoroutine(res)
|
2020-05-11 12:06:07 +02:00
|
|
|
err = fmt.Errorf("panic in operation: %w", err)
|
2016-12-08 13:32:12 +01:00
|
|
|
}
|
|
|
|
}()
|
2016-12-08 13:32:52 +01:00
|
|
|
return operation()
|
2016-12-08 13:32:12 +01:00
|
|
|
}
|
|
|
|
}
|