Class: Telegem::RateLimit
- Inherits:
-
Object
- Object
- Telegem::RateLimit
- Defined in:
- lib/core/rate_limit.rb
Instance Method Summary collapse
- #call(ctx, next_middleware) ⇒ Object
-
#initialize(**options) ⇒ RateLimit
constructor
A new instance of RateLimit.
Constructor Details
#initialize(**options) ⇒ RateLimit
Returns a new instance of RateLimit.
4 5 6 7 8 9 10 11 12 13 14 15 16 |
# File 'lib/core/rate_limit.rb', line 4 def initialize(**) @options = { global: { max: 30, per: 1 }, # 30 reqs/second globally user: { max: 5, per: 10 }, # 5 reqs/10 seconds per user chat: { max: 20, per: 60 } # 20 reqs/minute per chat }.merge() @counters = { global: Telegem::Session::MemoryStore.new, user: Telegem::Session::MemoryStore.new, chat: Telegem::Session::MemoryStore.new } end |
Instance Method Details
#call(ctx, next_middleware) ⇒ Object
18 19 20 21 22 23 24 25 26 27 28 |
# File 'lib/core/rate_limit.rb', line 18 def call(ctx, next_middleware) return next_middleware.call(ctx) unless should_rate_limit?(ctx) if limit_exceeded?(ctx) ctx.logger&.warn("Rate limit exceeded for #{ctx.from&.id}") return rate_limit_response(ctx) end increment_counters(ctx) next_middleware.call(ctx) end |