Documentation ¶
Overview ¶
Package errguard makes it easy to retry error conditions that have a resonable chance of succeeding after a short pause. These error conditions include optimistic locking and deadlock.
Example ¶
package main import ( "context" "errors" "log" "math/rand" ) func main() { ctx := context.TODO() var guard Guard guard.Logger = loggerFunc(func(v ...interface{}) error { log.Println(v...) return nil }) guard.Run(ctx, func() error { return doSomethingWith(ctx) }) } func doSomethingWith(ctx context.Context) error { log.Println("doing something") // has a 90% chance of failure if rand.Intn(10) > 0 { // Retry will mark this error as being retryable return Retry(errors.New("error condition")) } return nil } type loggerFunc func(v ...interface{}) error func (f loggerFunc) Log(v ...interface{}) error { return f(v...) }
Output:
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
Types ¶
type Guard ¶
type Guard struct { // ShouldRetry returns true if the guard should retry // after encountering error err. If nil, the default // logic is used. ShouldRetry func(err error) bool // If logger is set, the guard will log a message every // time the guard encounters and error and retries. Logger Logger }
Guard is used to retry error conditions that have a reasonable chance of succeeding.
type Logger ¶
type Logger interface {
Log(v ...interface{}) error
}
Logger is the interface recognised by the guard.
var ( // ShouldRetry is the default test for whether // a guard should retry after encountering error err. // This value is used if not specified for an individual // guard. ShouldRetry func(err error) bool // DefaultLogger is the default logger to use if // not specified for an individual guard. DefaultLogger Logger )
Directories ¶
Path | Synopsis |
---|---|
cmd
|
|
Package gen generates code for errguard.
|
Package gen generates code for errguard. |
Click to show internal directories.
Click to hide internal directories.