class Sidekiq::Throttled::ExpirableSet
@api internal
Set of elements with expirations.
@example
set = ExpirableSet.new(10.0) set.add("a") sleep(5) set.add("b") set.to_a # => ["a", "b"] sleep(5) set.to_a # => ["b"]
Public Class Methods
Source
# File lib/sidekiq/throttled/expirable_set.rb, line 24 def initialize(ttl) raise ArgumentError, "ttl must be positive Float" unless ttl.is_a?(Float) && ttl.positive? @elements = Concurrent::Map.new @ttl = ttl end
@param ttl [Float] expiration is seconds @raise [ArgumentError] if ‘ttl` is not positive Float
Public Instance Methods
Source
# File lib/sidekiq/throttled/expirable_set.rb, line 33 def add(element) # cleanup expired elements to avoid mem-leak horizon = now expired = @elements.each_pair.select { |(_, sunset)| expired?(sunset, horizon) } expired.each { |pair| @elements.delete_pair(*pair) } # add new element @elements[element] = now + @ttl self end
@param element [Object] @return [ExpirableSet] self
Source
# File lib/sidekiq/throttled/expirable_set.rb, line 46 def each return to_enum __method__ unless block_given? horizon = now @elements.each_pair do |element, sunset| yield element unless expired?(sunset, horizon) end self end
@yield [Object] Gives each live (not expired) element to the block
Private Instance Methods
Source
# File lib/sidekiq/throttled/expirable_set.rb, line 65 def expired?(sunset, horizon) sunset <= horizon end
Source
# File lib/sidekiq/throttled/expirable_set.rb, line 61 def now ::Process.clock_gettime(::Process::CLOCK_MONOTONIC) end
@return [Float]