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.
@@ -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
@@ -0,0 +1 @@
1
+ 2.5.7
@@ -0,0 +1,9 @@
1
+ language: ruby
2
+ rvm:
3
+ - 1.9
4
+ - 2.0
5
+ - 2.1
6
+ - 2.2
7
+ - 2.3
8
+ - 2.4
9
+ - 2.5
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gem 'jeweler'
4
+ gem 'test-unit'
@@ -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/>
@@ -1,130 +1,156 @@
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
- == Installation
17
-
18
- sudo gem install glutton_ratelimit
19
-
20
- The gem is hosted at: http://rubygems.org/gems/glutton_ratelimit
21
-
22
- == Types of Limiting
23
-
24
- There are two types of rate limiting provided by this library:
25
-
26
- * Bursty Token Bucket Limiting (GluttonRatelimit::BurstyTokenBucket)
27
- * Average Throttle Limiting (GluttonRatelimit::AveragedThrottle)
28
-
29
- === Bursty Token Bucket
30
-
31
- If executions are limited to n times per m seconds then:
32
-
33
- 1. n executions will be allowed immediately.
34
- 2. Before the next execution the process will sleep for the remainder of the m second time period.
35
- 3. The process is repeated.
36
-
37
- The amount of time slept in step 2 will depend on how long the n executions took in step 1.
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
- == Instance Method Limiting Example
50
-
51
- The rate_limit method can be used at the end of a class definition to limit specific instance methods.
52
-
53
- class LimitInstanceMethod
54
- # The class must be extended to permit limiting.
55
- extend GluttonRatelimit
56
-
57
- def initialize
58
- @start = Time.now
59
- end
60
-
61
- def limit_me
62
- puts "#{Time.now - @start}"
63
- end
64
-
65
- # Throttle the limit_me method to six executions every six seconds.
66
- rate_limit :limit_me, 6, 6
67
- end
68
-
69
- t = LimitInstanceMethod.new
70
-
71
- 10.times { t.limit_me }
72
-
73
- When using the rate_limit method the limiting defaults to GluttonRatelimit::AveragedThrottle. Token Bucket limiting can also be specified:
74
-
75
- rate_limite :limit_me, 6, 6, GluttonRatelimit::BurstyTokenBucket
76
-
77
- == Simple Manual Limiting Example
78
-
79
- Chunks of code can also be manually throttled using the wait method of a specific GluttonRatelimit object.
80
-
81
- # Maximum of twelve executions every five seconds.
82
- rl = GluttonRatelimit::BurstyTokenBucket.new 12, 5
83
- # BurstyTokenBucket will allow for a full burst of executions followed by a pause.
84
-
85
- rl.times(25) do
86
- # Simulating a constant-time task:
87
- sleep 0.1
88
- end
89
-
90
- # Maximum of six executions every six seconds.
91
- rl = GluttonRatelimit::AveragedThrottle.new 6, 6
92
- # AverageThrottle will attempt to evenly space executions within the allowed period.
93
-
94
- rl.times(13) do
95
- # Simulating a 0 to 1 second random-time task:
96
- sleep rand
97
- end
98
-
99
- == More Examples
100
-
101
- More examples can be found within the examples folder.
102
-
103
- == Warnings
104
-
105
- Before using the library you should be aware of the following warnings.
106
-
107
- === Short Tasks and AveragedThrottle
108
-
109
- 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).
110
-
111
- See: http://codeidol.com/other/rubyckbk/Date-and-Time/Waiting-a-Certain-Amount-of-Time
112
-
113
- It is recommend that you use the BurstyTokenBucket limiting when throttling very short tasks.
114
-
115
- === Naive Throttling
116
-
117
- As mentioned above, throttling is accomplish by blocking script execution using sleep. There is no support for dropping or queuing throttled method invocations.
118
-
119
- This library is not thread safe.
120
-
121
- == Thanks
122
-
123
- Some of the algorithms were inspired by these discussions:
124
-
125
- * http://stackoverflow.com/questions/667508/whats-a-good-rate-limiting-algorithm
126
- * http://stackoverflow.com/questions/1407113/throttling-method-calls-to-m-requests-in-n-seconds
127
-
128
- == License
129
-
130
- This is free and unencumbered software released into the public domain. See LICENSE for details.
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.