sidekiq-throttled 0.11.0 → 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/{LICENSE.md → LICENSE} +3 -1
- data/README.adoc +296 -0
- data/lib/sidekiq/throttled/config.rb +44 -0
- data/lib/sidekiq/throttled/cooldown.rb +55 -0
- data/lib/sidekiq/throttled/expirable_set.rb +70 -0
- data/lib/sidekiq/throttled/job.rb +128 -0
- data/lib/sidekiq/throttled/middleware.rb +10 -3
- data/lib/sidekiq/throttled/patches/basic_fetch.rb +53 -0
- data/lib/sidekiq/throttled/registry.rb +6 -12
- data/lib/sidekiq/throttled/strategy/base.rb +6 -2
- data/lib/sidekiq/throttled/strategy/concurrency.rb +6 -8
- data/lib/sidekiq/throttled/strategy/threshold.rb +7 -8
- data/lib/sidekiq/throttled/strategy.rb +18 -23
- data/lib/sidekiq/throttled/strategy_collection.rb +68 -0
- data/lib/sidekiq/throttled/version.rb +1 -1
- data/lib/sidekiq/throttled/web/stats.rb +7 -10
- data/lib/sidekiq/throttled/web/throttled.html.erb +6 -2
- data/lib/sidekiq/throttled/web.rb +2 -45
- data/lib/sidekiq/throttled/worker.rb +6 -121
- data/lib/sidekiq/throttled.rb +39 -50
- metadata +34 -68
- data/.coveralls.yml +0 -1
- data/.gitignore +0 -12
- data/.rspec +0 -5
- data/.rubocop.yml +0 -74
- data/.rubocop_todo.yml +0 -57
- data/.travis.yml +0 -38
- data/.yardopts +0 -1
- data/Appraisals +0 -13
- data/CHANGES.md +0 -203
- data/Gemfile +0 -31
- data/Guardfile +0 -25
- data/README.md +0 -266
- data/Rakefile +0 -27
- data/gemfiles/sidekiq_5.0.gemfile +0 -30
- data/gemfiles/sidekiq_5.1.gemfile +0 -30
- data/gemfiles/sidekiq_5.2.gemfile +0 -30
- data/lib/sidekiq/throttled/communicator/callbacks.rb +0 -73
- data/lib/sidekiq/throttled/communicator/listener.rb +0 -109
- data/lib/sidekiq/throttled/communicator.rb +0 -116
- data/lib/sidekiq/throttled/configuration.rb +0 -50
- data/lib/sidekiq/throttled/expirable_list.rb +0 -73
- data/lib/sidekiq/throttled/fetch/unit_of_work.rb +0 -78
- data/lib/sidekiq/throttled/fetch.rb +0 -89
- data/lib/sidekiq/throttled/patches/queue.rb +0 -18
- data/lib/sidekiq/throttled/queue_name.rb +0 -46
- data/lib/sidekiq/throttled/queues_pauser.rb +0 -152
- data/lib/sidekiq/throttled/testing.rb +0 -12
- data/lib/sidekiq/throttled/utils.rb +0 -19
- data/lib/sidekiq/throttled/web/queues.html.erb +0 -49
- data/lib/sidekiq/throttled/web/summary_fix.js +0 -10
- data/lib/sidekiq/throttled/web/summary_fix.rb +0 -34
- data/sidekiq-throttled.gemspec +0 -32
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: c7e45d38e16a4fd149ab2a5df541308d54304ae35bbd337d3701e534bffa025b
|
|
4
|
+
data.tar.gz: 7aa8a7a9469dbec0d06df4829dee111f015119861ac0caff00ca70148d014602
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 29e1a58bf302daeacb18695a618e55eec6ec177112274ac5dc85f7fbc1b14cb56c1bb8f8e9250dd22683182c8baee0d95c40f3708dba8518a5dde7cfbc71bbb6
|
|
7
|
+
data.tar.gz: 24ada7f5c9ee65400d1442feb72f95428ef9efdaf7aac2d38b732b2f4d649709712fb25d6b97e2c59627647bdcadf4c5d206fbb46f9b8321a5d293184866f2d2
|
data/{LICENSE.md → LICENSE}
RENAMED
|
@@ -1,6 +1,8 @@
|
|
|
1
1
|
The MIT License (MIT)
|
|
2
2
|
|
|
3
|
-
Copyright (c)
|
|
3
|
+
Copyright (c) 2022 Alexey Zapparov
|
|
4
|
+
Copyright (c) 2020-2021 Alexey Zapparov, SensorTower Inc.
|
|
5
|
+
Copyright (c) 2015-2020 SensorTower Inc.
|
|
4
6
|
|
|
5
7
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
8
|
of this software and associated documentation files (the "Software"), to deal
|
data/README.adoc
ADDED
|
@@ -0,0 +1,296 @@
|
|
|
1
|
+
= Sidekiq::Throttled
|
|
2
|
+
:ci-link: https://github.com/ixti/sidekiq-throttled/actions/workflows/ci.yml
|
|
3
|
+
:ci-badge: https://img.shields.io/github/actions/workflow/status/ixti/sidekiq-throttled/ci.yml?branch=main&style=for-the-badge
|
|
4
|
+
:gem-link: http://rubygems.org/gems/sidekiq-throttled
|
|
5
|
+
:gem-badge: https://img.shields.io/gem/v/sidekiq-throttled?style=for-the-badge
|
|
6
|
+
:doc-link: http://www.rubydoc.info/gems/sidekiq-throttled
|
|
7
|
+
:doc-badge: https://img.shields.io/badge/Documentation-API-blue?style=for-the-badge
|
|
8
|
+
|
|
9
|
+
****
|
|
10
|
+
{ci-link}[image:{ci-badge}[CI Status]]
|
|
11
|
+
{gem-link}[image:{gem-badge}[Latest Version]]
|
|
12
|
+
{doc-link}[image:{doc-badge}[API Documentation]]
|
|
13
|
+
****
|
|
14
|
+
|
|
15
|
+
NOTE: This is the 1.x *development* branch. For the 0.x *stable* branch, please
|
|
16
|
+
see: https://github.com/ixti/sidekiq-throttled/tree/0-x-stable[0-x-stable]
|
|
17
|
+
|
|
18
|
+
Concurrency and threshold throttling for https://github.com/mperham/sidekiq[Sidekiq].
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
== Installation
|
|
22
|
+
|
|
23
|
+
Add this line to your application's Gemfile:
|
|
24
|
+
|
|
25
|
+
[source,ruby]
|
|
26
|
+
----
|
|
27
|
+
gem "sidekiq-throttled"
|
|
28
|
+
----
|
|
29
|
+
|
|
30
|
+
And then execute:
|
|
31
|
+
|
|
32
|
+
$ bundle
|
|
33
|
+
|
|
34
|
+
Or install it yourself as:
|
|
35
|
+
|
|
36
|
+
$ gem install sidekiq-throttled
|
|
37
|
+
|
|
38
|
+
|
|
39
|
+
== Usage
|
|
40
|
+
|
|
41
|
+
Add somewhere in your app's bootstrap (e.g. `config/initializers/sidekiq.rb` if
|
|
42
|
+
you are using Rails):
|
|
43
|
+
|
|
44
|
+
[source,ruby]
|
|
45
|
+
----
|
|
46
|
+
require "sidekiq/throttled"
|
|
47
|
+
Sidekiq::Throttled.setup!
|
|
48
|
+
----
|
|
49
|
+
|
|
50
|
+
Load order can be an issue if you are using other Sidekiq plugins and/or middleware.
|
|
51
|
+
To prevent any problems, add the `.setup!` call to the bottom of your init file.
|
|
52
|
+
|
|
53
|
+
Once you've done that you can include `Sidekiq::Throttled::Job` to your
|
|
54
|
+
job classes and configure throttling:
|
|
55
|
+
|
|
56
|
+
[source,ruby]
|
|
57
|
+
----
|
|
58
|
+
class MyJob
|
|
59
|
+
include Sidekiq::Job
|
|
60
|
+
include Sidekiq::Throttled::Job
|
|
61
|
+
|
|
62
|
+
sidekiq_options :queue => :my_queue
|
|
63
|
+
|
|
64
|
+
sidekiq_throttle(
|
|
65
|
+
# Allow maximum 10 concurrent jobs of this class at a time.
|
|
66
|
+
concurrency: { limit: 10 },
|
|
67
|
+
# Allow maximum 1K jobs being processed within one hour window.
|
|
68
|
+
threshold: { limit: 1_000, period: 1.hour }
|
|
69
|
+
)
|
|
70
|
+
|
|
71
|
+
def perform
|
|
72
|
+
# ...
|
|
73
|
+
end
|
|
74
|
+
end
|
|
75
|
+
----
|
|
76
|
+
|
|
77
|
+
TIP: `Sidekiq::Throttled::Job` is aliased as `Sidekiq::Throttled::Worker`,
|
|
78
|
+
thus if you're using `Sidekiq::Worker` naming convention, you can use the
|
|
79
|
+
alias for consistency:
|
|
80
|
+
|
|
81
|
+
[source,ruby]
|
|
82
|
+
----
|
|
83
|
+
class MyWorker
|
|
84
|
+
include Sidekiq::Worker
|
|
85
|
+
include Sidekiq::Throttled::Worker
|
|
86
|
+
|
|
87
|
+
# ...
|
|
88
|
+
end
|
|
89
|
+
----
|
|
90
|
+
|
|
91
|
+
|
|
92
|
+
=== Configuration
|
|
93
|
+
|
|
94
|
+
[source,ruby]
|
|
95
|
+
----
|
|
96
|
+
Sidekiq::Throttled.configure do |config|
|
|
97
|
+
# Period in seconds to exclude queue from polling in case it returned
|
|
98
|
+
# {config.cooldown_threshold} amount of throttled jobs in a row. Set
|
|
99
|
+
# this value to `nil` to disable cooldown manager completely.
|
|
100
|
+
# Default: 2.0
|
|
101
|
+
config.cooldown_period = 2.0
|
|
102
|
+
|
|
103
|
+
# Exclude queue from polling after it returned given amount of throttled
|
|
104
|
+
# jobs in a row.
|
|
105
|
+
# Default: 1 (cooldown after first throttled job)
|
|
106
|
+
config.cooldown_threshold = 1
|
|
107
|
+
end
|
|
108
|
+
----
|
|
109
|
+
|
|
110
|
+
|
|
111
|
+
=== Observer
|
|
112
|
+
|
|
113
|
+
You can specify an observer that will be called on throttling. To do so pass an
|
|
114
|
+
`:observer` option with callable object:
|
|
115
|
+
|
|
116
|
+
[source,ruby]
|
|
117
|
+
----
|
|
118
|
+
class MyJob
|
|
119
|
+
include Sidekiq::Job
|
|
120
|
+
include Sidekiq::Throttled::Job
|
|
121
|
+
|
|
122
|
+
MY_OBSERVER = lambda do |strategy, *args|
|
|
123
|
+
# do something
|
|
124
|
+
end
|
|
125
|
+
|
|
126
|
+
sidekiq_options queue: :my_queue
|
|
127
|
+
|
|
128
|
+
sidekiq_throttle(
|
|
129
|
+
concurrency: { limit: 10 },
|
|
130
|
+
threshold: { limit: 100, period: 1.hour },
|
|
131
|
+
observer: MY_OBSERVER
|
|
132
|
+
)
|
|
133
|
+
|
|
134
|
+
def perform(*args)
|
|
135
|
+
# ...
|
|
136
|
+
end
|
|
137
|
+
end
|
|
138
|
+
----
|
|
139
|
+
|
|
140
|
+
Observer will receive `strategy, *args` arguments, where `strategy` is a Symbol
|
|
141
|
+
`:concurrency` or `:threshold`, and `*args` are the arguments that were passed
|
|
142
|
+
to the job.
|
|
143
|
+
|
|
144
|
+
|
|
145
|
+
=== Dynamic throttling
|
|
146
|
+
|
|
147
|
+
You can throttle jobs dynamically with `:key_suffix` option:
|
|
148
|
+
|
|
149
|
+
[source,ruby]
|
|
150
|
+
----
|
|
151
|
+
class MyJob
|
|
152
|
+
include Sidekiq::Job
|
|
153
|
+
include Sidekiq::Throttled::Job
|
|
154
|
+
|
|
155
|
+
sidekiq_options queue: :my_queue
|
|
156
|
+
|
|
157
|
+
sidekiq_throttle(
|
|
158
|
+
# Allow maximum 10 concurrent jobs per user at a time.
|
|
159
|
+
concurrency: { limit: 10, key_suffix: -> (user_id) { user_id } }
|
|
160
|
+
)
|
|
161
|
+
|
|
162
|
+
def perform(user_id)
|
|
163
|
+
# ...
|
|
164
|
+
end
|
|
165
|
+
end
|
|
166
|
+
----
|
|
167
|
+
|
|
168
|
+
You can also supply dynamic values for limits and periods by supplying a proc
|
|
169
|
+
for these values. The proc will be evaluated at the time the job is fetched
|
|
170
|
+
and will receive the same arguments that are passed to the job.
|
|
171
|
+
|
|
172
|
+
[source,ruby]
|
|
173
|
+
----
|
|
174
|
+
class MyJob
|
|
175
|
+
include Sidekiq::Job
|
|
176
|
+
include Sidekiq::Throttled::Job
|
|
177
|
+
|
|
178
|
+
sidekiq_options queue: :my_queue
|
|
179
|
+
|
|
180
|
+
sidekiq_throttle(
|
|
181
|
+
# Allow maximum 1000 concurrent jobs of this class at a time for VIPs and 10 for all other users.
|
|
182
|
+
concurrency: {
|
|
183
|
+
limit: ->(user_id) { User.vip?(user_id) ? 1_000 : 10 },
|
|
184
|
+
key_suffix: ->(user_id) { User.vip?(user_id) ? "vip" : "std" }
|
|
185
|
+
},
|
|
186
|
+
# Allow 1000 jobs/hour to be processed for VIPs and 10/day for all others
|
|
187
|
+
threshold: {
|
|
188
|
+
limit: ->(user_id) { User.vip?(user_id) ? 1_000 : 10 },
|
|
189
|
+
period: ->(user_id) { User.vip?(user_id) ? 1.hour : 1.day },
|
|
190
|
+
key_suffix: ->(user_id) { User.vip?(user_id) ? "vip" : "std" }
|
|
191
|
+
}
|
|
192
|
+
)
|
|
193
|
+
|
|
194
|
+
def perform(user_id)
|
|
195
|
+
# ...
|
|
196
|
+
end
|
|
197
|
+
end
|
|
198
|
+
----
|
|
199
|
+
|
|
200
|
+
You also can use several different keys to throttle one worker.
|
|
201
|
+
|
|
202
|
+
[source,ruby]
|
|
203
|
+
----
|
|
204
|
+
class MyJob
|
|
205
|
+
include Sidekiq::Job
|
|
206
|
+
include Sidekiq::Throttled::Job
|
|
207
|
+
|
|
208
|
+
sidekiq_options queue: :my_queue
|
|
209
|
+
|
|
210
|
+
sidekiq_throttle(
|
|
211
|
+
# Allow maximum 10 concurrent jobs per project at a time and maximum 2 jobs per user
|
|
212
|
+
concurrency: [
|
|
213
|
+
{ limit: 10, key_suffix: -> (project_id, user_id) { project_id } },
|
|
214
|
+
{ limit: 2, key_suffix: -> (project_id, user_id) { user_id } }
|
|
215
|
+
]
|
|
216
|
+
# For :threshold it works the same
|
|
217
|
+
)
|
|
218
|
+
|
|
219
|
+
def perform(project_id, user_id)
|
|
220
|
+
# ...
|
|
221
|
+
end
|
|
222
|
+
end
|
|
223
|
+
----
|
|
224
|
+
|
|
225
|
+
IMPORTANT: Don't forget to specify `:key_suffix` and make it return different
|
|
226
|
+
values if you are using dynamic limit/period options. Otherwise, you risk
|
|
227
|
+
getting into some trouble.
|
|
228
|
+
|
|
229
|
+
|
|
230
|
+
=== Concurrency throttling fine-tuning
|
|
231
|
+
|
|
232
|
+
Concurrency throttling is based on distributed locks. Those locks have default
|
|
233
|
+
time to live (TTL) set to 15 minutes. If your job takes more than 15 minutes
|
|
234
|
+
to finish, lock will be released and you might end up with more jobs running
|
|
235
|
+
concurrently than you expect.
|
|
236
|
+
|
|
237
|
+
This is done to avoid deadlocks - when by any reason (e.g. Sidekiq process was
|
|
238
|
+
OOM-killed) cleanup middleware wasn't executed and locks were not released.
|
|
239
|
+
|
|
240
|
+
If your job takes more than 15 minutes to complete, you can tune concurrency
|
|
241
|
+
lock TTL to fit your needs:
|
|
242
|
+
|
|
243
|
+
[source,ruby]
|
|
244
|
+
----
|
|
245
|
+
# Set concurrency strategy lock TTL to 1 hour.
|
|
246
|
+
sidekiq_throttle(concurrency: { limit: 20, ttl: 1.hour.to_i })
|
|
247
|
+
----
|
|
248
|
+
|
|
249
|
+
|
|
250
|
+
== Supported Ruby Versions
|
|
251
|
+
|
|
252
|
+
This library aims to support and is tested against the following Ruby versions:
|
|
253
|
+
|
|
254
|
+
* Ruby 3.0.x
|
|
255
|
+
* Ruby 3.1.x
|
|
256
|
+
* Ruby 3.2.x
|
|
257
|
+
|
|
258
|
+
If something doesn't work on one of these versions, it's a bug.
|
|
259
|
+
|
|
260
|
+
This library may inadvertently work (or seem to work) on other Ruby versions,
|
|
261
|
+
however support will only be provided for the versions listed above.
|
|
262
|
+
|
|
263
|
+
If you would like this library to support another Ruby version or
|
|
264
|
+
implementation, you may volunteer to be a maintainer. Being a maintainer
|
|
265
|
+
entails making sure all tests run and pass on that implementation. When
|
|
266
|
+
something breaks on your implementation, you will be responsible for providing
|
|
267
|
+
patches in a timely fashion. If critical issues for a particular implementation
|
|
268
|
+
exist at the time of a major release, support for that Ruby version may be
|
|
269
|
+
dropped.
|
|
270
|
+
|
|
271
|
+
|
|
272
|
+
== Supported Sidekiq Versions
|
|
273
|
+
|
|
274
|
+
This library aims to support and work with following Sidekiq versions:
|
|
275
|
+
|
|
276
|
+
* Sidekiq 7.0.x
|
|
277
|
+
* Sidekiq 7.1.x
|
|
278
|
+
* Sidekiq 7.2.x
|
|
279
|
+
|
|
280
|
+
|
|
281
|
+
== Development
|
|
282
|
+
|
|
283
|
+
bundle install
|
|
284
|
+
bundle exec appraisal generate
|
|
285
|
+
bundle exec appraisal install
|
|
286
|
+
bundle exec rake
|
|
287
|
+
|
|
288
|
+
|
|
289
|
+
== Contributing
|
|
290
|
+
|
|
291
|
+
* Fork sidekiq-throttled on GitHub
|
|
292
|
+
* Make your changes
|
|
293
|
+
* Ensure all tests pass (`bundle exec rake`)
|
|
294
|
+
* Send a pull request
|
|
295
|
+
* If we like them we'll merge them
|
|
296
|
+
* If we've accepted a patch, feel free to ask for commit access!
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Sidekiq
|
|
4
|
+
module Throttled
|
|
5
|
+
# Configuration object.
|
|
6
|
+
class Config
|
|
7
|
+
# Period in seconds to exclude queue from polling in case it returned
|
|
8
|
+
# {#cooldown_threshold} amount of throttled jobs in a row.
|
|
9
|
+
#
|
|
10
|
+
# Set this to `nil` to disable cooldown completely.
|
|
11
|
+
#
|
|
12
|
+
# @return [Float, nil]
|
|
13
|
+
attr_reader :cooldown_period
|
|
14
|
+
|
|
15
|
+
# Amount of throttled jobs returned from the queue subsequently after
|
|
16
|
+
# which queue will be excluded from polling for the durations of
|
|
17
|
+
# {#cooldown_period}.
|
|
18
|
+
#
|
|
19
|
+
# @return [Integer]
|
|
20
|
+
attr_reader :cooldown_threshold
|
|
21
|
+
|
|
22
|
+
def initialize
|
|
23
|
+
@cooldown_period = 2.0
|
|
24
|
+
@cooldown_threshold = 1
|
|
25
|
+
end
|
|
26
|
+
|
|
27
|
+
# @!attribute [w] cooldown_period
|
|
28
|
+
def cooldown_period=(value)
|
|
29
|
+
raise TypeError, "unexpected type #{value.class}" unless value.nil? || value.is_a?(Float)
|
|
30
|
+
raise ArgumentError, "period must be positive" unless value.nil? || value.positive?
|
|
31
|
+
|
|
32
|
+
@cooldown_period = value
|
|
33
|
+
end
|
|
34
|
+
|
|
35
|
+
# @!attribute [w] cooldown_threshold
|
|
36
|
+
def cooldown_threshold=(value)
|
|
37
|
+
raise TypeError, "unexpected type #{value.class}" unless value.is_a?(Integer)
|
|
38
|
+
raise ArgumentError, "threshold must be positive" unless value.positive?
|
|
39
|
+
|
|
40
|
+
@cooldown_threshold = value
|
|
41
|
+
end
|
|
42
|
+
end
|
|
43
|
+
end
|
|
44
|
+
end
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "concurrent"
|
|
4
|
+
|
|
5
|
+
require_relative "./expirable_set"
|
|
6
|
+
|
|
7
|
+
module Sidekiq
|
|
8
|
+
module Throttled
|
|
9
|
+
# @api internal
|
|
10
|
+
#
|
|
11
|
+
# Queues cooldown manager. Tracks list of queues that should be temporarily
|
|
12
|
+
# (for the duration of {Config#cooldown_period}) excluded from polling.
|
|
13
|
+
class Cooldown
|
|
14
|
+
class << self
|
|
15
|
+
# Returns new {Cooldown} instance if {Config#cooldown_period} is not `nil`.
|
|
16
|
+
#
|
|
17
|
+
# @param config [Config]
|
|
18
|
+
# @return [Cooldown, nil]
|
|
19
|
+
def [](config)
|
|
20
|
+
new(config) if config.cooldown_period
|
|
21
|
+
end
|
|
22
|
+
end
|
|
23
|
+
|
|
24
|
+
# @param config [Config]
|
|
25
|
+
def initialize(config)
|
|
26
|
+
@queues = ExpirableSet.new(config.cooldown_period)
|
|
27
|
+
@threshold = config.cooldown_threshold
|
|
28
|
+
@tracker = Concurrent::Map.new
|
|
29
|
+
end
|
|
30
|
+
|
|
31
|
+
# Notify that given queue returned job that was throttled.
|
|
32
|
+
#
|
|
33
|
+
# @param queue [String]
|
|
34
|
+
# @return [void]
|
|
35
|
+
def notify_throttled(queue)
|
|
36
|
+
@queues.add(queue) if @threshold <= @tracker.merge_pair(queue, 1, &:succ)
|
|
37
|
+
end
|
|
38
|
+
|
|
39
|
+
# Notify that given queue returned job that was not throttled.
|
|
40
|
+
#
|
|
41
|
+
# @param queue [String]
|
|
42
|
+
# @return [void]
|
|
43
|
+
def notify_admitted(queue)
|
|
44
|
+
@tracker.delete(queue)
|
|
45
|
+
end
|
|
46
|
+
|
|
47
|
+
# List of queues that should not be polled
|
|
48
|
+
#
|
|
49
|
+
# @return [Array<String>]
|
|
50
|
+
def queues
|
|
51
|
+
@queues.to_a
|
|
52
|
+
end
|
|
53
|
+
end
|
|
54
|
+
end
|
|
55
|
+
end
|
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "concurrent"
|
|
4
|
+
|
|
5
|
+
module Sidekiq
|
|
6
|
+
module Throttled
|
|
7
|
+
# @api internal
|
|
8
|
+
#
|
|
9
|
+
# Set of elements with expirations.
|
|
10
|
+
#
|
|
11
|
+
# @example
|
|
12
|
+
# set = ExpirableSet.new(10.0)
|
|
13
|
+
# set.add("a")
|
|
14
|
+
# sleep(5)
|
|
15
|
+
# set.add("b")
|
|
16
|
+
# set.to_a # => ["a", "b"]
|
|
17
|
+
# sleep(5)
|
|
18
|
+
# set.to_a # => ["b"]
|
|
19
|
+
class ExpirableSet
|
|
20
|
+
include Enumerable
|
|
21
|
+
|
|
22
|
+
# @param ttl [Float] expiration is seconds
|
|
23
|
+
# @raise [ArgumentError] if `ttl` is not positive Float
|
|
24
|
+
def initialize(ttl)
|
|
25
|
+
raise ArgumentError, "ttl must be positive Float" unless ttl.is_a?(Float) && ttl.positive?
|
|
26
|
+
|
|
27
|
+
@elements = Concurrent::Map.new
|
|
28
|
+
@ttl = ttl
|
|
29
|
+
end
|
|
30
|
+
|
|
31
|
+
# @param element [Object]
|
|
32
|
+
# @return [ExpirableSet] self
|
|
33
|
+
def add(element)
|
|
34
|
+
# cleanup expired elements to avoid mem-leak
|
|
35
|
+
horizon = now
|
|
36
|
+
expired = @elements.each_pair.select { |(_, sunset)| expired?(sunset, horizon) }
|
|
37
|
+
expired.each { |pair| @elements.delete_pair(*pair) }
|
|
38
|
+
|
|
39
|
+
# add new element
|
|
40
|
+
@elements[element] = now + @ttl
|
|
41
|
+
|
|
42
|
+
self
|
|
43
|
+
end
|
|
44
|
+
|
|
45
|
+
# @yield [Object] Gives each live (not expired) element to the block
|
|
46
|
+
def each
|
|
47
|
+
return to_enum __method__ unless block_given?
|
|
48
|
+
|
|
49
|
+
horizon = now
|
|
50
|
+
|
|
51
|
+
@elements.each_pair do |element, sunset|
|
|
52
|
+
yield element unless expired?(sunset, horizon)
|
|
53
|
+
end
|
|
54
|
+
|
|
55
|
+
self
|
|
56
|
+
end
|
|
57
|
+
|
|
58
|
+
private
|
|
59
|
+
|
|
60
|
+
# @return [Float]
|
|
61
|
+
def now
|
|
62
|
+
::Process.clock_gettime(::Process::CLOCK_MONOTONIC)
|
|
63
|
+
end
|
|
64
|
+
|
|
65
|
+
def expired?(sunset, horizon)
|
|
66
|
+
sunset <= horizon
|
|
67
|
+
end
|
|
68
|
+
end
|
|
69
|
+
end
|
|
70
|
+
end
|
|
@@ -0,0 +1,128 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
# internal
|
|
4
|
+
require_relative "./registry"
|
|
5
|
+
|
|
6
|
+
module Sidekiq
|
|
7
|
+
module Throttled
|
|
8
|
+
# Adds helpers to your worker classes
|
|
9
|
+
#
|
|
10
|
+
# @example Usage
|
|
11
|
+
#
|
|
12
|
+
# class MyJob
|
|
13
|
+
# include Sidekiq::Job
|
|
14
|
+
# include Sidekiq::Throttled::Job
|
|
15
|
+
#
|
|
16
|
+
# sidekiq_options :queue => :my_queue
|
|
17
|
+
# sidekiq_throttle :threshold => { :limit => 123, :period => 1.hour }
|
|
18
|
+
#
|
|
19
|
+
# def perform
|
|
20
|
+
# # ...
|
|
21
|
+
# end
|
|
22
|
+
# end
|
|
23
|
+
#
|
|
24
|
+
# @see ClassMethods
|
|
25
|
+
module Job
|
|
26
|
+
# Extends worker class with {ClassMethods}.
|
|
27
|
+
#
|
|
28
|
+
# @note Using `included` hook with extending worker with {ClassMethods}
|
|
29
|
+
# in order to make API inline with `include Sidekiq::Job`.
|
|
30
|
+
#
|
|
31
|
+
# @private
|
|
32
|
+
def self.included(base)
|
|
33
|
+
base.extend(ClassMethods)
|
|
34
|
+
end
|
|
35
|
+
|
|
36
|
+
# Helper methods added to the singleton class of destination
|
|
37
|
+
module ClassMethods
|
|
38
|
+
# Registers some strategy for the worker.
|
|
39
|
+
#
|
|
40
|
+
# @example Allow max 123 MyJob jobs per hour
|
|
41
|
+
#
|
|
42
|
+
# class MyJob
|
|
43
|
+
# include Sidekiq::Job
|
|
44
|
+
# include Sidekiq::Throttled::Job
|
|
45
|
+
#
|
|
46
|
+
# sidekiq_throttle({
|
|
47
|
+
# :threshold => { :limit => 123, :period => 1.hour }
|
|
48
|
+
# })
|
|
49
|
+
# end
|
|
50
|
+
#
|
|
51
|
+
# @example Allow max 10 concurrently running MyJob jobs
|
|
52
|
+
#
|
|
53
|
+
# class MyJob
|
|
54
|
+
# include Sidekiq::Job
|
|
55
|
+
# include Sidekiq::Throttled::Job
|
|
56
|
+
#
|
|
57
|
+
# sidekiq_throttle({
|
|
58
|
+
# :concurrency => { :limit => 10 }
|
|
59
|
+
# })
|
|
60
|
+
# end
|
|
61
|
+
#
|
|
62
|
+
# @example Allow max 10 concurrent MyJob jobs and max 123 per hour
|
|
63
|
+
#
|
|
64
|
+
# class MyJob
|
|
65
|
+
# include Sidekiq::Job
|
|
66
|
+
# include Sidekiq::Throttled::Job
|
|
67
|
+
#
|
|
68
|
+
# sidekiq_throttle({
|
|
69
|
+
# :threshold => { :limit => 123, :period => 1.hour },
|
|
70
|
+
# :concurrency => { :limit => 10 }
|
|
71
|
+
# })
|
|
72
|
+
# end
|
|
73
|
+
#
|
|
74
|
+
# @see Registry.add
|
|
75
|
+
# @return [void]
|
|
76
|
+
def sidekiq_throttle(**kwargs)
|
|
77
|
+
Registry.add(self, **kwargs)
|
|
78
|
+
end
|
|
79
|
+
|
|
80
|
+
# Adds current worker to preconfigured throttling strategy. Allows
|
|
81
|
+
# sharing same pool for multiple workers.
|
|
82
|
+
#
|
|
83
|
+
# First of all we need to create shared throttling strategy:
|
|
84
|
+
#
|
|
85
|
+
# # Create google_api throttling strategy
|
|
86
|
+
# Sidekiq::Throttled::Registry.add(:google_api, {
|
|
87
|
+
# :threshold => { :limit => 123, :period => 1.hour },
|
|
88
|
+
# :concurrency => { :limit => 10 }
|
|
89
|
+
# })
|
|
90
|
+
#
|
|
91
|
+
# Now we can assign it to our workers:
|
|
92
|
+
#
|
|
93
|
+
# class FetchProfileJob
|
|
94
|
+
# include Sidekiq::Job
|
|
95
|
+
# include Sidekiq::Throttled::Job
|
|
96
|
+
#
|
|
97
|
+
# sidekiq_throttle_as :google_api
|
|
98
|
+
# end
|
|
99
|
+
#
|
|
100
|
+
# class FetchCommentsJob
|
|
101
|
+
# include Sidekiq::Job
|
|
102
|
+
# include Sidekiq::Throttled::Job
|
|
103
|
+
#
|
|
104
|
+
# sidekiq_throttle_as :google_api
|
|
105
|
+
# end
|
|
106
|
+
#
|
|
107
|
+
# With the above configuration we ensure that there are maximum 10
|
|
108
|
+
# concurrently running jobs of FetchProfileJob or FetchCommentsJob
|
|
109
|
+
# allowed. And only 123 jobs of those are executed per hour.
|
|
110
|
+
#
|
|
111
|
+
# In other words, it will allow:
|
|
112
|
+
#
|
|
113
|
+
# - only `X` concurrent `FetchProfileJob`s
|
|
114
|
+
# - max `XX` `FetchProfileJob` per hour
|
|
115
|
+
# - only `Y` concurrent `FetchCommentsJob`s
|
|
116
|
+
# - max `YY` `FetchCommentsJob` per hour
|
|
117
|
+
#
|
|
118
|
+
# Where `(X + Y) == 10` and `(XX + YY) == 123`
|
|
119
|
+
#
|
|
120
|
+
# @see Registry.add_alias
|
|
121
|
+
# @return [void]
|
|
122
|
+
def sidekiq_throttle_as(name)
|
|
123
|
+
Registry.add_alias(self, name)
|
|
124
|
+
end
|
|
125
|
+
end
|
|
126
|
+
end
|
|
127
|
+
end
|
|
128
|
+
end
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
2
|
|
|
3
3
|
# internal
|
|
4
|
-
|
|
4
|
+
require_relative "./registry"
|
|
5
5
|
|
|
6
6
|
module Sidekiq
|
|
7
7
|
module Throttled
|
|
@@ -9,12 +9,19 @@ module Sidekiq
|
|
|
9
9
|
#
|
|
10
10
|
# @private
|
|
11
11
|
class Middleware
|
|
12
|
+
include Sidekiq::ServerMiddleware
|
|
13
|
+
|
|
12
14
|
# Called within Sidekiq job processing
|
|
13
15
|
def call(_worker, msg, _queue)
|
|
14
16
|
yield
|
|
15
17
|
ensure
|
|
16
|
-
|
|
17
|
-
|
|
18
|
+
job = msg.fetch("wrapped") { msg["class"] }
|
|
19
|
+
jid = msg["jid"]
|
|
20
|
+
|
|
21
|
+
if job && jid
|
|
22
|
+
Registry.get job do |strategy|
|
|
23
|
+
strategy.finalize!(jid, *msg["args"])
|
|
24
|
+
end
|
|
18
25
|
end
|
|
19
26
|
end
|
|
20
27
|
end
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "sidekiq"
|
|
4
|
+
require "sidekiq/fetch"
|
|
5
|
+
|
|
6
|
+
module Sidekiq
|
|
7
|
+
module Throttled
|
|
8
|
+
module Patches
|
|
9
|
+
module BasicFetch
|
|
10
|
+
# Retrieves job from redis.
|
|
11
|
+
#
|
|
12
|
+
# @return [Sidekiq::Throttled::UnitOfWork, nil]
|
|
13
|
+
def retrieve_work
|
|
14
|
+
work = super
|
|
15
|
+
|
|
16
|
+
if work && Throttled.throttled?(work.job)
|
|
17
|
+
Throttled.cooldown&.notify_throttled(work.queue)
|
|
18
|
+
requeue_throttled(work)
|
|
19
|
+
return nil
|
|
20
|
+
end
|
|
21
|
+
|
|
22
|
+
Throttled.cooldown&.notify_admitted(work.queue) if work
|
|
23
|
+
|
|
24
|
+
work
|
|
25
|
+
end
|
|
26
|
+
|
|
27
|
+
private
|
|
28
|
+
|
|
29
|
+
# Pushes job back to the head of the queue, so that job won't be tried
|
|
30
|
+
# immediately after it was requeued (in most cases).
|
|
31
|
+
#
|
|
32
|
+
# @note This is triggered when job is throttled. So it is same operation
|
|
33
|
+
# Sidekiq performs upon `Sidekiq::Worker.perform_async` call.
|
|
34
|
+
#
|
|
35
|
+
# @return [void]
|
|
36
|
+
def requeue_throttled(work)
|
|
37
|
+
redis { |conn| conn.lpush(work.queue, work.job) }
|
|
38
|
+
end
|
|
39
|
+
|
|
40
|
+
# Returns list of queues to try to fetch jobs from.
|
|
41
|
+
#
|
|
42
|
+
# @note It may return an empty array.
|
|
43
|
+
# @param [Array<String>] queues
|
|
44
|
+
# @return [Array<String>]
|
|
45
|
+
def queues_cmd
|
|
46
|
+
super - (Throttled.cooldown&.queues || [])
|
|
47
|
+
end
|
|
48
|
+
end
|
|
49
|
+
end
|
|
50
|
+
end
|
|
51
|
+
end
|
|
52
|
+
|
|
53
|
+
Sidekiq::BasicFetch.prepend(Sidekiq::Throttled::Patches::BasicFetch)
|