Top Related Projects
⏱ The exponential backoff algorithm in Go
Netflix's Hystrix latency and fault tolerance library, for Go
Circuit Breaker implemented in Go
Circuit Breakers in Go
Quick Overview
The eapache/go-resiliency repository is a Go library that provides resilience patterns for building robust and fault-tolerant applications. It offers implementations of common resilience strategies such as retries, circuit breakers, and rate limiters, allowing developers to easily incorporate these patterns into their Go projects.
Pros
- Easy to use and integrate into existing Go projects
- Provides multiple resilience patterns in a single library
- Well-documented with clear examples
- Lightweight and has no external dependencies
Cons
- Limited to a few specific resilience patterns
- May require additional customization for complex use cases
- Not actively maintained (last commit was over 2 years ago)
- Lacks some advanced features found in more comprehensive resilience libraries
Code Examples
- Using the Retry pattern:
r := retry.New(retry.ConstantBackoff(3, 100*time.Millisecond), nil)
err := r.Run(func() error {
// Your code that might fail
return someOperation()
})
- Implementing a Circuit Breaker:
cb := circuitbreaker.NewThresholdBreaker(10)
result, err := cb.Execute(func() (interface{}, error) {
// Your code that might fail
return someRiskyOperation()
})
- Using a Rate Limiter:
rl := ratelimit.New(10, time.Second) // 10 requests per second
for i := 0; i < 20; i++ {
rl.Call(func() error {
// Your rate-limited code
return makeAPIRequest()
})
}
Getting Started
To use the go-resiliency library in your Go project, follow these steps:
-
Install the library:
go get github.com/eapache/go-resiliency
-
Import the desired package in your Go code:
import ( "github.com/eapache/go-resiliency/retrier" "github.com/eapache/go-resiliency/circuitbreaker" "github.com/eapache/go-resiliency/ratelimit" )
-
Use the imported packages to implement resilience patterns in your code, as shown in the examples above.
Competitor Comparisons
⏱ The exponential backoff algorithm in Go
Pros of backoff
- More comprehensive retry strategies, including exponential backoff with jitter
- Better support for context cancellation and deadlines
- More active development and maintenance
Cons of backoff
- Slightly more complex API, which may be overkill for simple use cases
- Less focus on other resilience patterns beyond retries
Code Comparison
backoff:
b := backoff.NewExponentialBackOff()
err := backoff.Retry(operation, b)
go-resiliency:
r := resiliency.NewRetrier(resiliency.ExponentialBackoff(5, 10*time.Millisecond))
err := r.Run(func() error {
return operation()
})
Both libraries provide similar functionality for implementing retry mechanisms, but backoff offers more advanced options and better integration with Go's context package. go-resiliency, on the other hand, provides a simpler API and includes additional resilience patterns like circuit breakers and semaphores.
The choice between the two depends on the specific requirements of your project. If you need advanced retry strategies and better context support, backoff might be the better choice. However, if you're looking for a simpler API or require additional resilience patterns, go-resiliency could be more suitable.
Netflix's Hystrix latency and fault tolerance library, for Go
Pros of hystrix-go
- More comprehensive circuit breaker implementation with additional features like fallbacks and metrics
- Better integration with monitoring and dashboard tools
- More active development and community support
Cons of hystrix-go
- Higher complexity and steeper learning curve
- More dependencies and potential overhead
- Less flexibility for custom circuit breaker configurations
Code Comparison
hystrix-go:
hystrix.ConfigureCommand("my_command", hystrix.CommandConfig{
Timeout: 1000,
MaxConcurrentRequests: 100,
ErrorPercentThreshold: 25,
})
err := hystrix.Do("my_command", func() error {
// Do work here
return nil
}, nil)
go-resiliency:
cb := resiliency.NewCircuitBreaker(
resiliency.CircuitBreakerConfig{
ErrorThreshold: 5,
SuccessThreshold: 5,
Timeout: time.Second * 60,
},
)
err := cb.Run(func() error {
// Do work here
return nil
})
Both libraries provide circuit breaker functionality, but hystrix-go offers a more feature-rich implementation with built-in command configuration and fallback support. go-resiliency, on the other hand, provides a simpler and more lightweight approach, focusing on core circuit breaker functionality with greater flexibility for custom configurations.
Circuit Breaker implemented in Go
Pros of gobreaker
- More focused and specialized for circuit breaking
- Simpler API with fewer concepts to learn
- Better documentation and examples
Cons of gobreaker
- Less flexible compared to go-resiliency's multiple patterns
- Fewer configuration options for fine-tuning behavior
- Limited to circuit breaking, while go-resiliency offers additional resilience patterns
Code Comparison
gobreaker:
cb := gobreaker.NewCircuitBreaker(gobreaker.Settings{
Name: "my-circuit-breaker",
MaxRequests: 3,
Interval: 5 * time.Second,
Timeout: 30 * time.Second,
})
result, err := cb.Execute(func() (interface{}, error) {
// Your function logic here
})
go-resiliency:
cb := circuitbreaker.NewCircuitBreaker(circuitbreaker.Settings{
RequestVolumeThreshold: 3,
ErrorThresholdPercentage: 50,
SleepWindow: 30 * time.Second,
})
err := cb.Run(func() error {
// Your function logic here
return nil
})
Both libraries provide circuit breaking functionality, but gobreaker offers a simpler API with fewer configuration options. go-resiliency, on the other hand, provides more flexibility and additional resilience patterns beyond circuit breaking. The choice between the two depends on the specific needs of your project and the desired level of control over the circuit breaker behavior.
Circuit Breakers in Go
Pros of circuitbreaker
- Simpler API with fewer concepts to learn
- Includes a basic retry mechanism out of the box
- Lightweight implementation with minimal dependencies
Cons of circuitbreaker
- Less flexible configuration options
- Limited to circuit breaker pattern only
- Lacks advanced features like rate limiting or retries with backoff
Code Comparison
circuitbreaker:
breaker = CircuitBreaker.new(
invocation_timeout: 10,
failure_threshold: 5,
reset_timeout: 60
)
breaker.run do
# Your code here
end
go-resiliency:
cb := circuitbreaker.NewThresholdBreaker(10)
retrier := retrier.New(retrier.ExponentialBackoff(5, 10*time.Millisecond), nil)
err := cb.Run(func() error {
return retrier.Run(func() error {
// Your code here
})
})
go-resiliency offers a more comprehensive set of resiliency patterns, including circuit breakers, retries with backoff, and rate limiting. It provides greater flexibility and configurability, making it suitable for complex scenarios. However, this comes at the cost of a steeper learning curve and more verbose code.
circuitbreaker, on the other hand, focuses solely on the circuit breaker pattern with a simpler API. It's easier to get started with but may lack advanced features needed in some use cases. The choice between the two depends on the specific requirements of your project and the desired balance between simplicity and functionality.
Convert designs to code with AI
Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.
Try Visual CopilotREADME
go-resiliency
Resiliency patterns for golang. Based in part on Hystrix, Semian, and others.
Currently implemented patterns include:
- circuit-breaker (in the
breaker
directory) - semaphore (in the
semaphore
directory) - deadline/timeout (in the
deadline
directory) - batching (in the
batcher
directory) - retriable (in the
retrier
directory)
Note: I will occasionally bump the minimum required Golang version without bumping the major version of this package, which violates the official Golang packaging convention around breaking changes. Typically the versions being dropped are multiple years old and long unsupported.
Top Related Projects
⏱ The exponential backoff algorithm in Go
Netflix's Hystrix latency and fault tolerance library, for Go
Circuit Breaker implemented in Go
Circuit Breakers in Go
Convert designs to code with AI
Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.
Try Visual Copilot