All Projects → dream11 → lua-circuit-breaker

dream11 / lua-circuit-breaker

Licence: MIT license
Circuit breaker pattern in Lua

Programming Languages

lua
6591 projects
shell
77523 projects
Dockerfile
14818 projects
Makefile
30231 projects

Projects that are alternatives of or similar to lua-circuit-breaker

kong-circuit-breaker
Kong plugin for wrapping all proxy calls with a circuit-breaker
Stars: ✭ 27 (-3.57%)
Mutual labels:  kong, circuit-breaker
course-spring-microservices
Code examples built for the purpose of video course: Microservices With Spring Boot And Spring Cloud
Stars: ✭ 74 (+164.29%)
Mutual labels:  circuit-breaker, resilience4j
Go Kong
DEPRECATED
Stars: ✭ 25 (-10.71%)
Mutual labels:  kong
kong-init
Declarative configuration tool for Kong
Stars: ✭ 38 (+35.71%)
Mutual labels:  kong
Kuma
🐻 The Universal Service Mesh. CNCF Sandbox Project.
Stars: ✭ 2,516 (+8885.71%)
Mutual labels:  kong
Kongpose
Kong and Konga (admin webapp) development setup on docker-compose
Stars: ✭ 52 (+85.71%)
Mutual labels:  kong
Kong Dist Kubernetes
Kubernetes managed Kong cluster
Stars: ✭ 250 (+792.86%)
Mutual labels:  kong
Kongdash
An elegant desktop client for Kong Admin API
Stars: ✭ 449 (+1503.57%)
Mutual labels:  kong
meshery-kuma
Meshery Adapter for Kuma
Stars: ✭ 35 (+25%)
Mutual labels:  kong
Kong Dashboard
Dashboard for managing Kong gateway
Stars: ✭ 2,142 (+7550%)
Mutual labels:  kong
kongsul
Kong Api Gateway with Consul Service Discovery (MicroService)
Stars: ✭ 35 (+25%)
Mutual labels:  kong
Wicked.haufe.io
An API Management system based on Mashape Kong
Stars: ✭ 110 (+292.86%)
Mutual labels:  kong
Kong Plugin Response Cache
A Kong plugin that will cache responses in redis
Stars: ✭ 66 (+135.71%)
Mutual labels:  kong
kong-plugin-url-rewrite
Kong API Gateway plugin for url-rewrite purposes
Stars: ✭ 43 (+53.57%)
Mutual labels:  kong
Kong
🦍 The Cloud-Native API Gateway
Stars: ✭ 30,838 (+110035.71%)
Mutual labels:  kong
kongverge
A desired state configuration tool for Kong
Stars: ✭ 23 (-17.86%)
Mutual labels:  kong
Quarkus Microservices Poc
Very simplified shop sales system made in a microservices architecture using quarkus
Stars: ✭ 16 (-42.86%)
Mutual labels:  kong
Kubernetes Ingress Controller
🦍 Kong for Kubernetes: the official Ingress Controller for Kubernetes.
Stars: ✭ 1,347 (+4710.71%)
Mutual labels:  kong
Deck
decK: Configuration management and drift detection for Kong
Stars: ✭ 211 (+653.57%)
Mutual labels:  kong
Uragano
Uragano, A simple, high performance RPC library. Support load balancing, circuit breaker, fallback, caching, intercepting.
Stars: ✭ 28 (+0%)
Mutual labels:  circuit-breaker

lua-circuit-breaker

Continuous Integration Code Coverage License

Overview

lua-circuit-breaker provides circuit-breaker functionality like resilience4j i.e. for Java.
Any IO function/method that can fail can be wrapped around lua-circuit-breaker and can be made to fail fast, leading to improved resiliency and fault tolerance.

How does it work?

  1. The library creates a CB(circuit breaker) object for a function.
  2. Before making the function call, we call CB._before(). This will return an error if the circuit breaker is in open state otherwise will increment the counter of total_requests by 1.
  3. After the function call ends, we call CB._after(CB._generation, ok). If ok is true, the success counter is incremented by 1. Otherwise, the failure counter gets incremented by 1.
  4. CB object transitions into three states: closed, open, and half-open based on the settings defined by the user.

Installation

luarocks

luarocks install lua-circuit-breaker

source

Clone this repo and run:

luarocks make

Sample Usage

--Import Circuit breaker factory.
local circuit_breaker_lib = require "lua-circuit-breaker.factory"

--Create a new instance of the circuit breaker factory. Always set version = 0. This is used for flushing the circuit breakers when the configuration is changed.
local circuit_breakers = circuit_breaker_lib:new()

-- Get a circuit breaker instance from factory. Returns a new instance only if not already created.
local settings = {
    version = 1,
    window_time = 10,
    min_calls_in_window= 20,
    failure_percent_threshold= 51,
    wait_duration_in_open_state= 15,
    wait_duration_in_half_open_state= 120,
    half_open_max_calls_in_window= 10,
    half_open_min_calls_in_window= 5,
    notify = function(name, state)
        print(string.format("Breaker [ %s ] state changed to [ %s ]", name, state))
    end,
}
local cb, err = circuit_breakers:get_circuit_breaker(
    "io_call_x", -- Name of circuit breaker. This should be unique.
    "io_calls", -- Used to group certain CB objects into one.
    settings,
)

-- Check state of cb. This function returns an error if the state is open or half_open_max_calls_in_window is breached.
local _, err_cb = cb:_before()
if err_cb then
    return false, "Circuit breaker open error"
end
local generation = cb._generation

-- Call IO method for which circuit breaking is required.
local res, err_http = makeIOCall()

-- Update the state of the cb based on successful / failure response.
local ok = res and res.status and res.status < 500
cb:_after(generation, ok) -- generation is used to update the counter in the correct time bucket.

Openresty Sample Usage

There is a sample nginx openresty application (using docker for ease of usage) on ngx_lua_sample

Parameters

Key Default Type Required Description
name NA string true Name of circuit breaker, this should be unique
group "default_group" string false Group to which the CB object will belong
settings.version NA number true Maintains version of settings object, changing this will create new CB and flush older CB
settings.window_time 10 number true Window size in seconds
settings.min_calls_in_window 20 number true Minimum number of calls to be present in the window to start calculation
settings.failure_percent_threshold 51 number true % of requests that should fail to open the circuit
settings.wait_duration_in_open_state 15 number true Duration(sec) to wait before automatically transitioning from open to half-open state
settings.wait_duration_in_half_open_state 120 number true Duration(sec) to wait in half-open state before automatically transitioning to closed state
settings.half_open_min_calls_in_window 5 number true Minimum number of calls to be present in the half open state to start calculation
settings.half_open_max_calls_in_window 10 number true Maximum calls to allow in half open state
settings.half_open_to_open NA function false Overrides transition from half-open to open state
settings.half_open_to_close NA function false Overrides transition from half-open to closed state
settings.closed_to_open NA function false Overrides transtition from closed to open state
settings.notify NA function false Overrides with a custom logger function

Available Methods

  1. new() : create a new circuit breaker factory
  2. get_circuit_breaker(name, group, settings) : create a new CB object
  3. check_group(group) : check if this group is present
  4. remove_breakers_by_group(group) : remove all CB objects in a group
  5. remove_circuit_breaker(name, group) : remove a particular CB inside a group. if group is not passed, "default_group" is assumed.

Inspired by

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].