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(strategy_key, opts) ⇒ Concurrency

Returns a new instance of Concurrency.

Parameters:

  • strategy_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 in seconds

  • :key_suffix (Object)

    Proc for dynamic key suffix.



31
32
33
34
35
36
# File 'lib/sidekiq/throttled/strategy/concurrency.rb', line 31

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

Instance Attribute Details

#limitObject (readonly)

Returns the value of attribute limit.



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

def limit
  @limit
end

Instance Method Details

#count(*job_args) ⇒ Integer

Returns Current count of jobs.

Returns:

  • (Integer)

    Current count of jobs



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

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

#dynamic_keys?Boolean

Returns:

  • (Boolean)


38
39
40
# File 'lib/sidekiq/throttled/strategy/concurrency.rb', line 38

def dynamic_keys?
  @key_suffix
end

#finalize!(jid, *job_args) ⇒ void

This method returns an undefined value.

Remove jid from the pool of jobs in progress



60
61
62
# File 'lib/sidekiq/throttled/strategy/concurrency.rb', line 60

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

#reset!(*job_args) ⇒ void

This method returns an undefined value.

Resets count of jobs



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

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

#throttled?(jid, *job_args) ⇒ Boolean

Returns whenever job is throttled or not.

Returns:

  • (Boolean)

    whenever job is throttled or not



43
44
45
# File 'lib/sidekiq/throttled/strategy/concurrency.rb', line 43

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