glutton_ratelimit 0.1.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.
- data/.document +5 -0
- data/.gitignore +21 -0
- data/LICENSE +24 -0
- data/README.rdoc +125 -0
- data/Rakefile +52 -0
- data/VERSION +1 -0
- data/examples/limit_instance_methods.rb +34 -0
- data/examples/simple_manual.rb +30 -0
- data/glutton_ratelimit.gemspec +64 -0
- data/lib/glutton_ratelimit.rb +29 -0
- data/lib/glutton_ratelimit/averaged_throttle.rb +44 -0
- data/lib/glutton_ratelimit/bursty_ring_buffer.rb +24 -0
- data/lib/glutton_ratelimit/bursty_token_bucket.rb +26 -0
- data/test/helper.rb +21 -0
- data/test/test_glutton_ratelimit_averaged_throttle.rb +15 -0
- data/test/test_glutton_ratelimit_bursty_ring_buffer.rb +15 -0
- data/test/test_glutton_ratelimit_bursty_token_bucket.rb +15 -0
- data/test/testing_module.rb +67 -0
- metadata +86 -0
data/.document
ADDED
data/.gitignore
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,24 @@
|
|
1
|
+
This is free and unencumbered software released into the public domain.
|
2
|
+
|
3
|
+
Anyone is free to copy, modify, publish, use, compile, sell, or
|
4
|
+
distribute this software, either in source code form or as a compiled
|
5
|
+
binary, for any purpose, commercial or non-commercial, and by any
|
6
|
+
means.
|
7
|
+
|
8
|
+
In jurisdictions that recognize copyright laws, the author or authors
|
9
|
+
of this software dedicate any and all copyright interest in the
|
10
|
+
software to the public domain. We make this dedication for the benefit
|
11
|
+
of the public at large and to the detriment of our heirs and
|
12
|
+
successors. We intend this dedication to be an overt act of
|
13
|
+
relinquishment in perpetuity of all present and future rights to this
|
14
|
+
software under copyright law.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
|
19
|
+
IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
|
20
|
+
OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
|
21
|
+
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
|
22
|
+
OTHER DEALINGS IN THE SOFTWARE.
|
23
|
+
|
24
|
+
For more information, please refer to <http://unlicense.org/>
|
data/README.rdoc
ADDED
@@ -0,0 +1,125 @@
|
|
1
|
+
= glutton_ratelimit
|
2
|
+
|
3
|
+
A Ruby library for limiting the number of times a method can be invoked within a specified time period.
|
4
|
+
|
5
|
+
The rate-limiting is simple if somewhat naïve. Throttled methods are blocked using sleep.
|
6
|
+
|
7
|
+
== Use Cases
|
8
|
+
|
9
|
+
You might wish to use this library to throttle code that:
|
10
|
+
|
11
|
+
* Accesses a 3rd-party API that imposes a maximum rate-limit.
|
12
|
+
* Scrapes data from any website where rapid repeated access is banned.
|
13
|
+
|
14
|
+
For example, EchoNest API requests need to be limited to 120 every minute.
|
15
|
+
|
16
|
+
== Types of Limiting
|
17
|
+
|
18
|
+
There are two types of rate limiting provided by this library:
|
19
|
+
|
20
|
+
* Bursty Token Bucket Limiting (GluttonRatelimit::BurstyTokenBucket)
|
21
|
+
* Average Throttle Limiting (GluttonRatelimit::AveragedThrottle)
|
22
|
+
|
23
|
+
=== Bursty Token Bucket
|
24
|
+
|
25
|
+
If executions are limited to n times per m seconds then:
|
26
|
+
|
27
|
+
1. n executions will be allowed immediately.
|
28
|
+
2. Before the next execution the process will sleep for the remainder of the m second time period.
|
29
|
+
3. The process is repeated.
|
30
|
+
|
31
|
+
The amount of time slept in step 2 will depend on how long the n executions took in step 1.
|
32
|
+
|
33
|
+
=== Average Throttle
|
34
|
+
|
35
|
+
If executions are limited to n times per m seconds then:
|
36
|
+
|
37
|
+
1. The first execution will occur immediately.
|
38
|
+
2. Before each of the remaining n-1 executions the process will attempt to sleep so that the executions are evenly spaced within the m second time period.
|
39
|
+
3. The process is repeated.
|
40
|
+
|
41
|
+
The amount of time slept before each execution will depend on the time period m and the average elapsed time of each execution.
|
42
|
+
|
43
|
+
== Instance Method Limiting Example
|
44
|
+
|
45
|
+
The rate_limit method can be used at the end of a class definition to limit specific instance methods.
|
46
|
+
|
47
|
+
class LimitInstanceMethod
|
48
|
+
# The class must be extended to permit limiting.
|
49
|
+
extend GluttonRatelimit
|
50
|
+
|
51
|
+
def initialize
|
52
|
+
@start = Time.now
|
53
|
+
end
|
54
|
+
|
55
|
+
def limit_me
|
56
|
+
puts "#{Time.now - @start}"
|
57
|
+
end
|
58
|
+
|
59
|
+
# Throttle the limit_me method to six executions every six seconds.
|
60
|
+
rate_limit :limit_me, 6, 6
|
61
|
+
end
|
62
|
+
|
63
|
+
t = LimitInstanceMethod.new
|
64
|
+
|
65
|
+
10.times { t.limit_me }
|
66
|
+
|
67
|
+
When using the rate_limit method the limiting defaults to GluttonRatelimit::AveragedThrottle. Token Bucket limiting can also be specified:
|
68
|
+
|
69
|
+
rate_limite :limit_me, 6, 6, GluttonRatelimit::BurstyTokenBucket
|
70
|
+
|
71
|
+
== Simple Manual Limiting Example
|
72
|
+
|
73
|
+
Chunks of code can also be manually throttled using the wait method of a specific GluttonRatelimit object.
|
74
|
+
|
75
|
+
# Maximum of twelve executions every five seconds.
|
76
|
+
rl = GluttonRatelimit::BurstyTokenBucket.new 12, 5
|
77
|
+
|
78
|
+
25.times do
|
79
|
+
# BurstyTokenBucket will allow for a full burst of executions followed by a pause.
|
80
|
+
rl.wait
|
81
|
+
# Simulating a constant-time task:
|
82
|
+
sleep 0.1
|
83
|
+
end
|
84
|
+
|
85
|
+
# Maximum of six executions every six seconds.
|
86
|
+
rl = GluttonRatelimit::AveragedThrottle.new 6, 6
|
87
|
+
13.times do
|
88
|
+
# AverageThrottle will attempt to evenly space executions within the allowed period.
|
89
|
+
rl.wait
|
90
|
+
# Simulating a 0 to 1 second random-time task:
|
91
|
+
sleep rand
|
92
|
+
end
|
93
|
+
|
94
|
+
== More Examples
|
95
|
+
|
96
|
+
More examples can be found within the examples folder.
|
97
|
+
|
98
|
+
== Warnings
|
99
|
+
|
100
|
+
Before using the library you should be aware of the following warnings.
|
101
|
+
|
102
|
+
=== Short Tasks and AveragedThrottle
|
103
|
+
|
104
|
+
The inaccuracy of Ruby's sleep method may cause timing issues with the AveragedThrottle limiting. Specifically, the limiting accuracy may be slightly-off when trying to rate-limit quick methods (sub-millisecond tasks).
|
105
|
+
|
106
|
+
See: http://codeidol.com/other/rubyckbk/Date-and-Time/Waiting-a-Certain-Amount-of-Time
|
107
|
+
|
108
|
+
It is recommend that you use the BurstyTokenBucket limiting when throttling very short tasks.
|
109
|
+
|
110
|
+
=== Naive Throttling
|
111
|
+
|
112
|
+
As mentioned above, throttling is accomplish by blocking script execution using sleep. There is no support for dropping or queuing throttled method invocations.
|
113
|
+
|
114
|
+
This library is not thread safe.
|
115
|
+
|
116
|
+
== Thanks
|
117
|
+
|
118
|
+
Some of the algorithms were inspired by these discussions:
|
119
|
+
|
120
|
+
* http://stackoverflow.com/questions/667508/whats-a-good-rate-limiting-algorithm
|
121
|
+
* http://stackoverflow.com/questions/1407113/throttling-method-calls-to-m-requests-in-n-seconds
|
122
|
+
|
123
|
+
== License
|
124
|
+
|
125
|
+
This is free and unencumbered software released into the public domain. See LICENSE for details.
|
data/Rakefile
ADDED
@@ -0,0 +1,52 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'rake'
|
3
|
+
|
4
|
+
begin
|
5
|
+
require 'jeweler'
|
6
|
+
Jeweler::Tasks.new do |gem|
|
7
|
+
gem.name = "glutton_ratelimit"
|
8
|
+
gem.summary = %Q{Simple Ruby library for self-imposed rater-limiting.}
|
9
|
+
gem.description = %Q{A Ruby library for limiting the number of times a method can be invoked within a specified time period.}
|
10
|
+
gem.email = "stungeye@gmail.com"
|
11
|
+
gem.homepage = "http://github.com/stungeye/glutton_ratelimit"
|
12
|
+
gem.authors = ["Wally Glutton"]
|
13
|
+
# gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
|
14
|
+
end
|
15
|
+
Jeweler::GemcutterTasks.new
|
16
|
+
rescue LoadError
|
17
|
+
puts "Jeweler (or a dependency) not available. Install it with: gem install jeweler"
|
18
|
+
end
|
19
|
+
|
20
|
+
require 'rake/testtask'
|
21
|
+
Rake::TestTask.new(:test) do |test|
|
22
|
+
test.libs << 'lib' << 'test'
|
23
|
+
test.pattern = 'test/**/test_*.rb'
|
24
|
+
test.verbose = true
|
25
|
+
end
|
26
|
+
|
27
|
+
begin
|
28
|
+
require 'rcov/rcovtask'
|
29
|
+
Rcov::RcovTask.new do |test|
|
30
|
+
test.libs << 'test'
|
31
|
+
test.pattern = 'test/**/test_*.rb'
|
32
|
+
test.verbose = true
|
33
|
+
end
|
34
|
+
rescue LoadError
|
35
|
+
task :rcov do
|
36
|
+
abort "RCov is not available. In order to run rcov, you must: sudo gem install spicycode-rcov"
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
task :test => :check_dependencies
|
41
|
+
|
42
|
+
task :default => :test
|
43
|
+
|
44
|
+
require 'rake/rdoctask'
|
45
|
+
Rake::RDocTask.new do |rdoc|
|
46
|
+
version = File.exist?('VERSION') ? File.read('VERSION') : ""
|
47
|
+
|
48
|
+
rdoc.rdoc_dir = 'rdoc'
|
49
|
+
rdoc.title = "glutton_ratelimit #{version}"
|
50
|
+
rdoc.rdoc_files.include('README*')
|
51
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
52
|
+
end
|
data/VERSION
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
0.1.0
|
@@ -0,0 +1,34 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'glutton_ratelimit'
|
3
|
+
|
4
|
+
class LimitInstanceMethods
|
5
|
+
extend GluttonRatelimit
|
6
|
+
|
7
|
+
def initialize
|
8
|
+
@start = Time.now
|
9
|
+
end
|
10
|
+
|
11
|
+
def limit_me
|
12
|
+
puts "#{Time.now - @start}"
|
13
|
+
sleep 0.001
|
14
|
+
end
|
15
|
+
|
16
|
+
def cap_me
|
17
|
+
puts "#{Time.now - @start}"
|
18
|
+
sleep 0.001
|
19
|
+
end
|
20
|
+
|
21
|
+
rate_limit :limit_me, 6, 6
|
22
|
+
rate_limit :cap_me, 6, 6, GluttonRatelimit::BurstyTokenBucket
|
23
|
+
end
|
24
|
+
|
25
|
+
t = LimitInstanceMethods.new
|
26
|
+
|
27
|
+
puts "Six requests every 6 seconds (Averaged): "
|
28
|
+
13.times { t.limit_me }
|
29
|
+
puts "Six requests every 6 seconds (Bursty): "
|
30
|
+
13.times { t.cap_me }
|
31
|
+
|
32
|
+
# In both cases:
|
33
|
+
# The 7th execution should occur after 6 seconds after the first.
|
34
|
+
# The 13th execution should occur after 12 seconds after the first.
|
@@ -0,0 +1,30 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'glutton_ratelimit'
|
3
|
+
|
4
|
+
puts "Maximum of 12 executions every 5 seconds (Bursty):"
|
5
|
+
rl = GluttonRatelimit::BurstyTokenBucket.new 12, 5
|
6
|
+
|
7
|
+
start = Time.now
|
8
|
+
25.times do |n|
|
9
|
+
# BurstyTokenBucket will allow for a full burst of executions followed by a pause.
|
10
|
+
rl.wait
|
11
|
+
puts "#{n+1} - #{Time.now - start}"
|
12
|
+
# Simulating a constant-time task:
|
13
|
+
sleep 0.1
|
14
|
+
end
|
15
|
+
|
16
|
+
# The 25th execution should occur after 10 seconds has elapsed.
|
17
|
+
|
18
|
+
puts "Maximum of 3 executions every 3 seconds (Averaged):"
|
19
|
+
rl = GluttonRatelimit::AveragedThrottle.new 3, 3
|
20
|
+
|
21
|
+
start = Time.now
|
22
|
+
7.times do |n|
|
23
|
+
# AverageThrottle will attempt to evenly space executions within the allowed period.
|
24
|
+
rl.wait
|
25
|
+
puts "#{n+1} - #{Time.now - start}"
|
26
|
+
# Simulating a 0 to 1 second random-time task:
|
27
|
+
sleep rand
|
28
|
+
end
|
29
|
+
|
30
|
+
# The 7th execution should occur after 6 seconds has elapsed.
|
@@ -0,0 +1,64 @@
|
|
1
|
+
# Generated by jeweler
|
2
|
+
# DO NOT EDIT THIS FILE DIRECTLY
|
3
|
+
# Instead, edit Jeweler::Tasks in Rakefile, and run the gemspec command
|
4
|
+
# -*- encoding: utf-8 -*-
|
5
|
+
|
6
|
+
Gem::Specification.new do |s|
|
7
|
+
s.name = %q{glutton_ratelimit}
|
8
|
+
s.version = "0.1.0"
|
9
|
+
|
10
|
+
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
|
+
s.authors = ["Wally Glutton"]
|
12
|
+
s.date = %q{2010-06-10}
|
13
|
+
s.description = %q{A Ruby library for limiting the number of times a method can be invoked within a specified time period.}
|
14
|
+
s.email = %q{stungeye@gmail.com}
|
15
|
+
s.extra_rdoc_files = [
|
16
|
+
"LICENSE",
|
17
|
+
"README.rdoc"
|
18
|
+
]
|
19
|
+
s.files = [
|
20
|
+
".document",
|
21
|
+
".gitignore",
|
22
|
+
"LICENSE",
|
23
|
+
"README.rdoc",
|
24
|
+
"Rakefile",
|
25
|
+
"VERSION",
|
26
|
+
"examples/limit_instance_methods.rb",
|
27
|
+
"examples/simple_manual.rb",
|
28
|
+
"glutton_ratelimit.gemspec",
|
29
|
+
"lib/glutton_ratelimit.rb",
|
30
|
+
"lib/glutton_ratelimit/averaged_throttle.rb",
|
31
|
+
"lib/glutton_ratelimit/bursty_ring_buffer.rb",
|
32
|
+
"lib/glutton_ratelimit/bursty_token_bucket.rb",
|
33
|
+
"test/helper.rb",
|
34
|
+
"test/test_glutton_ratelimit_averaged_throttle.rb",
|
35
|
+
"test/test_glutton_ratelimit_bursty_ring_buffer.rb",
|
36
|
+
"test/test_glutton_ratelimit_bursty_token_bucket.rb",
|
37
|
+
"test/testing_module.rb"
|
38
|
+
]
|
39
|
+
s.homepage = %q{http://github.com/stungeye/glutton_ratelimit}
|
40
|
+
s.rdoc_options = ["--charset=UTF-8"]
|
41
|
+
s.require_paths = ["lib"]
|
42
|
+
s.rubygems_version = %q{1.3.6}
|
43
|
+
s.summary = %q{Simple Ruby library for self-imposed rater-limiting.}
|
44
|
+
s.test_files = [
|
45
|
+
"test/test_glutton_ratelimit_averaged_throttle.rb",
|
46
|
+
"test/helper.rb",
|
47
|
+
"test/test_glutton_ratelimit_bursty_ring_buffer.rb",
|
48
|
+
"test/testing_module.rb",
|
49
|
+
"test/test_glutton_ratelimit_bursty_token_bucket.rb",
|
50
|
+
"examples/limit_instance_methods.rb",
|
51
|
+
"examples/simple_manual.rb"
|
52
|
+
]
|
53
|
+
|
54
|
+
if s.respond_to? :specification_version then
|
55
|
+
current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
|
56
|
+
s.specification_version = 3
|
57
|
+
|
58
|
+
if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
|
59
|
+
else
|
60
|
+
end
|
61
|
+
else
|
62
|
+
end
|
63
|
+
end
|
64
|
+
|
@@ -0,0 +1,29 @@
|
|
1
|
+
module GluttonRatelimit
|
2
|
+
|
3
|
+
def rate_limit symbol, executions, time_period, rl_class = AveragedThrottle
|
4
|
+
rl = rl_class.new executions, time_period
|
5
|
+
old_symbol = "#{symbol}_old".to_sym
|
6
|
+
alias_method old_symbol, symbol
|
7
|
+
define_method symbol do |*args|
|
8
|
+
rl.wait
|
9
|
+
self.send old_symbol, *args
|
10
|
+
end
|
11
|
+
end
|
12
|
+
|
13
|
+
private
|
14
|
+
# All the other classes extend this parent and are therefore
|
15
|
+
# constructed in the same manner.
|
16
|
+
class ParentLimiter
|
17
|
+
attr_reader :executions
|
18
|
+
|
19
|
+
def initialize executions, time_period
|
20
|
+
@executions = executions
|
21
|
+
@time_period = time_period
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
dir = File.expand_path(File.dirname(__FILE__))
|
27
|
+
require File.join(dir, "glutton_ratelimit", "bursty_ring_buffer")
|
28
|
+
require File.join(dir, "glutton_ratelimit", "bursty_token_bucket")
|
29
|
+
require File.join(dir, "glutton_ratelimit", "averaged_throttle")
|
@@ -0,0 +1,44 @@
|
|
1
|
+
module GluttonRatelimit
|
2
|
+
|
3
|
+
class AveragedThrottle < ParentLimiter
|
4
|
+
|
5
|
+
def reset_bucket
|
6
|
+
@before_previous_execution = Time.now if @before_previous_execution.nil?
|
7
|
+
@oldest_timestamp = Time.now
|
8
|
+
@tokens = @executions
|
9
|
+
@total_task_time = 0
|
10
|
+
end
|
11
|
+
|
12
|
+
def executed_this_period
|
13
|
+
@executions - @tokens
|
14
|
+
end
|
15
|
+
|
16
|
+
def average_task_time
|
17
|
+
@total_task_time.to_f / executed_this_period
|
18
|
+
end
|
19
|
+
|
20
|
+
def wait
|
21
|
+
reset_bucket if @tokens.nil?
|
22
|
+
|
23
|
+
now = Time.now
|
24
|
+
delta_since_previous = now - @before_previous_execution
|
25
|
+
@total_task_time += delta_since_previous
|
26
|
+
remaining_time = @time_period - (now - @oldest_timestamp)
|
27
|
+
|
28
|
+
if @tokens.zero?
|
29
|
+
sleep(remaining_time) if remaining_time > 0
|
30
|
+
reset_bucket
|
31
|
+
elsif executed_this_period != 0
|
32
|
+
throttle = (remaining_time.to_f + delta_since_previous) / (@tokens+1) - average_task_time
|
33
|
+
sleep throttle if throttle > 0
|
34
|
+
end
|
35
|
+
|
36
|
+
@tokens -= 1
|
37
|
+
@before_previous_execution = Time.now
|
38
|
+
end
|
39
|
+
|
40
|
+
end
|
41
|
+
|
42
|
+
end
|
43
|
+
|
44
|
+
|
@@ -0,0 +1,24 @@
|
|
1
|
+
module GluttonRatelimit
|
2
|
+
|
3
|
+
class BurstyRingBuffer < ParentLimiter
|
4
|
+
|
5
|
+
def oldest_timestamp
|
6
|
+
@timestamps = Array.new executions, (Time.now - @time_period) if @timestamps.nil?
|
7
|
+
@timestamps[0]
|
8
|
+
end
|
9
|
+
|
10
|
+
def current_timestamp= new_stamp
|
11
|
+
@timestamps.push(new_stamp).shift
|
12
|
+
end
|
13
|
+
|
14
|
+
def wait
|
15
|
+
delta = Time.now - oldest_timestamp
|
16
|
+
sleep(@time_period - delta) if delta < @time_period
|
17
|
+
self.current_timestamp = Time.now
|
18
|
+
end
|
19
|
+
|
20
|
+
end
|
21
|
+
|
22
|
+
end
|
23
|
+
|
24
|
+
|
@@ -0,0 +1,26 @@
|
|
1
|
+
module GluttonRatelimit
|
2
|
+
|
3
|
+
class BurstyTokenBucket < ParentLimiter
|
4
|
+
|
5
|
+
def reset_bucket
|
6
|
+
@oldest_timestamp = Time.now
|
7
|
+
@tokens = @executions
|
8
|
+
end
|
9
|
+
|
10
|
+
def wait
|
11
|
+
reset_bucket if @tokens.nil?
|
12
|
+
|
13
|
+
if @tokens.zero?
|
14
|
+
delta = Time.now - @oldest_timestamp
|
15
|
+
sleep(@time_period - delta) if delta < @time_period
|
16
|
+
reset_bucket
|
17
|
+
end
|
18
|
+
|
19
|
+
@tokens -= 1
|
20
|
+
end
|
21
|
+
|
22
|
+
end
|
23
|
+
|
24
|
+
end
|
25
|
+
|
26
|
+
|
data/test/helper.rb
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'test/unit'
|
3
|
+
|
4
|
+
$LOAD_PATH.unshift(File.dirname(__FILE__))
|
5
|
+
$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
|
6
|
+
require 'glutton_ratelimit'
|
7
|
+
|
8
|
+
class Test::Unit::TestCase
|
9
|
+
end
|
10
|
+
|
11
|
+
def timed_run ratelimiter, passes = 1
|
12
|
+
before_last_invocation = 0
|
13
|
+
start_time = Time.now
|
14
|
+
(passes * ratelimiter.executions + 1).times do |n|
|
15
|
+
ratelimiter.wait
|
16
|
+
before_last_invocation = Time.now
|
17
|
+
yield
|
18
|
+
end
|
19
|
+
before_last_invocation - start_time
|
20
|
+
end
|
21
|
+
|
@@ -0,0 +1,15 @@
|
|
1
|
+
require 'helper'
|
2
|
+
require 'testing_module.rb'
|
3
|
+
|
4
|
+
class TestGluttonRatelimitAveragedThrottle < Test::Unit::TestCase
|
5
|
+
include TestingModule
|
6
|
+
|
7
|
+
def setup
|
8
|
+
@testClass = GluttonRatelimit::AveragedThrottle
|
9
|
+
end
|
10
|
+
|
11
|
+
def test_object_creations
|
12
|
+
rl = @testClass.new 1, 1
|
13
|
+
assert_instance_of GluttonRatelimit::AveragedThrottle, rl
|
14
|
+
end
|
15
|
+
end
|
@@ -0,0 +1,15 @@
|
|
1
|
+
require 'helper'
|
2
|
+
require 'testing_module.rb'
|
3
|
+
|
4
|
+
class TestGluttonRatelimitBurstyRingBuffer < Test::Unit::TestCase
|
5
|
+
include TestingModule
|
6
|
+
|
7
|
+
def setup
|
8
|
+
@testClass = GluttonRatelimit::BurstyRingBuffer
|
9
|
+
end
|
10
|
+
|
11
|
+
def test_object_creations
|
12
|
+
rl = @testClass.new 1, 1
|
13
|
+
assert_instance_of GluttonRatelimit::BurstyRingBuffer, rl
|
14
|
+
end
|
15
|
+
end
|
@@ -0,0 +1,15 @@
|
|
1
|
+
require 'helper'
|
2
|
+
require 'testing_module.rb'
|
3
|
+
|
4
|
+
class TestGluttonRatelimitBurtyTokenBucket < Test::Unit::TestCase
|
5
|
+
include TestingModule
|
6
|
+
|
7
|
+
def setup
|
8
|
+
@testClass = GluttonRatelimit::BurstyTokenBucket
|
9
|
+
end
|
10
|
+
|
11
|
+
def test_object_creations
|
12
|
+
rl = @testClass.new 1, 1
|
13
|
+
assert_instance_of GluttonRatelimit::BurstyTokenBucket, rl
|
14
|
+
end
|
15
|
+
end
|
@@ -0,0 +1,67 @@
|
|
1
|
+
module TestingModule
|
2
|
+
# This module assumes use as a mixin within
|
3
|
+
# a test class which defines @testClass.
|
4
|
+
|
5
|
+
def test_120_tasks_every_second_with_ms_task
|
6
|
+
min_time = 1
|
7
|
+
rl = @testClass.new 120, min_time
|
8
|
+
delta = timed_run(rl) { sleep 0.001 }
|
9
|
+
#puts "#{delta} >? #{min_time}"
|
10
|
+
assert delta > min_time
|
11
|
+
end
|
12
|
+
|
13
|
+
def test_120_tasks_every_quarter_second_with_ms_task
|
14
|
+
min_time = 0.25
|
15
|
+
rl = @testClass.new 120, min_time
|
16
|
+
delta = timed_run(rl) { sleep 0.001 }
|
17
|
+
#puts "#{delta} >? #{min_time}"
|
18
|
+
assert delta > min_time
|
19
|
+
end
|
20
|
+
|
21
|
+
def test_120_tasks_every_second_with_cenisecond_task
|
22
|
+
min_time = 1
|
23
|
+
rl = @testClass.new 120, min_time
|
24
|
+
delta = timed_run(rl) { sleep 0.01 }
|
25
|
+
#puts "#{delta} >? #{min_time}"
|
26
|
+
assert delta > min_time
|
27
|
+
end
|
28
|
+
|
29
|
+
def test_50_tasks_every_second_with_fast_task
|
30
|
+
min_time = 1
|
31
|
+
rl = @testClass.new 50, min_time
|
32
|
+
delta = timed_run(rl) { sleep 0 }
|
33
|
+
#puts "#{delta} >? #{min_time}"
|
34
|
+
assert delta > min_time
|
35
|
+
end
|
36
|
+
|
37
|
+
def test_1_task_every_1_seconds_with_1_second_task
|
38
|
+
min_time = 1
|
39
|
+
rl = @testClass.new 1, min_time
|
40
|
+
delta = timed_run(rl) { sleep 1 }
|
41
|
+
#puts "#{delta} >? #{min_time}"
|
42
|
+
assert delta > min_time
|
43
|
+
end
|
44
|
+
|
45
|
+
def test_5_tasks_every_quarter_second_four_runs_with_ms_task
|
46
|
+
min_time = 0.25
|
47
|
+
rl = @testClass.new 1, min_time
|
48
|
+
delta = timed_run(rl, 4) { sleep 0.001 }
|
49
|
+
#puts "#{delta} >? #{min_time}"
|
50
|
+
assert delta > min_time * 4
|
51
|
+
end
|
52
|
+
|
53
|
+
def test_120_tasks_every_half_second_two_runs_with_no_task
|
54
|
+
min_time = 0.5
|
55
|
+
rl = @testClass.new 120, min_time
|
56
|
+
delta = timed_run(rl, 2) { sleep 0 }
|
57
|
+
assert delta > min_time * 2
|
58
|
+
end
|
59
|
+
|
60
|
+
def test_10_tasks_every_1_seconds_two_runs_with_short_rnd_task
|
61
|
+
min_time = 1
|
62
|
+
rl = @testClass.new 10, min_time
|
63
|
+
delta = timed_run(rl, 2) { sleep(0.2 * rand) }
|
64
|
+
assert delta > min_time * 2
|
65
|
+
end
|
66
|
+
|
67
|
+
end
|
metadata
ADDED
@@ -0,0 +1,86 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: glutton_ratelimit
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
prerelease: false
|
5
|
+
segments:
|
6
|
+
- 0
|
7
|
+
- 1
|
8
|
+
- 0
|
9
|
+
version: 0.1.0
|
10
|
+
platform: ruby
|
11
|
+
authors:
|
12
|
+
- Wally Glutton
|
13
|
+
autorequire:
|
14
|
+
bindir: bin
|
15
|
+
cert_chain: []
|
16
|
+
|
17
|
+
date: 2010-06-10 00:00:00 -05:00
|
18
|
+
default_executable:
|
19
|
+
dependencies: []
|
20
|
+
|
21
|
+
description: A Ruby library for limiting the number of times a method can be invoked within a specified time period.
|
22
|
+
email: stungeye@gmail.com
|
23
|
+
executables: []
|
24
|
+
|
25
|
+
extensions: []
|
26
|
+
|
27
|
+
extra_rdoc_files:
|
28
|
+
- LICENSE
|
29
|
+
- README.rdoc
|
30
|
+
files:
|
31
|
+
- .document
|
32
|
+
- .gitignore
|
33
|
+
- LICENSE
|
34
|
+
- README.rdoc
|
35
|
+
- Rakefile
|
36
|
+
- VERSION
|
37
|
+
- examples/limit_instance_methods.rb
|
38
|
+
- examples/simple_manual.rb
|
39
|
+
- glutton_ratelimit.gemspec
|
40
|
+
- lib/glutton_ratelimit.rb
|
41
|
+
- lib/glutton_ratelimit/averaged_throttle.rb
|
42
|
+
- lib/glutton_ratelimit/bursty_ring_buffer.rb
|
43
|
+
- lib/glutton_ratelimit/bursty_token_bucket.rb
|
44
|
+
- test/helper.rb
|
45
|
+
- test/test_glutton_ratelimit_averaged_throttle.rb
|
46
|
+
- test/test_glutton_ratelimit_bursty_ring_buffer.rb
|
47
|
+
- test/test_glutton_ratelimit_bursty_token_bucket.rb
|
48
|
+
- test/testing_module.rb
|
49
|
+
has_rdoc: true
|
50
|
+
homepage: http://github.com/stungeye/glutton_ratelimit
|
51
|
+
licenses: []
|
52
|
+
|
53
|
+
post_install_message:
|
54
|
+
rdoc_options:
|
55
|
+
- --charset=UTF-8
|
56
|
+
require_paths:
|
57
|
+
- lib
|
58
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
59
|
+
requirements:
|
60
|
+
- - ">="
|
61
|
+
- !ruby/object:Gem::Version
|
62
|
+
segments:
|
63
|
+
- 0
|
64
|
+
version: "0"
|
65
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
66
|
+
requirements:
|
67
|
+
- - ">="
|
68
|
+
- !ruby/object:Gem::Version
|
69
|
+
segments:
|
70
|
+
- 0
|
71
|
+
version: "0"
|
72
|
+
requirements: []
|
73
|
+
|
74
|
+
rubyforge_project:
|
75
|
+
rubygems_version: 1.3.6
|
76
|
+
signing_key:
|
77
|
+
specification_version: 3
|
78
|
+
summary: Simple Ruby library for self-imposed rater-limiting.
|
79
|
+
test_files:
|
80
|
+
- test/test_glutton_ratelimit_averaged_throttle.rb
|
81
|
+
- test/helper.rb
|
82
|
+
- test/test_glutton_ratelimit_bursty_ring_buffer.rb
|
83
|
+
- test/testing_module.rb
|
84
|
+
- test/test_glutton_ratelimit_bursty_token_bucket.rb
|
85
|
+
- examples/limit_instance_methods.rb
|
86
|
+
- examples/simple_manual.rb
|