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:
- Host: GitHub
- URL: https://github.com/devjefster/goretry
- Owner: devjefster
- License: mit
- Created: 2025-02-15T20:28:44.000Z (over 1 year ago)
- Default Branch: main
- Last Pushed: 2025-02-15T21:02:25.000Z (over 1 year ago)
- Last Synced: 2025-02-15T22:18:22.312Z (over 1 year ago)
- Language: Go
- Size: 10.7 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
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 ๐