Git Product home page Git Product logo

hoglet's Introduction

Build Status Go Reference Go Report Card

hoglet

Simple low-overhead circuit breaker library.

Usage

h, err := hoglet.NewCircuit(
    func(ctx context.Context, bar int) (Foo, error) {
        if bar == 42 {
            return Foo{Bar: bar}, nil
        }
        return Foo{}, fmt.Errorf("bar is not 42")
    },
    hoglet.NewSlidingWindowBreaker(5*time.Second, 0.1),
    hoglet.WithFailureCondition(hoglet.IgnoreContextCanceled),
)
/* if err != nil ... */

f, _ := h.Call(context.Background(), 42)
fmt.Println(f.Bar) // 42

_, err = h.Call(context.Background(), 0)
fmt.Println(err) // bar is not 42

_, err = h.Call(context.Background(), 42)
fmt.Println(err) // hoglet: breaker is open

time.Sleep(5 * time.Second)

f, _ = h.Call(context.Background(), 42)
fmt.Println(f.Bar) // 42

Operation

Each call to the wrapped function (via Circuit.Call) is tracked and its result "observed". Breakers then react to these observations according to their own logic, optionally opening the circuit.

An open circuit does not allow any calls to go through, and will return an error immediately.

If the wrapped function blocks, Circuit.Call will block as well, but any context cancellations or expirations will count towards the failure rate, allowing the circuit to respond timely to failures, while still having well-defined and non-racy behavior around the failed function.

Design

Hoglet prefers throughput to correctness (e.g. by avoiding locks), which means it cannot guarantee an exact number of calls will go through.

hoglet's People

Contributors

costela avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.