module

Lucky::RateLimit

Adds in action level rate limiting. Limit the request rate of a specific action by including this module, then define the rate_limit method to configure. For convenience, you can also use the rate_limit macro.

class Reports::Index < ApiAction
  include Lucky::RateLimit
  rate_limit to: 5, within: 1.minute

  get "/reports"
    plain_text "ok"
  end
end

By default, the rate_limit_identifier uses the IP address. You can override this method to define a different strategy.

Instance methods

rate_limit

Defines the rate limit limiting to to requests within within time span.

def rate_limit : NamedTuple(to: Int32, within: Time::Span)
  {to: 5, within: 1.minute}
end
Source

Macros

rate_limit(to, within)

Convenience macro to define the required rate_limit method

rate_limit to: 5, within: 1.minute
Source