• Stars
    star
    2,606
  • Rank 17,564 (Top 0.4 %)
  • Language
    Go
  • License
    MIT License
  • Created over 9 years ago
  • Updated about 1 year ago

Reviews

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

Repository Details

Circuit Breaker implemented in Go

gobreaker

GoDoc

gobreaker implements the Circuit Breaker pattern in Go.

Installation

go get github.com/sony/gobreaker

Usage

The struct CircuitBreaker is a state machine to prevent sending requests that are likely to fail. The function NewCircuitBreaker creates a new CircuitBreaker.

func NewCircuitBreaker(st Settings) *CircuitBreaker

You can configure CircuitBreaker by the struct Settings:

type Settings struct {
	Name          string
	MaxRequests   uint32
	Interval      time.Duration
	Timeout       time.Duration
	ReadyToTrip   func(counts Counts) bool
	OnStateChange func(name string, from State, to State)
	IsSuccessful  func(err error) bool
}
  • Name is the name of the CircuitBreaker.

  • MaxRequests is the maximum number of requests allowed to pass through when the CircuitBreaker is half-open. If MaxRequests is 0, CircuitBreaker allows only 1 request.

  • Interval is the cyclic period of the closed state for CircuitBreaker to clear the internal Counts, described later in this section. If Interval is 0, CircuitBreaker doesn't clear the internal Counts during the closed state.

  • Timeout is the period of the open state, after which the state of CircuitBreaker becomes half-open. If Timeout is 0, the timeout value of CircuitBreaker is set to 60 seconds.

  • ReadyToTrip is called with a copy of Counts whenever a request fails in the closed state. If ReadyToTrip returns true, CircuitBreaker will be placed into the open state. If ReadyToTrip is nil, default ReadyToTrip is used. Default ReadyToTrip returns true when the number of consecutive failures is more than 5.

  • OnStateChange is called whenever the state of CircuitBreaker changes.

  • IsSuccessful is called with the error returned from a request. If IsSuccessful returns true, the error is counted as a success. Otherwise the error is counted as a failure. If IsSuccessful is nil, default IsSuccessful is used, which returns false for all non-nil errors.

The struct Counts holds the numbers of requests and their successes/failures:

type Counts struct {
	Requests             uint32
	TotalSuccesses       uint32
	TotalFailures        uint32
	ConsecutiveSuccesses uint32
	ConsecutiveFailures  uint32
}

CircuitBreaker clears the internal Counts either on the change of the state or at the closed-state intervals. Counts ignores the results of the requests sent before clearing.

CircuitBreaker can wrap any function to send a request:

func (cb *CircuitBreaker) Execute(req func() (interface{}, error)) (interface{}, error)

The method Execute runs the given request if CircuitBreaker accepts it. Execute returns an error instantly if CircuitBreaker rejects the request. Otherwise, Execute returns the result of the request. If a panic occurs in the request, CircuitBreaker handles it as an error and causes the same panic again.

Example

var cb *breaker.CircuitBreaker

func Get(url string) ([]byte, error) {
	body, err := cb.Execute(func() (interface{}, error) {
		resp, err := http.Get(url)
		if err != nil {
			return nil, err
		}

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

		return body, nil
	})
	if err != nil {
		return nil, err
	}

	return body.([]byte), nil
}

See example for details.

License

The MIT License (MIT)

See LICENSE for details.

More Repositories

1

sonyflake

A distributed unique ID generator inspired by Twitter's Snowflake
Go
3,484
star
2

nnabla

Neural Network Libraries
Python
2,634
star
3

flutter-embedded-linux

Embedded Linux embedding for Flutter
C++
995
star
4

flutter-elinux

Flutter tools for embedded Linux (eLinux)
Dart
411
star
5

v8eval

Multi-language bindings to JavaScript engine V8
C++
399
star
6

ai-research-code

Python
316
star
7

model_optimization

Model Compression Toolkit (MCT) is an open source project for neural network model optimization under efficient, constrained hardware. This project provides researchers, developers, and engineers advanced quantization and compression tools for deploying state-of-the-art neural networks.
Python
295
star
8

nnabla-examples

Neural Network Libraries https://nnabla.org/ - Examples
Python
280
star
9

easyhttpcpp

A cross-platform HTTP client library with a focus on usability and speed
C++
152
star
10

sqvae

Pytorch implementation of stochastically quantized variational autoencoder (SQ-VAE)
Python
132
star
11

mapray-js

JavaScript library for Interactive high quality 3D globes and maps in the browser
TypeScript
118
star
12

nmos-cpp

An NMOS (Networked Media Open Specifications) Registry and Node in C++ (IS-04, IS-05)
C++
113
star
13

nnabla-rl

Deep reinforcement learning library built on top of Neural Network Libraries
Python
107
star
14

nnabla-ext-cuda

A CUDA Extension of Neural Network Libraries
Cuda
89
star
15

DiffRoll

PyTorch implementation of DiffRoll, a diffusion-based generative automatic music transcription (AMT) model
Jupyter Notebook
69
star
16

creativeai

CSS
63
star
17

meta-flutter

Yocto recipes for Flutter Engine and custom embedders
BitBake
61
star
18

FxNorm-automix

FxNorm-Automix - Implementation of automatic music mixing systems. We show how we can use wet music data and repurpose it to train a fully automatic mixing system
Python
51
star
19

flutter-elinux-plugins

Flutter plugins for embedded Linux (eLinux)
C++
47
star
20

appsync-client-go

AWS AppSync golang client library
Go
46
star
21

nnabla-nas

Neural Architecture Search for Neural Network Libraries
Python
44
star
22

nnabla-c-runtime

Neural Network Libraries https://nnabla.org/ - C Runtime
C
38
star
23

huis-ui-creator

JavaScript
38
star
24

NDJIR

NDJIR: Neural Direct and Joint Inverse Rendering for Geometry, Lights, and Materials of Real Object
Python
36
star
25

timbre-trap

Code for the paper "Timbre-Trap: A Low-Resource Framework for Instrument-Agnostic Music Transcription"
Python
34
star
26

pyIEOE

Python
31
star
27

nmos-js

An NMOS (Networked Media Open Specifications) Client in Javascript (IS-04, IS-05)
JavaScript
27
star
28

openocd-nuttx

Fork of OpenOCD with NuttX thread support.
C
24
star
29

CLIPSep

Python
23
star
30

pdaf-library

C
22
star
31

cdp-js

Libraries/SDK modules for multi-platform application development
TypeScript
20
star
32

polar-densification

Python
17
star
33

cordova-plugin-cdp-nativebridge

JavaScript
16
star
34

audio-visual-seld-dcase2023

Baseline method for audio-visual sound event localization and detection task of DCASE 2023 challenge
Python
16
star
35

generator-cordova-plugin-devbed

JavaScript
14
star
36

nnc-plugin

Plugins for Neural Network Console (https://dl.sony.com/).
Python
14
star
37

dolp-colorconstancy

Python
11
star
38

typescript-fsa-redux-middleware

Fluent syntax for defining typesafe Redux vanilla middlewares on top of typescript-fsa.
TypeScript
9
star
39

cdn-purge-control-php

Multi CDN purge control library for PHP
PHP
8
star
40

micro-notifier

Simplified Pusher Clone
Go
8
star
41

nnabla-browser

Visualization toolkit for Neural Network Libraries
TypeScript
8
star
42

isren

JavaScript
8
star
43

pixel-guided-diffusion

Fine-grained Image Editing by Pixel-wise Guidance Using Diffusion Models
Python
8
star
44

smarttennissensorsdk

The Smart Tennis Sensor plugs into the end of a tennis racket and records data about all the shots you make throughout a game or practice. With the SDK, you can develop apps for analyzing and presenting that data in real-time.
Java
8
star
45

cdp-cli

Command line tools for generating start point of multi-platform application development (Details: see cdp-js repository)
HTML
7
star
46

custom_layers

Python
7
star
47

mct_quantizers

Python
6
star
48

aibo-development-tutorial

6
star
49

smarttennissensormp4meta

Java
4
star
50

fp-diffusion

Jupyter Notebook
3
star
51

diffusion-timbre-transfer

Jupyter Notebook
3
star
52

node-win-usbdev

C++
3
star
53

evsCluster

Python scripts to process EVS (Event-based vision sensor) data
Python
3
star
54

Instruct3Dto3D-doc

Official documentation of Instruct 3D-to-3D
HTML
2
star
55

nnabla-js

TypeScript
1
star
56

nnabla-doc

1
star