ruby by @btsouth
Rails: request limiter
Online attempts send test input and timing for score validation. Only passage progress is kept for replay. You are playing as a guest: connect a profile within seven days to keep and publish a result, or it is removed.
YOUR TIME0.00s
BEAT @btsouth60.11s
PROGRESS0%
You
@btsouth
def rate_limiting(to:, within:, by:, with:)↵
limiter = Kredis.limiter "rate-limit:#{controller_path}:#{instance_exec(&by)}", limit: to, expires_in: within↵
↵
if limiter.exceeded?↵
ActiveSupport::Notifications.instrument("rate_limit.action_controller", request: request) do↵
instance_exec(&with)↵
end↵
else↵
limiter.poke↵
end↵
end
↵ means Enter, including blank lines. Tab stays in the test. Escape releases typing focus.
About this code
Builds a request counter for the controller and caller. When the limit is exceeded, it emits a notification and runs the configured response. Otherwise, it increments the counter.
Type the passage as written. Understanding it is optional; the explanation is here if you are curious.
David Heinemeier Hansson · Rails · MIT · Read the original source ↗