/circuitbreaker

Simple example of a Circuit Breaker in Python

Primary LanguagePython

Python Circuit Breaker

This is a simple example library that implements a circuit breaker in Python.

The picture below shows the Circuit Breaker logic:

circuit breaker

Prerequisites

  • Python 3.6+

  • Install dependencies with pip:

    pip install -r requirements.yml

How to test

  • Run a simple flask http server which listens on http://localhost:5000:

    FLAS_APP=http_server.py flask run

    The http server has 3 endpoints:

  • Open another terminal and execute the script:

    python circuit_breaker.py

Test scenario:

The default circuit state is CLOSED, where requests flow freely. At first we send 5 requests to the success endpoint.

Then we send 7 requests to the faulty endpoint. When the error threshold is reached (5), we can verify by the messages in stdout that circuit state changes to OPEN after which point further requests are blocked. The circuit remains OPEN until some time has passed, 10 seconds in our case. After this time the circuit state is set to HALF_OPEN. A request is attemted, if it succeeds the circuit closes again and requests flow freely, otherwise the circuit remains OPEN until the next attempt after 10 seconds.

We add a sleep step which waits for 12 seconds and send requests to the random status endpoint. We can verify the circuit has closed (after 10 seconds).