An open API service indexing awesome lists of open source software.

https://github.com/devjefster/goretry

This Go library provides a robust **retry mechanism** with support for:
https://github.com/devjefster/goretry

Last synced: about 1 month ago
JSON representation

This Go library provides a robust **retry mechanism** with support for:

Awesome Lists containing this project

README

          

# Go Retry Library

## ๐Ÿ“Œ Overview

This Go library provides a robust **retry mechanism** with support for:

- โœ… **Exponential, linear, and fixed backoff strategies**
- โœ… **Custom retry logic** with user-defined functions
- โœ… **Circuit breaker protection** to prevent excessive retries
- โœ… **Retryable HTTP errors** and generic error handling
- โœ… **Context-aware retries** with timeout and cancellation support

## ๐Ÿš€ Features

- **Multiple Backoff Strategies**: Fixed, Linear, Exponential, Custom
- **Circuit Breaker Support**: Prevents retrying failures indefinitely
- **Flexible Configuration**: Customize retry behavior easily
- **Retry Middleware**: Wraps HTTP handlers for automatic retries
- **Optimized Performance**: Uses maps for fast lookup of retryable errors

----------

## โš™ Installation

```sh
go get github.com/devjefster/GoRetry

```

----------

## ๐Ÿ›  Usage

### ๐Ÿ”น Basic Example

```go
package main

import (
"context"
"errors"
"fmt"
"time"
"github.com/devjefster/GoRetry"
)

func unstableOperation() error {
if time.Now().UnixNano()%2 == 0 {
return errors.New("temporary failure")
}
return nil
}

func main() {
cfg := retry.DefaultConfig
cfg.MaxRetries = 3

ctx := context.Background()
err := retry.Retry(ctx, unstableOperation, cfg)
if err != nil {
fmt.Println("Operation failed:", err)
} else {
fmt.Println("Operation succeeded")
}
}

```

----------

### ๐Ÿ”น Using Circuit Breaker

```go
cfg := retry.Config{
MaxRetries: 5,
CircuitBreakerThreshold: 2, // Stops retrying after 2 consecutive failures
}

```

----------

### ๐Ÿ”น Custom Backoff Function

```go
cfg := retry.Config{
BackoffStrategy: retry.CustomBackoff,
CustomBackoffFunc: func(attempt int) time.Duration {
return time.Duration(attempt) * 200 * time.Millisecond
},
}

```

----------

### ๐Ÿ”น HTTP Retry Middleware

Wrap your HTTP handlers with retry logic:

```go
mux := http.NewServeMux()
mux.Handle("/data", myHandler)

cfg := retry.DefaultConfig
cfg.RetryableStatusCodes = []int{500, 502, 503, 504}
wrappedHandler := retry.RetryMiddleware(cfg, mux)

http.ListenAndServe(":8080", wrappedHandler)

```

----------

## ๐Ÿงช Running Tests

```sh
go test -v ./...

```

----------

## ๐Ÿ“œ License

MIT License

----------

## ๐Ÿค Contributing

1. Fork the repo
2. Create a new branch (`git checkout -b feature-name`)
3. Commit your changes (`git commit -m 'Added new feature'`)
4. Push to the branch (`git push origin feature-name`)
5. Open a Pull Request ๐ŸŽ‰