Class: Sidekiq::Throttled::ExpirableSet
- Inherits:
-
Object
- Object
- Sidekiq::Throttled::ExpirableSet
- Includes:
- Enumerable
- Defined in:
- lib/sidekiq/throttled/expirable_set.rb
Overview
Set of elements with expirations.
Instance Method Summary collapse
-
#add(element) ⇒ ExpirableSet
Self.
- #each {|Object| ... } ⇒ Object
-
#initialize(ttl) ⇒ ExpirableSet
constructor
A new instance of ExpirableSet.
Constructor Details
#initialize(ttl) ⇒ ExpirableSet
Returns a new instance of ExpirableSet.
24 25 26 27 28 29 |
# 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 |
Instance Method Details
#add(element) ⇒ ExpirableSet
Returns self.
33 34 35 36 37 38 39 40 41 42 43 |
# 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 |
#each {|Object| ... } ⇒ Object
46 47 48 49 50 51 52 53 54 55 56 |
# 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 |