glutton_ratelimit 0.2.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 +7 -0
- data/.document +5 -5
- data/.github/workflows/ruby.yml +20 -0
- data/.ruby-version +1 -0
- data/.travis.yml +9 -0
- data/Gemfile +4 -0
- data/Gemfile.lock +63 -0
- data/LICENSE +23 -23
- data/README.rdoc +156 -130
- data/Rakefile +50 -52
- data/VERSION +1 -1
- data/examples/limit_instance_methods.rb +33 -33
- data/examples/simple_manual.rb +31 -31
- data/glutton_ratelimit.gemspec +64 -64
- data/lib/glutton_ratelimit.rb +39 -38
- data/lib/glutton_ratelimit/averaged_throttle.rb +45 -44
- data/lib/glutton_ratelimit/bursty_ring_buffer.rb +24 -24
- data/lib/glutton_ratelimit/bursty_token_bucket.rb +30 -26
- data/test/helper.rb +20 -20
- data/test/test_glutton_ratelimit_averaged_throttle.rb +15 -15
- data/test/test_glutton_ratelimit_bursty_ring_buffer.rb +15 -15
- data/test/test_glutton_ratelimit_bursty_token_bucket.rb +15 -15
- data/test/testing_module.rb +73 -69
- metadata +56 -43
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: b8a1b9b8b47cbcc4573a67ec3af28dad39ed240e61e85729d2773645cb109eef
|
4
|
+
data.tar.gz: c56bf652f9348c46aa2d45f94d1159316a94a8d37fad53aea15eae4fa00e41bd
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 61ff459ec83219910e727aaa837347bdb39604f23aa0bf49be2c19ad9b6ab15dbae60d2ea6e3268ec4a272fb411f24c29957ce95cc299e210662d7246ce9b691
|
7
|
+
data.tar.gz: e56a2767ff91f37b28990f6e54ff977481cf0eccbca5da99c345151fd254046e635dd52ab201d2a18a39b828b18f047b0e5f1ee944d4585b2cbaddcd3312f1c7
|
data/.document
CHANGED
@@ -1,5 +1,5 @@
|
|
1
|
-
README.rdoc
|
2
|
-
lib/**/*.rb
|
3
|
-
bin/*
|
4
|
-
features/**/*.feature
|
5
|
-
LICENSE
|
1
|
+
README.rdoc
|
2
|
+
lib/**/*.rb
|
3
|
+
bin/*
|
4
|
+
features/**/*.feature
|
5
|
+
LICENSE
|
@@ -0,0 +1,20 @@
|
|
1
|
+
name: Ruby
|
2
|
+
|
3
|
+
on: [push]
|
4
|
+
|
5
|
+
jobs:
|
6
|
+
build:
|
7
|
+
|
8
|
+
runs-on: ubuntu-latest
|
9
|
+
|
10
|
+
steps:
|
11
|
+
- uses: actions/checkout@v1
|
12
|
+
- name: Set up Ruby 2.6
|
13
|
+
uses: actions/setup-ruby@v1
|
14
|
+
with:
|
15
|
+
ruby-version: 2.6.x
|
16
|
+
- name: Build and test with Rake
|
17
|
+
run: |
|
18
|
+
gem install bundler
|
19
|
+
bundle install --jobs 4 --retry 3
|
20
|
+
bundle exec rake
|
data/.ruby-version
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
2.5.7
|
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
@@ -0,0 +1,63 @@
|
|
1
|
+
GEM
|
2
|
+
remote: https://rubygems.org/
|
3
|
+
specs:
|
4
|
+
addressable (2.4.0)
|
5
|
+
builder (3.2.4)
|
6
|
+
descendants_tracker (0.0.4)
|
7
|
+
thread_safe (~> 0.3, >= 0.3.1)
|
8
|
+
faraday (0.9.2)
|
9
|
+
multipart-post (>= 1.2, < 3)
|
10
|
+
git (1.5.0)
|
11
|
+
github_api (0.16.0)
|
12
|
+
addressable (~> 2.4.0)
|
13
|
+
descendants_tracker (~> 0.0.4)
|
14
|
+
faraday (~> 0.8, < 0.10)
|
15
|
+
hashie (>= 3.4)
|
16
|
+
mime-types (>= 1.16, < 3.0)
|
17
|
+
oauth2 (~> 1.0)
|
18
|
+
hashie (4.0.0)
|
19
|
+
highline (2.0.3)
|
20
|
+
jeweler (2.3.9)
|
21
|
+
builder
|
22
|
+
bundler
|
23
|
+
git (>= 1.2.5)
|
24
|
+
github_api (~> 0.16.0)
|
25
|
+
highline (>= 1.6.15)
|
26
|
+
nokogiri (>= 1.5.10)
|
27
|
+
psych
|
28
|
+
rake
|
29
|
+
rdoc
|
30
|
+
semver2
|
31
|
+
jwt (2.2.1)
|
32
|
+
mime-types (2.99.3)
|
33
|
+
mini_portile2 (2.4.0)
|
34
|
+
multi_json (1.14.1)
|
35
|
+
multi_xml (0.6.0)
|
36
|
+
multipart-post (2.1.1)
|
37
|
+
nokogiri (1.10.7)
|
38
|
+
mini_portile2 (~> 2.4.0)
|
39
|
+
oauth2 (1.4.2)
|
40
|
+
faraday (>= 0.8, < 2.0)
|
41
|
+
jwt (>= 1.0, < 3.0)
|
42
|
+
multi_json (~> 1.3)
|
43
|
+
multi_xml (~> 0.5)
|
44
|
+
rack (>= 1.2, < 3)
|
45
|
+
power_assert (1.1.5)
|
46
|
+
psych (3.1.0)
|
47
|
+
rack (2.0.8)
|
48
|
+
rake (13.0.1)
|
49
|
+
rdoc (6.2.0)
|
50
|
+
semver2 (3.4.2)
|
51
|
+
test-unit (3.3.4)
|
52
|
+
power_assert
|
53
|
+
thread_safe (0.3.6)
|
54
|
+
|
55
|
+
PLATFORMS
|
56
|
+
ruby
|
57
|
+
|
58
|
+
DEPENDENCIES
|
59
|
+
jeweler
|
60
|
+
test-unit
|
61
|
+
|
62
|
+
BUNDLED WITH
|
63
|
+
2.1.1
|
data/LICENSE
CHANGED
@@ -1,24 +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
|
-
|
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
24
|
For more information, please refer to <http://unlicense.org/>
|
data/README.rdoc
CHANGED
@@ -1,130 +1,156 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
==
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
=== Average Throttle
|
40
|
-
|
41
|
-
If executions are limited to n times per m seconds then:
|
42
|
-
|
43
|
-
1. The first execution will occur immediately.
|
44
|
-
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.
|
45
|
-
3. The process is repeated.
|
46
|
-
|
47
|
-
The amount of time slept before each execution will depend on the time period m and the average elapsed time of each execution.
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
|
98
|
-
|
99
|
-
|
100
|
-
|
101
|
-
|
102
|
-
|
103
|
-
|
104
|
-
|
105
|
-
|
106
|
-
|
107
|
-
|
108
|
-
|
109
|
-
|
110
|
-
|
111
|
-
|
112
|
-
|
113
|
-
|
114
|
-
|
115
|
-
|
116
|
-
|
117
|
-
|
118
|
-
|
119
|
-
|
120
|
-
|
121
|
-
==
|
122
|
-
|
123
|
-
|
124
|
-
|
125
|
-
|
126
|
-
|
127
|
-
|
128
|
-
|
129
|
-
|
130
|
-
|
1
|
+
*NOTE:* This library is no longer under active development. Pull requests are, however, still being accepted.
|
2
|
+
|
3
|
+
Shopify maintains a gem with similar functionality called {ruby-limiter}[https://github.com/Shopify/limiter].
|
4
|
+
|
5
|
+
== glutton_ratelimit
|
6
|
+
|
7
|
+
A Ruby library for limiting the number of times a method can be invoked within a specified time period.
|
8
|
+
|
9
|
+
The rate-limiting is simple if somewhat naïve. Throttled methods are blocked using sleep.
|
10
|
+
|
11
|
+
The 1.x.x version of the library is designed to work with Ruby 2.4+. Please use version 0.2.0 if you need to support an ealier version of Ruby.
|
12
|
+
|
13
|
+
== Use Cases
|
14
|
+
|
15
|
+
You might wish to use this library to throttle code that:
|
16
|
+
|
17
|
+
* Accesses a 3rd-party API that imposes a maximum rate-limit.
|
18
|
+
* Scrapes data from any website where rapid repeated access is banned.
|
19
|
+
|
20
|
+
For example, EchoNest API requests need to be limited to 120 every minute.
|
21
|
+
|
22
|
+
== Installation
|
23
|
+
|
24
|
+
gem install glutton_ratelimit
|
25
|
+
|
26
|
+
Or add the gem to your project Gemfile:
|
27
|
+
|
28
|
+
gem 'glutton_ratelimit'
|
29
|
+
|
30
|
+
If you need to use this gem with Ruby version 2.3 or ealier, use version 0.2.0 of the gem.
|
31
|
+
|
32
|
+
== Types of Limiting
|
33
|
+
|
34
|
+
There are two types of rate limiting provided by this library:
|
35
|
+
|
36
|
+
* Average Throttle Limiting (GluttonRatelimit::AveragedThrottle) DEFAULT
|
37
|
+
* Bursty Token Bucket Limiting (GluttonRatelimit::BurstyTokenBucket)
|
38
|
+
|
39
|
+
=== Average Throttle (Default Mode)
|
40
|
+
|
41
|
+
If executions are limited to n times per m seconds then:
|
42
|
+
|
43
|
+
1. The first execution will occur immediately.
|
44
|
+
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.
|
45
|
+
3. The process is repeated.
|
46
|
+
|
47
|
+
The amount of time slept before each execution will depend on the time period m and the average elapsed time of each execution.
|
48
|
+
|
49
|
+
=== Bursty Token Bucket
|
50
|
+
|
51
|
+
If executions are limited to n times per m seconds then:
|
52
|
+
|
53
|
+
1. n executions will be allowed immediately.
|
54
|
+
2. Before the next execution the process will sleep for the remainder of the m second time period.
|
55
|
+
3. The process is repeated.
|
56
|
+
|
57
|
+
The amount of time slept in step 2 will depend on how long the n executions took in step 1.
|
58
|
+
|
59
|
+
== Instance Method Limiting Example
|
60
|
+
|
61
|
+
The rate_limit method can be used at the end of a class definition to limit specific instance methods.
|
62
|
+
|
63
|
+
rate_limit :name_of_instance_method, number_of_executions, timeframe_in_seconds
|
64
|
+
|
65
|
+
The specified instance method will be throlled to maintain an execution rate of:
|
66
|
+
|
67
|
+
timeframe_in_seconds / number_of_executions
|
68
|
+
|
69
|
+
==== An example where the limit_me instance method of the RateLimitTest class is throttled:
|
70
|
+
|
71
|
+
class RateLimitTest
|
72
|
+
# The class must be extended to permit limiting.
|
73
|
+
extend GluttonRatelimit
|
74
|
+
|
75
|
+
def initialize
|
76
|
+
@start = Time.now
|
77
|
+
end
|
78
|
+
|
79
|
+
def limit_me
|
80
|
+
puts "#{Time.now - @start}"
|
81
|
+
end
|
82
|
+
|
83
|
+
# Throttle the limit_me method to five executions every sixty seconds.
|
84
|
+
rate_limit :limit_me, 5, 60
|
85
|
+
end
|
86
|
+
|
87
|
+
t = RateLimitTest.new
|
88
|
+
|
89
|
+
10.times { t.limit_me }
|
90
|
+
|
91
|
+
When using the rate_limit method the limiting defaults to GluttonRatelimit::AveragedThrottle. Token Bucket limiting can also be specified:
|
92
|
+
|
93
|
+
rate_limite :limit_me, 5, 60, GluttonRatelimit::BurstyTokenBucket
|
94
|
+
|
95
|
+
== Simple Manual Limiting Example
|
96
|
+
|
97
|
+
Chunks of code can also be manually throttled using the times method of a specific GluttonRatelimit object.
|
98
|
+
|
99
|
+
# Maximum of twelve executions every five seconds.
|
100
|
+
rl = GluttonRatelimit::BurstyTokenBucket.new 12, 5
|
101
|
+
# BurstyTokenBucket will allow for a full burst of executions followed by a pause.
|
102
|
+
|
103
|
+
rl.times(25) do
|
104
|
+
# Simulating a constant-time task:
|
105
|
+
sleep 0.1
|
106
|
+
end
|
107
|
+
|
108
|
+
# Maximum of six executions every six seconds.
|
109
|
+
rl = GluttonRatelimit::AveragedThrottle.new 6, 6
|
110
|
+
# AverageThrottle will attempt to evenly space executions within the allowed period.
|
111
|
+
|
112
|
+
rl.times(13) do
|
113
|
+
# Simulating a 0 to 1 second random-time task:
|
114
|
+
sleep rand
|
115
|
+
end
|
116
|
+
|
117
|
+
== More Examples
|
118
|
+
|
119
|
+
More examples can be found within the examples folder.
|
120
|
+
|
121
|
+
== Warnings
|
122
|
+
|
123
|
+
Before using the library you should be aware of the following warnings.
|
124
|
+
|
125
|
+
=== Short Tasks and AveragedThrottle
|
126
|
+
|
127
|
+
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).
|
128
|
+
|
129
|
+
See: http://codeidol.com/other/rubyckbk/Date-and-Time/Waiting-a-Certain-Amount-of-Time
|
130
|
+
|
131
|
+
It is recommend that you use the BurstyTokenBucket limiting when throttling very short tasks.
|
132
|
+
|
133
|
+
=== Naive Throttling
|
134
|
+
|
135
|
+
As mentioned above, throttling is accomplish by blocking script execution using sleep. There is no support for dropping or queuing throttled method invocations.
|
136
|
+
|
137
|
+
This library is not thread safe.
|
138
|
+
|
139
|
+
== Tests
|
140
|
+
|
141
|
+
The tests integration/timing tests with a tolerance of 0.5 of a percent outside the expected timings.
|
142
|
+
|
143
|
+
The tests can be run using:
|
144
|
+
|
145
|
+
bundle exec rake test
|
146
|
+
|
147
|
+
== Thanks
|
148
|
+
|
149
|
+
Some of the algorithms were inspired by these discussions:
|
150
|
+
|
151
|
+
* http://stackoverflow.com/questions/667508/whats-a-good-rate-limiting-algorithm
|
152
|
+
* http://stackoverflow.com/questions/1407113/throttling-method-calls-to-m-requests-in-n-seconds
|
153
|
+
|
154
|
+
== License
|
155
|
+
|
156
|
+
This is free and unencumbered software released into the public domain. See LICENSE for details.
|