Skip to content

Commit

Permalink
Switch to redis-prescription
Browse files Browse the repository at this point in the history
We're working on new, better sidekiq fetch strategye that will allow to
build reliable / throttled and the mix of two strategies easily.

Throttling logic most likley will be completely rewritten and extracted
out to a seprate, sidekiq-agnostic gem as well.
  • Loading branch information
ixti committed Feb 12, 2018
1 parent 2f54c20 commit ae7e948
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 142 deletions.
13 changes: 8 additions & 5 deletions lib/sidekiq/throttled/strategy/concurrency.rb
@@ -1,7 +1,8 @@
# frozen_string_literal: true

require "redis/prescription"

require "sidekiq/throttled/strategy/base"
require "sidekiq/throttled/strategy/script"

module Sidekiq
module Throttled
Expand All @@ -19,7 +20,7 @@ class Concurrency
# PUSH(@key, @jid)
# return 0
# end
SCRIPT = Script.read "#{__dir__}/concurrency.lua"
SCRIPT = Redis::Prescription.read "#{__dir__}/concurrency.lua"
private_constant :SCRIPT

# @param [#to_s] strategy_key
Expand All @@ -43,10 +44,12 @@ def dynamic?
def throttled?(jid, *job_args)
return false unless (job_limit = limit(job_args))

keys = [key(job_args)]
args = [jid.to_s, job_limit, @ttl, Time.now.to_f]
kwargs = {
:keys => [key(job_args)],
:argv => [jid.to_s, job_limit, @ttl, Time.now.to_f]
}

1 == SCRIPT.eval(keys, args)
Sidekiq.redis { |redis| 1 == SCRIPT.eval(redis, kwargs) }
end

# @return [Integer] Current count of jobs
Expand Down
95 changes: 0 additions & 95 deletions lib/sidekiq/throttled/strategy/script.rb

This file was deleted.

13 changes: 8 additions & 5 deletions lib/sidekiq/throttled/strategy/threshold.rb
@@ -1,7 +1,8 @@
# frozen_string_literal: true

require "redis/prescription"

require "sidekiq/throttled/strategy/base"
require "sidekiq/throttled/strategy/script"

module Sidekiq
module Throttled
Expand Down Expand Up @@ -29,7 +30,7 @@ class Threshold
#
# increase!
# return 0
SCRIPT = Script.read "#{__dir__}/threshold.lua"
SCRIPT = Redis::Prescription.read "#{__dir__}/threshold.lua"
private_constant :SCRIPT

# @param [#to_s] strategy_key
Expand Down Expand Up @@ -59,10 +60,12 @@ def dynamic?
def throttled?(*job_args)
return false unless (job_limit = limit(job_args))

keys = [key(job_args)]
args = [job_limit, period(job_args), Time.now.to_f]
kwargs = {
:keys => [key(job_args)],
:argv => [job_limit, period(job_args), Time.now.to_f]
}

1 == SCRIPT.eval(keys, args)
Sidekiq.redis { |redis| 1 == SCRIPT.eval(redis, kwargs) }
end

# @return [Integer] Current count of jobs
Expand Down
1 change: 1 addition & 0 deletions sidekiq-throttled.gemspec
Expand Up @@ -25,6 +25,7 @@ Gem::Specification.new do |spec|
spec.require_paths = ["lib"]

spec.add_runtime_dependency "sidekiq"
spec.add_runtime_dependency "redis-prescription"

spec.add_development_dependency "bundler", "~> 1.10"
end
37 changes: 0 additions & 37 deletions spec/sidekiq/throttled/strategy/script_spec.rb

This file was deleted.

0 comments on commit ae7e948

Please sign in to comment.