module Lucky::RateLimit

Overview

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 configue. For convienence, 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.

Defined in:

lucky/rate_limit.cr

Macro Summary

Instance Method Summary

Macro Detail

macro rate_limit(to, within) #

Convience macro to define the required #rate_limit method

rate_limit to: 5, within: 1.minute

[View source]

Instance Method Detail

abstract def rate_limit : NamedTuple(to: Int32, within: Time::Span) #

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

[View source]