mirror of
https://github.com/octoleo/restic.git
synced 2024-11-14 17:24:10 +00:00
74 lines
1.2 KiB
Go
74 lines
1.2 KiB
Go
package backoff
|
|
|
|
import (
|
|
"log"
|
|
|
|
"golang.org/x/net/context"
|
|
)
|
|
|
|
func ExampleRetry() {
|
|
// An operation that may fail.
|
|
operation := func() error {
|
|
return nil // or an error
|
|
}
|
|
|
|
err := Retry(operation, NewExponentialBackOff())
|
|
if err != nil {
|
|
// Handle error.
|
|
return
|
|
}
|
|
|
|
// Operation is successful.
|
|
}
|
|
|
|
func ExampleRetryContext() {
|
|
// A context
|
|
ctx := context.Background()
|
|
|
|
// An operation that may fail.
|
|
operation := func() error {
|
|
return nil // or an error
|
|
}
|
|
|
|
b := WithContext(NewExponentialBackOff(), ctx)
|
|
|
|
err := Retry(operation, b)
|
|
if err != nil {
|
|
// Handle error.
|
|
return
|
|
}
|
|
|
|
// Operation is successful.
|
|
}
|
|
|
|
func ExampleTicker() {
|
|
// An operation that may fail.
|
|
operation := func() error {
|
|
return nil // or an error
|
|
}
|
|
|
|
ticker := NewTicker(NewExponentialBackOff())
|
|
|
|
var err error
|
|
|
|
// Ticks will continue to arrive when the previous operation is still running,
|
|
// so operations that take a while to fail could run in quick succession.
|
|
for _ = range ticker.C {
|
|
if err = operation(); err != nil {
|
|
log.Println(err, "will retry...")
|
|
continue
|
|
}
|
|
|
|
ticker.Stop()
|
|
break
|
|
}
|
|
|
|
if err != nil {
|
|
// Operation has failed.
|
|
return
|
|
}
|
|
|
|
// Operation is successful.
|
|
return
|
|
}
|