throttler 0.3.0 → 0.3.1
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 +5 -5
- data/lib/throttler.rb +3 -2
- data/lib/throttler/throttle.rb +18 -15
- data/lib/throttler/version.rb +1 -1
- data/test/throttler_test.rb +16 -0
- metadata +4 -4
data/README.md
CHANGED
@@ -3,20 +3,21 @@ Throttler
|
|
3
3
|
|
4
4
|
[![travis] [1]] [2]
|
5
5
|
|
6
|
-
Throttler rate-limits code execution across threads
|
6
|
+
Throttler rate-limits code execution across threads, evented blocks, or
|
7
|
+
processes on a server.
|
7
8
|
|
8
9
|
![Mapplethorpe][3]
|
9
10
|
|
10
11
|
Installation
|
11
12
|
------------
|
12
13
|
|
14
|
+
Throttler works only on platforms that support file locking.
|
15
|
+
|
13
16
|
```ruby
|
14
|
-
#
|
17
|
+
# Gemfile
|
15
18
|
gem 'throttler'
|
16
19
|
```
|
17
20
|
|
18
|
-
Throttler works only on platforms that support file locking.
|
19
|
-
|
20
21
|
Usage
|
21
22
|
-----
|
22
23
|
|
@@ -26,7 +27,6 @@ once every second per IP address.
|
|
26
27
|
```ruby
|
27
28
|
class Scrape
|
28
29
|
def self.perform(site, ip_address, *ids)
|
29
|
-
# The block is syntactic sugar.
|
30
30
|
Throttler.limit 1.0, site, ip_address do
|
31
31
|
spider = Spider.new site, ip_address
|
32
32
|
spider.scrape *ids
|
data/lib/throttler.rb
CHANGED
@@ -12,12 +12,13 @@ module Throttler
|
|
12
12
|
|
13
13
|
begin
|
14
14
|
throttle = Throttle.new namespace
|
15
|
-
throttle.
|
15
|
+
throttle.strangle
|
16
16
|
throttle.hold wait
|
17
17
|
ensure
|
18
|
-
throttle.
|
18
|
+
throttle.release
|
19
19
|
end
|
20
20
|
|
21
|
+
# Syntactic sugar.
|
21
22
|
yield if block_given?
|
22
23
|
end
|
23
24
|
end
|
data/lib/throttler/throttle.rb
CHANGED
@@ -8,15 +8,6 @@ module Throttler
|
|
8
8
|
|
9
9
|
def initialize(namespace)
|
10
10
|
@namespace = namespace
|
11
|
-
@file = File.open(path, File::RDWR|File::CREAT)
|
12
|
-
end
|
13
|
-
|
14
|
-
def lock
|
15
|
-
@file.flock(File::LOCK_EX)
|
16
|
-
end
|
17
|
-
|
18
|
-
def path
|
19
|
-
"#{self.class.tmp_dir}/.lock-#{@namespace}"
|
20
11
|
end
|
21
12
|
|
22
13
|
def hold(wait)
|
@@ -24,25 +15,37 @@ module Throttler
|
|
24
15
|
timestamp!
|
25
16
|
end
|
26
17
|
|
27
|
-
def
|
28
|
-
@file.flock
|
18
|
+
def release
|
19
|
+
@file.flock File::LOCK_UN
|
29
20
|
@file.close
|
30
21
|
end
|
31
22
|
|
23
|
+
def strangle
|
24
|
+
file.flock File::LOCK_EX
|
25
|
+
end
|
26
|
+
|
32
27
|
private
|
33
28
|
|
29
|
+
def file
|
30
|
+
@file ||= File.open path, File::RDWR|File::CREAT
|
31
|
+
end
|
32
|
+
|
34
33
|
def now
|
35
34
|
Time.now.to_f
|
36
35
|
end
|
37
36
|
|
37
|
+
def path
|
38
|
+
"#{self.class.tmp_dir}/.lock-#{@namespace}"
|
39
|
+
end
|
40
|
+
|
38
41
|
def timestamp
|
39
|
-
|
40
|
-
|
42
|
+
file.rewind
|
43
|
+
file.gets.to_f
|
41
44
|
end
|
42
45
|
|
43
46
|
def timestamp!
|
44
|
-
|
45
|
-
|
47
|
+
file.rewind
|
48
|
+
file.write now
|
46
49
|
end
|
47
50
|
end
|
48
51
|
end
|
data/lib/throttler/version.rb
CHANGED
data/test/throttler_test.rb
CHANGED
@@ -8,13 +8,29 @@ class Work
|
|
8
8
|
end
|
9
9
|
end
|
10
10
|
|
11
|
+
class WorkAsWell
|
12
|
+
def self.perform
|
13
|
+
Throttler.limit 0.1, 'foo'
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
11
17
|
class ThrottlerTest < Test::Unit::TestCase
|
12
18
|
def test_throttling
|
13
19
|
time = Benchmark.realtime do
|
14
20
|
11.times { Process.fork { Work.perform } }
|
15
21
|
Process.waitall
|
16
22
|
end
|
23
|
+
assert_equal 1, time.to_i
|
24
|
+
end
|
17
25
|
|
26
|
+
def test_namespace
|
27
|
+
time = Benchmark.realtime do
|
28
|
+
11.times do
|
29
|
+
Process.fork { Work.perform }
|
30
|
+
Process.fork { WorkAsWell.perform }
|
31
|
+
end
|
32
|
+
Process.waitall
|
33
|
+
end
|
18
34
|
assert_equal 1, time.to_i
|
19
35
|
end
|
20
36
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: throttler
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.3.
|
4
|
+
version: 0.3.1
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -10,11 +10,11 @@ authors:
|
|
10
10
|
autorequire:
|
11
11
|
bindir: bin
|
12
12
|
cert_chain: []
|
13
|
-
date: 2012-03-
|
13
|
+
date: 2012-03-17 00:00:00.000000000 Z
|
14
14
|
dependencies:
|
15
15
|
- !ruby/object:Gem::Dependency
|
16
16
|
name: rake
|
17
|
-
requirement: &
|
17
|
+
requirement: &70163640755900 !ruby/object:Gem::Requirement
|
18
18
|
none: false
|
19
19
|
requirements:
|
20
20
|
- - ~>
|
@@ -22,7 +22,7 @@ dependencies:
|
|
22
22
|
version: 0.9.2
|
23
23
|
type: :development
|
24
24
|
prerelease: false
|
25
|
-
version_requirements: *
|
25
|
+
version_requirements: *70163640755900
|
26
26
|
description: Throttler rate-limits code execution across threads or processes.
|
27
27
|
email:
|
28
28
|
- hakan.ensari@papercavalier.com
|