gocircuit

package module
v1.0.2 Latest Latest
Warning

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

Go to latest
Published: Jan 9, 2024 License: MIT Imports: 2 Imported by: 0

README

Go Circuit Breaker

Diagram

Go Circuit Breaker is a robust and flexible Go package that provides a Circuit Breaker implementation for managing unreliable or slow dependencies in your Go applications. It helps improve the resilience and reliability of your services by preventing cascading failures and allowing graceful handling of failing components.

Table of Contents

Introduction

A Circuit Breaker is a design pattern widely used in distributed systems to handle failures and prevent them from propagating through the system. It acts as a safety net that can temporarily block requests to a failing service, allowing it to recover before retrying. Go Circuit Breaker simplifies the implementation of this pattern in your Go applications.

Why Use Circuit Breaker

Here are some compelling reasons to consider using Go Circuit Breaker in your projects:

  • Fault Tolerance: Circuit Breakers enhance the fault tolerance of your application by intelligently handling unreliable dependencies.

  • Failover Strategy: They enable graceful failover strategies, such as using cached data or alternative services when a component is unavailable.

  • Efficiency: Circuit Breakers help reduce the load on your application and external services by avoiding repeated and futile requests.

  • Fallback Handling: You can define fallback mechanisms to provide a seamless user experience even when primary services are down.

Features

  • Three states: Closed, Half-Open, and Open.
  • Configurable thresholds for consecutive failures and successes.
  • Auto-close feature to automatically reset the Circuit Breaker.
  • Fallback mechanism for graceful failure handling.
  • Open duration to control the time a Circuit Breaker stays open.
  • Well-documented and tested for reliability.

Installation

To install Go Circuit Breaker, use the go get command:

go get github.com/Furkan-Gulsen/gocircuit

Usage

Getting Started

Using Go Circuit Breaker is straightforward. Here's a basic example of how to use it:

import (
    "github.com/Furkan-Gulsen/gocircuit"
)

func main() {
    // Create a Circuit Breaker configuration.
    config := gocircuit.CircuitBreakerConfig{
        FailureThreshold:   5,
        ResetTimeout:       30 * time.Second,
        SuccessThreshold:   3,
        AutoCloseThreshold: 2,
        AutoCloseDuration:  5 * time.Minute,
        OpenDuration:       1 * time.Minute,
    }

    // Create a Circuit Breaker instance.
    cb := gocircuit.NewCircuitBreaker(config, fallbackFunc)

    // Execute an action using the Circuit Breaker.
    err := cb.Execute(myRequestFunc)

    if err != nil {
        // Handle the error or fallback gracefully.
    }
}

For a more detailed usage guide, please refer to the Usage section in the README.

Configuration

You can configure the behavior of the Circuit Breaker by adjusting the following parameters in the CircuitBreakerConfig structure:

  • FailureThreshold: The threshold for consecutive failures required to trip the circuit.
  • ResetTimeout: The duration after which the circuit transitions to the half-open state.
  • SuccessThreshold: The threshold for consecutive successes required to reset the circuit.
  • AutoCloseThreshold: The threshold for consecutive successful executions required to auto-close the circuit.
  • AutoCloseDuration: The duration after which the circuit automatically closes if the threshold is not met.
  • OpenDuration: The duration for which the circuit remains open before transitioning to half-open.
Fallback Mechanism

Go Circuit Breaker allows you to define a fallback mechanism by providing a fallback function. This function will be executed when a request fails. You can use this mechanism to provide alternative functionality or data when a service is unavailable.

func fallbackFunc() error {
    // Implement your fallback logic here.
    return nil
}
Auto-Close Feature

The auto-close feature automatically resets

the Circuit Breaker to the closed state if the specified threshold for consecutive successful executions is met. This feature helps reduce downtime and ensures that the circuit is retested for reliability.

License

This project is licensed under the MIT License - see the LICENSE file for details.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type CircuitBreaker

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

CircuitBreaker represents a Circuit Breaker.

func NewCircuitBreaker

func NewCircuitBreaker(config CircuitBreakerConfig, fallbackFunc func() error) *CircuitBreaker

NewCircuitBreaker creates a new Circuit Breaker with the given configuration.

func (*CircuitBreaker) Execute

func (cb *CircuitBreaker) Execute(action func() error) error

Execute attempts to execute an action using the Circuit Breaker.

func (*CircuitBreaker) Reset

func (cb *CircuitBreaker) Reset()

reset resets the Circuit Breaker to the closed state.

func (*CircuitBreaker) State

func (cb *CircuitBreaker) State() CircuitState

State returns the current state of the Circuit Breaker.

type CircuitBreakerConfig

type CircuitBreakerConfig struct {
	FailureThreshold   int           // The threshold for consecutive failures required to trip the circuit
	ResetTimeout       time.Duration // The duration after which the circuit transitions to half-open
	SuccessThreshold   int           // The threshold for consecutive successes required to reset the circuit
	AutoCloseThreshold int           // The threshold for consecutive successful executions required to auto-close the circuit
	AutoCloseDuration  time.Duration // The duration after which the circuit automatically closes if threshold not met
	OpenDuration       time.Duration // The duration for which the circuit remains open before transitioning to half-open
}

CircuitBreakerConfig holds the configuration options for the Circuit Breaker.

type CircuitState

type CircuitState int

CircuitState represents the state of the Circuit Breaker.

const (
	StateClosed CircuitState = iota
	StateHalfOpen
	StateOpen
)

Jump to

Keyboard shortcuts

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