• Stars
    star
    1,124
  • Rank 41,395 (Top 0.9 %)
  • Language
    Go
  • License
    MIT License
  • Created over 10 years ago
  • Updated 6 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Circuit Breakers in Go

circuitbreaker

Circuitbreaker provides an easy way to use the Circuit Breaker pattern in a Go program.

Circuit breakers are typically used when your program makes remote calls. Remote calls can often hang for a while before they time out. If your application makes a lot of these requests, many resources can be tied up waiting for these time outs to occur. A circuit breaker wraps these remote calls and will trip after a defined amount of failures or time outs occur. When a circuit breaker is tripped any future calls will avoid making the remote call and return an error to the caller. In the meantime, the circuit breaker will periodically allow some calls to be tried again and will close the circuit if those are successful.

You can read more about this pattern and how it's used at:

GoDoc

Installation

  go get github.com/rubyist/circuitbreaker

Examples

Here is a quick example of what circuitbreaker provides

// Creates a circuit breaker that will trip if the function fails 10 times
cb := circuit.NewThresholdBreaker(10)

events := cb.Subscribe()
go func() {
  for {
    e := <-events
    // Monitor breaker events like BreakerTripped, BreakerReset, BreakerFail, BreakerReady
  }
}()

cb.Call(func() error {
	// This is where you'll do some remote call
	// If it fails, return an error
}, 0)

Circuitbreaker can also wrap a time out around the remote call.

// Creates a circuit breaker that will trip after 10 failures
// using a time out of 5 seconds
cb := circuit.NewThresholdBreaker(10)

cb.Call(func() error {
  // This is where you'll do some remote call
  // If it fails, return an error
}, time.Second * 5) // This will time out after 5 seconds, which counts as a failure

// Proceed as above

Circuitbreaker can also trip based on the number of consecutive failures.

// Creates a circuit breaker that will trip if 10 consecutive failures occur
cb := circuit.NewConsecutiveBreaker(10)

// Proceed as above

Circuitbreaker can trip based on the error rate.

// Creates a circuit breaker based on the error rate
cb := circuit.NewRateBreaker(0.95, 100) // trip when error rate hits 95%, with at least 100 samples

// Proceed as above

If it doesn't make sense to wrap logic in Call(), breakers can be handled manually.

cb := circuit.NewThresholdBreaker(10)

for {
  if cb.Ready() {
    // Breaker is not tripped, proceed
    err := doSomething()
    if err != nil {
      cb.Fail() // This will trip the breaker once it's failed 10 times
      continue
    }
    cb.Success()
  } else {
    // Breaker is in a tripped state.
  }
}

Circuitbreaker also provides a wrapper around http.Client that will wrap a time out around any request.

// Passing in nil will create a regular http.Client.
// You can also build your own http.Client and pass it in
client := circuit.NewHTTPClient(time.Second * 5, 10, nil)

resp, err := client.Get("http://example.com/resource.json")

See the godoc for more examples.

Bugs, Issues, Feedback

Right here on GitHub: https://github.com/rubyist/circuitbreaker

More Repositories

1

guard-rake

guard-rake runs a rake task when files change
Ruby
94
star
2

go-oracle

Atom package for interfacing with Go's oracle tool
CoffeeScript
34
star
3

guppy

Ruby lib to parse tcx and gpx files from GPS devices
Ruby
32
star
4

tomato

A simple Tomato timer for the pomodoro technique
Objective-C
30
star
5

gohat

Go Heap Dump Analysis Tool
Go
13
star
6

jacker

An extremely elementary time tracker
Ruby
12
star
7

language-openscad

OpenSCAD language support in Atom
CoffeeScript
12
star
8

tracerx

Output tracing information in your Go app based on environment variables
Go
11
star
9

campfire-mode

Campfire major mode for emacs
Ruby
8
star
10

lockfile

Easy advisory file locking for Go
Go
8
star
11

pingduino

An Arduino based ping pong game score keeper
C
8
star
12

dsp

A utility library I'm throwing together while learning DSP with http://www.dspguide.com/
Ruby
6
star
13

tisemu

TIS-100 emulator using Go
Go
6
star
14

pi-pong

A Ping Pong score keeper for Raspberry Pi / BeagleBone Black
Go
4
star
15

msdumper

Dump and graph MemStats for your Go program
Go
4
star
16

hateoas-maze

Maze HATEOAS example from Building Hypermedia APIs w/HTML5 & Node with a Rails backend
JavaScript
4
star
17

ECSlidingTemplate

A template for using ECSlidingViewController without storyboards
Objective-C
4
star
18

supermet

Simple osx metronome application
Objective-C
4
star
19

emacs-config

My emacs configuration
Emacs Lisp
4
star
20

rubyist.github.com

github page
3
star
21

insurance

Ruby C0 code coverage
3
star
22

science

A go package for measuring and validating code changes without altering behavior
Go
3
star
23

sbrun

A minimal run dialog written against GTK+2 circa 2002 or so
C
3
star
24

mcbans

Access the MCBans API from ruby
Ruby
3
star
25

haulbox

Arduino code for truck sim switch box
C++
2
star
26

twitter-scrubber

A Safari 5 extension for removing annoyances from twitter.com
2
star
27

flac2aac

Convert flacs to aacs and import into iTunes, preserving metadata
2
star
28

pabu.me

The source for pabu.me
JavaScript
2
star
29

darts

Dart scoring app written to learn CoffeeScript and Raphael JS
JavaScript
2
star
30

seacrest_and_bieber_suck

Remove stupid seacrest/bieber ad from yfrog
2
star
31

rgte

Email Filter
Ruby
2
star
32

linkeater

IRC bot that sucks up links
Go
1
star
33

orienteering

Minecraft server plugin for waypoints
Ruby
1
star
34

tdrum

A toy drum sequencer for the go challenge
Go
1
star
35

dotfiles

dotfiles
Emacs Lisp
1
star
36

dots

dots dots dots
Lua
1
star
37

emacs

my emacs config
Emacs Lisp
1
star
38

shrtly

A fun URL shortener
Elixir
1
star