Giter Club home page Giter Club logo

short_circuit's Introduction

ShortCircuit

A simple Python library that provides the CircuitBreaker pattern as a function decorator.

Context

A simple Python implementation of the CircuitBreaker pattern, as popularized by Michael T. Nygard in his book "Release it!: Design and Deploy Production-Ready Software" (Pragmatic Bookshelf, 2007)

The circuit breaker pattern is described as a way to protect a system from excessive requests that could cause it to fail. The idea is to use a "circuit breaker" to monitor the system's performance and trip (i.e., open) the circuit if the system becomes overloaded or unresponsive. This prevents further requests from reaching the system and allows it to recover.

The circuit breaker pattern has become a popular way to handle failures in distributed systems, and it is used in many different contexts, including web services, microservices, and distributed databases. It is particularly useful in systems that rely on remote resources or services, as it helps to prevent failures from propagating and causing widespread outages.

Features

This implementation aims to provide the following:

  • Closed state: in this state, the circuit breaker will allow through unlimited requests as long as they continue to succeed.
  • Open state: in this state, the circuit breaker has "tripped", not allowing any requests to be made until a preconfigured timeout has expired
  • Half-open state: in this state, the circuit breaker will allow a new request, but will immediately "trip" if that request fails. If the request succeeds, it will 'close' the circuit again, allowing requests to proceed. The idea behind this state is to carefully allow requests to pass through again without overloading the underlying system and causing more cascading failures. This 'reset' functionality is configured by a timeout variable.

Usage

Current interface allows for the use of a single circuit breaker as a function decorator

cb = CircuitBreaker(max_failures=3, reset_timeout=3)

@cb
def foo():
    # do stuff

The function decorator pattern allows a user to decorate multiple functions with a single breaker if so desired, depending on which underlying services are being called.

TODO

  • Allow to register circuit breakers, as well as provide an interface to interact with the various breakers (get stats, manually manipulate breakers if needed)
  • Allow for a singleton breaker to be used across an entire client (maybe)

short_circuit's People

Contributors

maxlvl avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.