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