seki (seki v0.4.3)
View SourceMain API for Seki resilience primitives.
Provides rate limiting with four algorithms (token bucket, sliding window, GCRA,
leaky bucket), circuit breaking with configurable failure/slow-call thresholds,
and a combined execute/3 that checks rate limits before calling through a breaker.
Erlang
seki:new_limiter(my_api, #{algorithm => sliding_window, limit => 100, window => 60000}).
seki:check(my_api, UserId).Elixir
:seki.new_limiter(:my_api, %{algorithm: :sliding_window, limit: 100, window: 60_000})
:seki.check(:my_api, user_id)
Summary
Functions
Execute a function through a circuit breaker.
Execute a function through a circuit breaker with options.
Check if a request is allowed for the given key (cost = 1).
Check if a request with a custom cost is allowed for the given key.
Stop and remove a supervised adaptive concurrency limiter.
Delete a circuit breaker and stop its process.
Stop and remove a supervised bulkhead.
Stop and remove a supervised health checker.
Delete a rate limiter and clean up its backend state.
Stop and remove a supervised load shedder.
Check rate limit then execute through circuit breaker. Returns {error, {rate_limited, Info}} if denied.
Non-destructive check — read current state without consuming tokens.
Non-destructive check with a custom cost.
Start a supervised adaptive concurrency limiter.
Create a new circuit breaker with the given options.
Start a supervised bulkhead.
Start a supervised health checker.
Create a new rate limiter with the given algorithm and options.
Start a supervised load shedder.
Reset the rate limit state for a key.
Reset a circuit breaker to closed state.
Get the current state of a circuit breaker.
Types
-type algorithm() :: token_bucket | sliding_window | gcra | leaky_bucket.
-type breaker_name() :: atom().
-type breaker_opts() :: #{window_type => count | time, window_size => pos_integer(), failure_threshold => 1..100, slow_call_threshold => 1..100, slow_call_duration => pos_integer(), wait_duration => pos_integer(), half_open_requests => pos_integer(), error_classifier => fun((term()) -> boolean())}.
-type check_result() :: {allow, #{remaining := non_neg_integer(), reset := non_neg_integer()}} | {deny, #{retry_after := non_neg_integer()}}.
-type key() :: term().
-type limiter_name() :: atom().
-type limiter_opts() :: #{algorithm := algorithm(), limit := pos_integer(), window := pos_integer(), burst => pos_integer(), backend => module(), backend_opts => map()}.
Functions
-spec call(breaker_name(), fun(() -> term())) -> call_result().
Execute a function through a circuit breaker.
-spec call(breaker_name(), fun(() -> term()), map()) -> call_result().
Execute a function through a circuit breaker with options.
-spec check(limiter_name(), key()) -> check_result().
Check if a request is allowed for the given key (cost = 1).
-spec check(limiter_name(), key(), pos_integer()) -> check_result().
Check if a request with a custom cost is allowed for the given key.
Stop and remove a supervised adaptive concurrency limiter.
-spec delete_breaker(breaker_name()) -> ok | {error, term()}.
Delete a circuit breaker and stop its process.
Stop and remove a supervised bulkhead.
Stop and remove a supervised health checker.
-spec delete_limiter(limiter_name()) -> ok.
Delete a rate limiter and clean up its backend state.
Stop and remove a supervised load shedder.
-spec execute(breaker_name(), limiter_name(), fun(() -> term())) -> call_result() | {error, rate_limited}.
Check rate limit then execute through circuit breaker. Returns {error, {rate_limited, Info}} if denied.
-spec inspect(limiter_name(), key()) -> check_result().
Non-destructive check — read current state without consuming tokens.
-spec inspect(limiter_name(), key(), pos_integer()) -> check_result().
Non-destructive check with a custom cost.
Start a supervised adaptive concurrency limiter.
-spec new_breaker(breaker_name(), breaker_opts()) -> {ok, pid()} | {error, term()}.
Create a new circuit breaker with the given options.
Start a supervised bulkhead.
Start a supervised health checker.
-spec new_limiter(limiter_name(), limiter_opts()) -> ok | {error, term()}.
Create a new rate limiter with the given algorithm and options.
Start a supervised load shedder.
-spec reset(limiter_name(), key()) -> ok.
Reset the rate limit state for a key.
-spec reset_breaker(breaker_name()) -> ok.
Reset a circuit breaker to closed state.
-spec state(breaker_name()) -> closed | open | half_open.
Get the current state of a circuit breaker.