Skip to content

Commit

Permalink
put back ticker example
Browse files Browse the repository at this point in the history
  • Loading branch information
cenkalti committed Dec 18, 2024
1 parent e5db35e commit 88e70f9
Showing 1 changed file with 36 additions and 0 deletions.
36 changes: 36 additions & 0 deletions example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"errors"
"fmt"
"log"
"net/http"
"strconv"
)
Expand Down Expand Up @@ -50,3 +51,38 @@ func ExampleRetry() {
fmt.Println(result)
// Output: hello
}

func ExampleTicker() {
// An operation that may fail.
operation := func() (string, error) {
return "hello", nil
}

ticker := NewTicker(NewExponentialBackOff())
defer ticker.Stop()

var result string
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 result, err = operation(); err != nil {
log.Println(err, "will retry...")
continue
}

break
}

if err != nil {
// Operation has failed.
fmt.Println("Error:", err)
return
}

// Operation is successful.

fmt.Println(result)
// Output: hello
}

0 comments on commit 88e70f9

Please sign in to comment.