MCPcopy Index your code
hub / github.com/Rican7/retry

github.com/Rican7/retry @v0.3.1

Chat with this repo
repository ↗ · DeepWiki ↗ · release v0.3.1 ↗ · + Follow
57 symbols 164 edges 10 files 23 documented · 40% 7 cross-repo links updated 3y agov0.3.1 · 2021-08-12★ 4791 open issues
What it actually does AI analysis from the code graph — generated when you open this
loading…
README

retry

Build Status Coverage Status Go Report Card Go Reference Latest Stable Version

A simple, stateless, functional mechanism to perform actions repetitively until successful.

Project Status

This project is currently in "pre-release". While the code is heavily tested, the API may change. Use a tagged version or vendor this dependency if you plan on using it.

That said, this code has been used in production without issue for years, and has been used by some relatively high-profile projects/codebases.

Examples

Basic

retry.Retry(func(attempt uint) error {
    return nil // Do something that may or may not cause an error
})

File Open

const logFilePath = "/var/log/myapp.log"

var logFile *os.File

err := retry.Retry(func(attempt uint) error {
    var err error

    logFile, err = os.Open(logFilePath)

    return err
})

if err != nil {
    log.Fatalf("Unable to open file %q with error %q", logFilePath, err)
}

logFile.Chdir() // Do something with the file

HTTP request with strategies and backoff

var response *http.Response

action := func(attempt uint) error {
    var err error

    response, err = http.Get("https://api.github.com/repos/Rican7/retry")

    if err == nil && response != nil && response.StatusCode > 200 {
        err = fmt.Errorf("failed to fetch (attempt #%d) with status code: %d", attempt, response.StatusCode)
    }

    return err
}

err := retry.Retry(
    action,
    strategy.Limit(5),
    strategy.Backoff(backoff.Fibonacci(10*time.Millisecond)),
)

if err != nil {
    log.Fatalf("Failed to fetch repository with error %q", err)
}

Retry with backoff jitter

action := func(attempt uint) error {
    return errors.New("something happened")
}

seed := time.Now().UnixNano()
random := rand.New(rand.NewSource(seed))

retry.Retry(
    action,
    strategy.Limit(5),
    strategy.BackoffWithJitter(
        backoff.BinaryExponential(10*time.Millisecond),
        jitter.Deviation(random, 0.5),
    ),
)

Extension points exported contracts — how you extend this code

Action (FuncType)
Action defines a callable function that package retry can handle.
retry.go
Transformation (FuncType)
Transformation defines a function that calculates a time.Duration based on the given duration.
jitter/jitter.go
Strategy (FuncType)
Strategy defines a function that Retry calls before every successive attempt to determine whether it should make the nex
strategy/strategy.go
Algorithm (FuncType)
Algorithm defines a function that calculates a time.Duration based on the given retry attempt number.
backoff/backoff.go

Core symbols most depended-on inside this repo

shouldAttempt
called by 10
retry.go
Retry
called by 7
retry.go
fallbackNewRandom
called by 6
jitter/jitter.go
Limit
called by 3
strategy/strategy.go
Wait
called by 3
strategy/strategy.go
BackoffWithJitter
called by 3
strategy/strategy.go
Exponential
called by 3
backoff/backoff.go
BinaryExponential
called by 3
backoff/backoff.go

Shape

Function 53
FuncType 4

Languages

Go100%

Modules by API surface

backoff/backoff_test.go11 symbols
strategy/strategy_test.go8 symbols
strategy/strategy.go7 symbols
backoff/backoff.go7 symbols
retry_test.go6 symbols
jitter/jitter.go6 symbols
jitter/jitter_test.go5 symbols
example_test.go4 symbols
retry.go3 symbols

For agents

$ claude mcp add retry \
  -- python -m otcore.mcp_server <graph>

⬇ download graph artifact

Ask about this repo answers extend the page