Skip to content

Lightweight implementation of circuit breaker pattern in golang with generics.

License

Notifications You must be signed in to change notification settings

piotrpersona/cbreaker

Repository files navigation

cbreaker

Go Reference Tests passing Lint passing

Actively maintained implementation of circuit breaker in Golang with generics support.

Install

go get github.com/piotrpersona/cbreaker

Usage

cb := cbreaker.NewBreaker[int]()

res, err := cb.Try(func() (int, error) {
    // call
    return 123, nil
})

If result is not needed:

cb := cbreaker.NewNoRetBreaker()

err := cb.Try(func() error {
    // call
    return nil
})

Configure

Note: Circuit breaker object won't automatically retry in half-open state.

breaker := cbreaker.NewBreaker[int](
    // sets thershold after which the circuit becomes open
    cbreaker.WithThreshold(3),
    // sets timeout after which the circuit become half-open
    cbreaker.WithOpenTimeout(time.Second),
    // sets maximum number of retries in half-open state
    cbreaker.WithRetryThreshold(1),
    // registers stateChangeCallback
    cbreaker.WithStateChangeCallback(func(current, newState cbreaker.State) {
        log.Printf("state transition: %s -> %s", current, newState)
    }),
)

About

Lightweight implementation of circuit breaker pattern in golang with generics.

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published