Sha256: d8a0bcadcf29d0acb325bb9cc70e4764385790d0dd7b7940e09dd126f51cc0ac
Contents?: true
Size: 1.01 KB
Versions: 38
Compression:
Stored size: 1.01 KB
Contents
# frozen_string_literal: true module Prefab # A key-based rate limiter that considers a key to be fresh if it has been # seen within the last `duration` seconds. # # This is used to rate limit the number of times we send a given context # to the server. # # Because expected usage is to immediately `set` on a `fresh?` miss, we do # not prune the data structure on `fresh?` calls. Instead, we manually invoke # `prune` periodically from the cache consumer. class RateLimitCache attr_reader :data def initialize(duration) @data = Concurrent::Map.new @duration = duration end def fresh?(key) timestamp = @data[key] return false unless timestamp return false if Time.now.utc.to_i - timestamp > @duration true end def set(key) @data[key] = Time.now.utc.to_i end def prune now = Time.now.utc.to_i @data.each_pair do |key, (timestamp, _)| @data.delete(key) if now - timestamp > @duration end end end end
Version data entries
38 entries across 38 versions & 1 rubygems