circuitbreaker

package
v0.20.0 Latest Latest
Warning

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

Go to latest
Published: Apr 26, 2024 License: Apache-2.0 Imports: 3 Imported by: 0

Documentation

Overview

CircuitBreaker following design: https://martinfowler.com/bliki/CircuitBreaker.html

Example (CircuitBreaker)
getGoogle := func(hc *http.Client, circuitBreaker Interface) {
	circuitBreaker.Do(
		func() error {
			resp, err := hc.Get("https://google.com")
			if err != nil {
				return err
			}
			resp.Write(os.Stdout)
			return nil
		},
		func(err error) {
			fmt.Println("google is not available. Error:", err)
		},
		func() {
			fmt.Println("google is not available. Circuit breaker is opened")
		},
	)
}

circuitBreaker := NewSyncCircuitBreaker(1, time.Second)
client := &http.Client{Timeout: time.Nanosecond}

// this call get will timeout
getGoogle(client, circuitBreaker)
// this call will be denied by circuit breaker because of it's opened
getGoogle(client, circuitBreaker)
Output:

Index

Examples

Constants

This section is empty.

Variables

View Source
var NeverOpen neverOpen

A circuit breaker would never be opened

Functions

This section is empty.

Types

type Interface added in v0.3.0

type Interface interface {
	// Do task
	//  task: task to be done
	//  onError: handle errors returned from task
	//  onOpen: be called if state == open
	Do(task func() error, onError func(error), onOpen func())
}

A circuit breaker has 3 states:

open: no task will be executed

closed: task will be executed

halfOpen: task will be executed, if failed, back to state of open, if success, transfer to state of close

type SyncCircuitBreaker

type SyncCircuitBreaker struct {
	// contains filtered or unexported fields
}

func NewSyncCircuitBreaker

func NewSyncCircuitBreaker(failureThreshold int, resetTimeout time.Duration) *SyncCircuitBreaker

failureThreshold: once the failures reach this threshold, the circuit breaker will be opened

resetTimeout: if circuit breaker is in state of open, after this timeout, the circuit breaker will be in half-open state

func (*SyncCircuitBreaker) Do

func (s *SyncCircuitBreaker) Do(task func() error, onError func(error), onOpen func())

Jump to

Keyboard shortcuts

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