AshRateLimiter
Copy MarkdownAn extension for Ash.Resource which adds the ability to rate limit access to actions.
rate_limit
Configure rate limiting for actions.
Backend
This library uses a pluggable backend for rate limiting. Any module implementing the
AshRateLimiter.Backend behaviour can be used. See AshRateLimiter.Backend for more information.
Keys
The backend uses a "key" to identify which bucket to allocate an event against. You can use this to tune the rate limit for specific users or events.
You can provide either a statically configured string key, or a function of arity one or two, which when given a query/changeset and optional context object can generate a key.
The default is AshRateLimiter.key_for_action/2. See it's docs for more information.
Nested DSLs
Examples
rate_limit do
backend MyApp.RateLimiter
action :create, limit: 10, per: :timer.minutes(5)
end
Options
| Name | Type | Default | Docs |
|---|---|---|---|
backend | module | The rate limiting backend module |
rate_limit.action
action actionConfigure rate limiting for a single action.
It does this by adding a global change or preparation to the resource with the provided configuration. For more advanced configuration you can add the change/preparation directly to your action using AshRateLimiter.BuiltinChanges.rate_limit/1 or AshRateLimiter.BuiltinPreparations.rate_limit/1.
Arguments
| Name | Type | Default | Docs |
|---|---|---|---|
action | atom | The name of the action to limit |
Options
| Name | Type | Default | Docs |
|---|---|---|---|
limit | pos_integer | The maximum number of events allowed within the given period | |
per | pos_integer | The time period (in milliseconds) for in which events are counted | |
key | String.t | (any -> any) | (any, any -> any) | &AshRateLimiter.key_for_action/2 | The key used to identify the event. See above. |
description | String.t | A description of the rate limit | |
on | :before_action | :before_transaction | :before_action | The lifecycle hook to use for rate limiting. :before_action (default) runs inside the transaction; :before_transaction runs outside the transaction, after validations. |
Introspection
Target: AshRateLimiter