Giter Site home page Giter Site logo

fastbreaker's Introduction

fastbreaker

Go Reference CI

fastbreaker implements the Circuit Breaker pattern in Go.

Installation

go get github.com/bluekiri/fastbreaker

Usage

The interface fastbreaker.FastBreaker is a state machine to prevent sending requests that are likely to fail. The function fastbreaker.New creates a new fastbreaker.FastBreaker.

func fastbreaker.New(configuration fastbreaker.Configuration) fastbreaker.FastBreaker

You can configure fastbreaker.FastBreaker by the struct fastbreaker.Configuration:

type Configuration struct {
    NumBuckets      int
    BucketDuration  time.Duration
    DurationOfBreak time.Duration
    ShouldTrip      ShouldTripFunc
}
  • NumBuckets is the number of buckets of the rolling window. If NumBuckets is less than 1, the fastbreaker.DefaultNumBuckets is used.

  • BucketDuration is the duration (truncated to the second) of every bucket. If BucketDuration is less than 1s, the fastbreaker.DefaultBucketDuration is used.

  • DurationOfBreak is the time (truncated to the second) of the open state, after which the state becomes half-open. If DurationOfBreak is less than 1s, the fastbreaker.DefaultDurationOfBreak is used.

  • ShouldTrip is called whenever a request fails in the closed state with the number of executions and the number of failures. If ShouldTrip returns true, fastbreaker.FastBreaker state becomes open. If ShouldTrip is nil, fastbreaker.DefaultShouldTrip is used. fastbreaker.DefaultShouldTrip returns true when the number of executions is greater than or equal to 10 and at least half the number of executions have failed.

Example

var cb fastbreaker.FastBreaker

func Get(url string) ([]byte, error) {
	feedback, err := cb.Allow()
	if err != nil {
		return nil, err
	}

	resp, err := http.Get(url)
	if err != nil {
		feedback(false)
		return nil, err
	}

	defer resp.Body.Close()
	body, err := ioutil.ReadAll(resp.Body)
	if err != nil {
		feedback(false)
		return nil, err
	}

	feedback(true)
	return body, nil
}

License

The MIT License (MIT)

See LICENSE for details.

fastbreaker's People

Contributors

ellull avatar

Stargazers

 avatar

Watchers

 avatar  avatar  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.