Skip to content

Lightweight fault tolerance library written in Go Inspired by resilience4j

License

Notifications You must be signed in to change notification settings

GaruGaru/resilience4go

Repository files navigation

resilience4go Build Status Coverage Status

Lightweight fault tolerance library written in Go Inspired by resilience4j and hystrix

Usage

import (
    "github.com/garugaru/resilience4go/circuitbreaker"
    "github.com/garugaru/resilience4go/fallback"
    "github.com/garugaru/resilience4go/resilience"
    "github.com/garugaru/resilience4go/retry"
)

var executor = resilience.New[string](
    // retry 10 times with a 10ms delay
    retry.New[string](retry.NewFixedDelay(10*time.Millisecond), 10),
    // circuit breaker based on error rate for the last 10 requests
    circuitbreaker.New[string](circuitbreaker.NewCountBasedWindow(10), 0.5, 0.5, 100*time.Millisecond),
    // if everything else fails, return a fallback value instead of error
    fallback.New[string]("fallback"),
)

out, err := executor.Execute(func() (string, error) {
        // failing function 
	return callVeryBadExternalService()
})

Features

Retries

Retry a failing function a fixed amount of times using customizable delay strategy. Available strategies are:

  • FixedDelay
  • ExponentialBackoff

Circuit breaker

A circuit breaker is a pattern used to protect external dependencies from being overwhelmed with requests in case of failures.

The circuit breaker takes in account the samples for the last N calls calculating the success / error rate, once the error rate exceeds a user-defined threshold the requests are discarded for a fixed amount of time giving the external service the time to be restored. After a customizable amount of time a percentage of the requests will be forwarded to the service in order to check if it's still unavailable, once the service operability is restored the circuit will be closed and all the requests will be delivered to the service.

Fallback

Return a fallback value instead of the error.

About

Lightweight fault tolerance library written in Go Inspired by resilience4j

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages