Class: Sidekiq::Throttled::Strategy::Concurrency

Inherits:
Object
  • Object
show all
Defined in:
lib/sidekiq/throttled/strategy/concurrency.rb

Overview

Concurrency throttling strategy

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(base_key, opts) ⇒ Concurrency

Returns a new instance of Concurrency.

Parameters:

  • base_key (#to_s)
  • opts (Hash)

Options Hash (opts):

  • :limit (#to_i)

    Amount of allwoed concurrent jobs processors running for given key

  • :ttl (#to_i) — default: 15.minutes

    Concurrency lock TTL



28
29
30
31
32
33
# File 'lib/sidekiq/throttled/strategy/concurrency.rb', line 28

def initialize(base_key, opts)
  @key   = "#{base_key}:concurrency".freeze
  @keys  = [@key]
  @limit = opts.fetch(:limit).to_i
  @ttl   = opts.fetch(:ttl, 900).to_i
end

Instance Attribute Details

#limitObject (readonly)

Returns the value of attribute limit.



21
22
23
# File 'lib/sidekiq/throttled/strategy/concurrency.rb', line 21

def limit
  @limit
end

Instance Method Details

#countInteger

Returns Current count of jobs.

Returns:

  • (Integer)

    Current count of jobs



41
42
43
# File 'lib/sidekiq/throttled/strategy/concurrency.rb', line 41

def count
  Sidekiq.redis { |conn| conn.scard(@key) }.to_i
end

#finalize!(jid) ⇒ void

This method returns an undefined value.

Remove jid from the pool of jobs in progress



53
54
55
# File 'lib/sidekiq/throttled/strategy/concurrency.rb', line 53

def finalize!(jid)
  Sidekiq.redis { |conn| conn.srem(@key, jid.to_s) }
end

#reset!void

This method returns an undefined value.

Resets count of jobs



47
48
49
# File 'lib/sidekiq/throttled/strategy/concurrency.rb', line 47

def reset!
  Sidekiq.redis { |conn| conn.del(@key) }.to_i
end

#throttled?(jid) ⇒ Boolean

Returns whenever job is throttled or not.

Returns:

  • (Boolean)

    whenever job is throttled or not



36
37
38
# File 'lib/sidekiq/throttled/strategy/concurrency.rb', line 36

def throttled?(jid)
  1 == SCRIPT.eval(@keys, [@limit, @ttl, jid.to_s])
end