Limen.Policy.Runtime (limen v0.2.1)

Copy Markdown View Source

Functions compiled policies call on the request path.

Summary

Functions

Checks the hard limits of policy, returning the first exceeded.

Describes a limit for humans.

Maps a score to a proof-of-work difficulty in leading zero bits.

The value of parameter name in the instance's :params, or default.

The sliding-window rate of dimension over window, as tracked for the current policy by track/2.

Whether the HTTP shape signal raised flag.

Counts the request in every rate window policy refers to, and stores the resulting rates in the context.

A score weight from parameter name: its configured value when it is an integer, else default.

Functions

check_limits(policy, ctx)

@spec check_limits(module(), Limen.Context.t()) ::
  :ok | {:exceeded, Limen.Decision.Match.t(), pos_integer()}

Checks the hard limits of policy, returning the first exceeded.

describe_limit(map)

@spec describe_limit(map()) :: String.t()

Describes a limit for humans.

difficulty_for(score, opts \\ [])

@spec difficulty_for(integer(), keyword()) :: pos_integer()

Maps a score to a proof-of-work difficulty in leading zero bits.

Scores up to :from get :base bits; every :step points above that add one bit, up to :max. Each extra bit doubles the expected work.

iex> Limen.Policy.Runtime.difficulty_for(40)
16
iex> Limen.Policy.Runtime.difficulty_for(85)
18
iex> Limen.Policy.Runtime.difficulty_for(1_000)
22

param(context, name, default)

@spec param(Limen.Context.t(), atom(), term()) :: term()

The value of parameter name in the instance's :params, or default.

rate(context, dimension, window)

@spec rate(Limen.Context.t(), atom(), atom()) :: non_neg_integer()

The sliding-window rate of dimension over window, as tracked for the current policy by track/2.

shape_flag?(context, flag)

@spec shape_flag?(Limen.Context.t(), atom()) :: boolean()

Whether the HTTP shape signal raised flag.

track(policy, ctx)

@spec track(module(), Limen.Context.t()) :: Limen.Context.t()

Counts the request in every rate window policy refers to, and stores the resulting rates in the context.

Runs for every request the policy covers, including pass holders, so rates reflect all traffic.

weight(ctx, name, default)

@spec weight(Limen.Context.t(), atom(), integer()) :: integer()

A score weight from parameter name: its configured value when it is an integer, else default.