circuitbreaker

package module
v0.0.0-...-2ed22d6 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jun 20, 2018 License: MIT Imports: 3 Imported by: 0

README

circuitbreaker

Build Status Go Report Card

This is a circuitbreaker module which is easy to use when you build your microservice.

How to use it

// func NewCirucuitBreaker(timeWin time.Duration, failCnt int, failPercent int) *Circuits

// create an instance of circuitbreaker
cbs := NewCirucuitBreaker(time.Second, 150, 20)
// register a command to a circuitbreaker
suc := cbs.RegisterCommandAsDefault(testcmd)

// report a result (true or false) to a circuit with the command-name
cbs.Report(testcmd, false)
cbs.Report(testcmd, true)

// to check if current request could be allowed
execAllow := cbs.AllowExec(testcmd)

Testing

$ go test
[DEBUG] To run test for command: testcmd
[DEBUG] [spec]total: 1000, allow: 1000
[DEBUG] Closed --> Open
[DEBUG] Open --> HalfOpen
[DEBUG] HalfOpen --> Closed
[DEBUG] Closed --> Open
[DEBUG] [spec]total: 1000, allow: 809
[DEBUG] Open --> HalfOpen
[DEBUG] HalfOpen --> Closed
[DEBUG] Closed --> Open
[DEBUG] Open --> HalfOpen
[DEBUG] HalfOpen --> Closed
[DEBUG] Closed --> Open
[DEBUG] Open --> HalfOpen
[DEBUG] HalfOpen --> Closed
[DEBUG] Closed --> Open
[DEBUG] Open --> HalfOpen
[DEBUG] HalfOpen --> Closed
[DEBUG] Closed --> Open
[DEBUG] Open --> HalfOpen
[DEBUG] HalfOpen --> Closed
[DEBUG] Closed --> Open
[DEBUG] Open --> HalfOpen
[DEBUG] HalfOpen --> Closed
[DEBUG] Closed --> Open
[DEBUG] [spec]total: 1000, allow: 24
[DEBUG] Closed --> Open
[DEBUG] Open --> HalfOpen
[DEBUG] Open --> HalfOpen
[DEBUG] HalfOpen --> Closed
[DEBUG] Closed --> Open
[DEBUG] Open --> HalfOpen
[DEBUG] HalfOpen --> Closed
[DEBUG] Closed --> Open
[DEBUG] Open --> HalfOpen
[DEBUG] HalfOpen --> Closed
[DEBUG] [rand]total: 1000, allow: 517
PASS
ok      github.com/moxiaomomo/circuitbreaker    24.737s

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Circuit

type Circuit struct {
	// TimeWindow time-window
	TimeWindow time.Duration
	// ThredsholdPercent fail-count's percent (1~100)
	ThredsholdPercent int
	// ThredsholdCount fail-count in s time-window
	ThredsholdCount int
	// contains filtered or unexported fields
}

Circuit Circuit

func (*Circuit) AllowExec

func (c *Circuit) AllowExec() bool

AllowExec is allow to execute command

func (*Circuit) Report

func (c *Circuit) Report(isSuc bool) bool

Report update a circuit state

type Circuits

type Circuits struct {
	Instances map[string]*Circuit
	// DefaultTimeWindow time-window
	DefaultTimeWindow time.Duration
	// DefaultThredsholdPercent fail-count's percent (1~100)
	DefaultThredsholdPercent int
	// DefaultThredsholdCount fail-count in s time-window
	DefaultThredsholdCount int
	// contains filtered or unexported fields
}

Circuits Circuits

func NewCirucuitBreaker

func NewCirucuitBreaker(timeWin time.Duration, failCnt int, failPercent int) *Circuits

NewCirucuitBreaker NewCirucuitBreaker

func (*Circuits) AllowExec

func (cb *Circuits) AllowExec(cmd string) bool

AllowExec is allow to execute command

func (*Circuits) RegisterCommand

func (cb *Circuits) RegisterCommand(cmd string, timeWin time.Duration, failCnt int, failPercent int) bool

RegisterCommand Register command using given settings

func (*Circuits) RegisterCommandAsDefault

func (cb *Circuits) RegisterCommandAsDefault(cmd string) bool

RegisterCommandAsDefault Register command using default settings

func (*Circuits) Report

func (cb *Circuits) Report(cmd string, isSuc bool) bool

Report report command status after execute (suc or fail); returns report result, suc or not

type StatusEnum

type StatusEnum int

StatusEnum circuitbreaker status enum

const (

	// StatusClosed StatusClosed
	StatusClosed StatusEnum
	// StatusOpen StatusOpen
	StatusOpen
	// StatusHalfOpen StatusHalfOpen
	StatusHalfOpen
)

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL