123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172 |
- package backoff
- import (
- "context"
- "log"
- )
- 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
- }
|