Add Operation with recover

This commit is contained in:
Emile Vauge 2016-12-08 13:32:12 +01:00
parent a394e6a3e3
commit be362f0d9f
No known key found for this signature in database
GPG key ID: D808B4C167352E59
11 changed files with 45 additions and 12 deletions

View file

@ -1,11 +1,12 @@
package safe
import (
"fmt"
"github.com/cenk/backoff"
"github.com/containous/traefik/log"
"context"
"runtime/debug"
"sync"
"github.com/containous/traefik/log"
)
type routine struct {
@ -145,3 +146,17 @@ func defaultRecoverGoroutine(err interface{}) {
log.Errorf("Error in Go routine: %s", err)
debug.PrintStack()
}
// OperationWithRecover wrap a backoff operation in a Recover
func OperationWithRecover(operation backoff.Operation) backoff.Operation {
return func() (err error) {
defer func() {
if res := recover(); res != nil {
defaultRecoverGoroutine(err)
err = fmt.Errorf("Panic in operation: %s", err)
}
}()
err = operation()
return nil
}
}