Module: BetterAuth::APIKey::RateLimit
- Defined in:
- lib/better_auth/api_key/rate_limit.rb
Class Method Summary collapse
- .counts_requests?(record, config) ⇒ Boolean
- .next_request_count(record, now) ⇒ Object
- .try_again_in(record, config, now) ⇒ Object
Class Method Details
.counts_requests?(record, config) ⇒ Boolean
25 26 27 28 29 |
# File 'lib/better_auth/api_key/rate_limit.rb', line 25 def counts_requests?(record, config) return false if config[:rate_limit][:enabled] == false || record["rateLimitEnabled"] == false !record["rateLimitTimeWindow"].nil? && !record["rateLimitMax"].nil? end |
.next_request_count(record, now) ⇒ Object
31 32 33 34 35 36 37 38 |
# File 'lib/better_auth/api_key/rate_limit.rb', line 31 def next_request_count(record, now) last = Utils.normalize_time(record["lastRequest"]) window = record["rateLimitTimeWindow"].to_i return 1 unless last && window.positive? elapsed_ms = (now - last) * 1000 (elapsed_ms <= window) ? record["requestCount"].to_i + 1 : 1 end |
.try_again_in(record, config, now) ⇒ Object
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
# File 'lib/better_auth/api_key/rate_limit.rb', line 8 def try_again_in(record, config, now) return nil if config[:rate_limit][:enabled] == false || record["rateLimitEnabled"] == false window = record["rateLimitTimeWindow"] max = record["rateLimitMax"] return nil if window.nil? || max.nil? last = Utils.normalize_time(record["lastRequest"]) return nil unless last elapsed_ms = (now - last) * 1000 return nil if elapsed_ms > window.to_i return nil if record["requestCount"].to_i < max.to_i (window.to_i - elapsed_ms).ceil end |