Skip to content

Instantly share code, notes, and snippets.

@pfinnn
Created August 10, 2020 08:36
Show Gist options
  • Save pfinnn/1ee87d8dad0f2247d61779021e7eb0fd to your computer and use it in GitHub Desktop.
Save pfinnn/1ee87d8dad0f2247d61779021e7eb0fd to your computer and use it in GitHub Desktop.
Go Circuit Breaker
package circuitbreaker
import "errors"
type ICircuitBreaker interface {
Close()
Open()
isOpen() bool
isClosed() bool
GetThreshold() int
GetErrorCount() int
ResetErrorCounter()
IncrementErrorCounter()
}
// private CircuitBreaker struct that can be initialized with the NewCircuitBreaker function call
// it will be closed on default
type CircuitBreaker struct {
closed bool
threshold int
counter int
ErrInvalidParameter error
ErrCircuitBreakerOpen error
}
func NewCircuitBreaker (errorThreshold int) (CircuitBreaker, error){
var (
// Constructor Errors
ErrInvalidParameter = errors.New("invalid parameters")
// Util Errors
ErrCircuitBreakerOpen = errors.New("circuit breaker open, denying requests to save resources")
)
// Throw error if parameters invalid
if errorThreshold < 1 {
panic(ErrInvalidParameter)
}
cb := CircuitBreaker {
// default values, all fields need to be initialized
true,
errorThreshold,
0,
ErrInvalidParameter, // error into its own struct?
ErrCircuitBreakerOpen,
}
return cb, nil
}
// GETTER
func (cb *CircuitBreaker) isOpen() bool{
return !cb.closed
}
func (cb *CircuitBreaker) IsClosed() bool{
return cb.closed
}
func (cb *CircuitBreaker) GetThreshold() int {
return cb.threshold
}
func (cb *CircuitBreaker) GetErrorCount() int {
return cb.counter
}
// SETTER
func (cb *CircuitBreaker) Close(){
cb.closed = true
}
func (cb *CircuitBreaker) Open() {
cb.closed = false
}
func (cb *CircuitBreaker) ResetErrorCounter(){
cb.counter = 0
}
func (cb *CircuitBreaker) IncrementErrorCounter() {
cb.counter += 1
}
// TODO: Add Timer that closes the CB after interval without error
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment