Class: Sidekiq::RateLimiter::Fetch

Inherits:
BasicFetch
  • Object
show all
Defined in:
lib/sidekiq-rate-limiter/fetch.rb

Instance Method Summary collapse

Instance Method Details

#limit(work) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/sidekiq-rate-limiter/fetch.rb', line 14

def limit(work)
  message = JSON.parse(work.message) rescue {}

  klass     = message['class']
  rate      = message['rate'] || message['throttle'] || {}
  limit     = rate['limit']   || rate['threshold']
  interval  = rate['period']  || rate['interval']
  name      = rate['name']    || DEFAULT_LIMIT_NAME

  return work unless !!(klass && limit && interval)

  options = {
    :limit    => limit,
    :interval => interval,
    :name     => name,
  }

  Sidekiq.redis do |conn|
    lim = Limit.new(conn, options)
    if lim.exceeded?(klass)
      conn.lpush("queue:#{work.queue_name}", work.message)
      nil
    else
      lim.add(klass)
      work
    end
  end
end

#retrieve_workObject



10
11
12
# File 'lib/sidekiq-rate-limiter/fetch.rb', line 10

def retrieve_work
  limit(super)
end