throttler 0.2.2 → 0.2.3
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.
- data/README.md +6 -7
- data/lib/throttler/version.rb +1 -1
- data/lib/throttler.rb +4 -7
- data/spec/unit/throttler_spec.rb +1 -1
- metadata +3 -3
data/README.md
CHANGED
@@ -1,26 +1,25 @@
|
|
1
1
|
Throttler
|
2
2
|
=========
|
3
3
|
|
4
|
-
Throttler is a tired, cranky module that
|
4
|
+
Throttler is a tired, cranky module that helps you throttle stuff in parallel-running Ruby scripts on a single machine.
|
5
5
|
|
6
6
|

|
7
7
|
|
8
8
|
Example
|
9
9
|
-------
|
10
10
|
|
11
|
-
Our very own
|
11
|
+
Our very own use case: We have multiple Resque workers hitting the Amazon IP on multiple addresses and want to make sure they don't fire more than once per locale per IP.
|
12
|
+
|
13
|
+
Here's some pseudo code:
|
12
14
|
|
13
15
|
class Worker
|
14
16
|
include Throttler
|
15
17
|
|
16
|
-
|
17
|
-
:interface
|
18
|
-
|
19
|
-
def perform
|
18
|
+
def perform(*args)
|
20
19
|
scope = "#{locale}-#{interface}"
|
21
20
|
freq = 1.0
|
22
21
|
|
23
|
-
throttle(scope,
|
22
|
+
throttle(scope, freq) do
|
24
23
|
# perform a request
|
25
24
|
end
|
26
25
|
end
|
data/lib/throttler/version.rb
CHANGED
data/lib/throttler.rb
CHANGED
@@ -2,16 +2,13 @@ require "throttler/timer"
|
|
2
2
|
|
3
3
|
# = Throttler
|
4
4
|
#
|
5
|
-
#
|
5
|
+
# A tired, cranky module that helps you throttle stuff in parallel-running Ruby
|
6
|
+
# scripts on a single machine. It gets the job done.
|
6
7
|
module Throttler
|
7
|
-
|
8
|
-
# Throttles the frequency in which a block is run
|
9
|
-
#
|
10
|
-
# Optionally pass a scope and interval.
|
11
|
-
def throttle(scope="throttler", interval=1.0)
|
8
|
+
def throttle(scope="throttler",freq=1.0)
|
12
9
|
timer = Timer.new(scope)
|
13
10
|
timer.lock
|
14
|
-
sleep [timer.timestamp +
|
11
|
+
sleep [timer.timestamp + freq - Time.now.to_f, 0.0].max
|
15
12
|
timer.timestamp = Time.now.to_f
|
16
13
|
timer.unlock
|
17
14
|
yield if block_given?
|
data/spec/unit/throttler_spec.rb
CHANGED
metadata
CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
|
|
5
5
|
segments:
|
6
6
|
- 0
|
7
7
|
- 2
|
8
|
-
-
|
9
|
-
version: 0.2.
|
8
|
+
- 3
|
9
|
+
version: 0.2.3
|
10
10
|
platform: ruby
|
11
11
|
authors:
|
12
12
|
- Hakan Ensari
|
@@ -33,7 +33,7 @@ dependencies:
|
|
33
33
|
version: 2.1.0
|
34
34
|
type: :development
|
35
35
|
version_requirements: *id001
|
36
|
-
description: A tired, cranky module that throttles parallel-running Ruby scripts on a single machine.
|
36
|
+
description: A tired, cranky module that helps you throttle stuff in parallel-running Ruby scripts on a single machine. A tired, cranky module that throttles parallel-running Ruby scripts on a single machine.
|
37
37
|
email:
|
38
38
|
- code@papercavalier.com
|
39
39
|
executables: []
|