improved-rack-throttle-w-expiry 0.8.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 ADDED
@@ -0,0 +1,5 @@
1
+ lib/**/*.rb
2
+ bin/*
3
+ -
4
+ features/**/*.feature
5
+ LICENSE.txt
data/Gemfile ADDED
@@ -0,0 +1,16 @@
1
+ source "http://rubygems.org"
2
+
3
+ gem "rack", ">= 1.0.0"
4
+
5
+ group :development, :test do
6
+ gem 'redis'
7
+ gem 'debugger'
8
+ gem 'timecop'
9
+ gem 'rack-test'
10
+ gem 'rspec'
11
+ gem 'yard'
12
+ gem "simplecov", :require => false
13
+ gem 'redcarpet'
14
+ gem 'rake'
15
+ gem 'jeweler'
16
+ end
data/Gemfile.lock ADDED
@@ -0,0 +1,57 @@
1
+ GEM
2
+ remote: http://rubygems.org/
3
+ specs:
4
+ columnize (0.3.6)
5
+ debugger (1.6.0)
6
+ columnize (>= 0.3.1)
7
+ debugger-linecache (~> 1.2.0)
8
+ debugger-ruby_core_source (~> 1.2.1)
9
+ debugger-linecache (1.2.0)
10
+ debugger-ruby_core_source (1.2.2)
11
+ diff-lcs (1.2.4)
12
+ git (1.2.5)
13
+ jeweler (1.8.4)
14
+ bundler (~> 1.0)
15
+ git (>= 1.2.5)
16
+ rake
17
+ rdoc
18
+ json (1.8.0)
19
+ multi_json (1.7.6)
20
+ rack (1.5.2)
21
+ rack-test (0.6.2)
22
+ rack (>= 1.0)
23
+ rake (10.0.4)
24
+ rdoc (4.0.1)
25
+ json (~> 1.4)
26
+ redcarpet (2.3.0)
27
+ redis (3.0.4)
28
+ rspec (2.13.0)
29
+ rspec-core (~> 2.13.0)
30
+ rspec-expectations (~> 2.13.0)
31
+ rspec-mocks (~> 2.13.0)
32
+ rspec-core (2.13.1)
33
+ rspec-expectations (2.13.0)
34
+ diff-lcs (>= 1.1.3, < 2.0)
35
+ rspec-mocks (2.13.1)
36
+ simplecov (0.7.1)
37
+ multi_json (~> 1.0)
38
+ simplecov-html (~> 0.7.1)
39
+ simplecov-html (0.7.1)
40
+ timecop (0.6.1)
41
+ yard (0.8.6.1)
42
+
43
+ PLATFORMS
44
+ ruby
45
+
46
+ DEPENDENCIES
47
+ debugger
48
+ jeweler
49
+ rack (>= 1.0.0)
50
+ rack-test
51
+ rake
52
+ redcarpet
53
+ redis
54
+ rspec
55
+ simplecov
56
+ timecop
57
+ yard
data/README.md ADDED
@@ -0,0 +1,247 @@
1
+ HTTP Request Rate Limiter for Rack Applications
2
+ ===============================================
3
+
4
+ This is a [Rack][] middleware that provides logic for rate-limiting incoming
5
+ HTTP requests to Rack applications. You can use `Rack::Throttle` with any
6
+ Ruby web framework based on Rack, including with Ruby on Rails 3.0 and with
7
+ Sinatra.
8
+
9
+ * <https://github.com/rooktone/improved-rack-throttle-w-expiry>
10
+
11
+ Features
12
+ --------
13
+
14
+ * Throttles a Rack application by enforcing a minimum time interval between
15
+ subsequent HTTP requests from a particular client, as well as by defining
16
+ a maximum number of allowed HTTP requests per a given time period (hourly
17
+ or daily).
18
+ * Scopes throttling rules by request path, http method, or user_agent
19
+ for applications that need a variety of rules
20
+ * Compatible with any Rack application and any Rack-based framework.
21
+ * Stores rate-limiting counters in any key/value store implementation that
22
+ responds to `#[]`/`#[]=` (like Ruby's hashes) or to `#get`/`#set` (like
23
+ memcached or Redis). This makes it easy to use global counters across
24
+ multiple web servers.
25
+ * Compatible with the [gdbm][] binding included in Ruby's standard library.
26
+ * Compatible with the [memcached][], [memcache-client][], [memcache][] and
27
+ [redis][] gems.
28
+ * Compatible with [Heroku][]'s [memcached add-on][Heroku memcache]
29
+ * THE BIG DIFFERENCE: Comes with redis expiry built in. No more relying on LRU. The cache keys won't hang around forever while killing all scalability.
30
+
31
+ Examples
32
+ --------
33
+
34
+ ### Adding throttling to a Rails 3.x application
35
+
36
+ # config/application.rb
37
+ require 'rack/throttle'
38
+
39
+ class Application < Rails::Application
40
+ config.middleware.use Rack::Throttle::Interval
41
+ end
42
+
43
+ ### Adding throttling to a Sinatra application
44
+
45
+ #!/usr/bin/env ruby -rubygems
46
+ require 'sinatra'
47
+ require 'rack/throttle'
48
+
49
+ use Rack::Throttle::Interval
50
+
51
+ get('/hello') { "Hello, world!\n" }
52
+
53
+ ### Adding throttling to a Rackup application
54
+
55
+ #!/usr/bin/env rackup
56
+ require 'rack/throttle'
57
+
58
+ use Rack::Throttle::Interval
59
+
60
+ run lambda { |env| [200, {'Content-Type' => 'text/plain'}, "Hello, world!\n"] }
61
+
62
+ ### Enforcing a minimum 3-second interval between requests
63
+
64
+ use Rack::Throttle::Interval, :min => 3.0
65
+
66
+ ### Allowing a maximum of 100 requests per hour
67
+
68
+ use Rack::Throttle::Hourly, :max => 100
69
+
70
+ ### Allowing a maximum of 1,000 requests per day
71
+
72
+ use Rack::Throttle::Daily, :max => 1000
73
+
74
+ ### Allowing 1 request per second, with bursts of up to 5 requests
75
+
76
+ use Rack::Throttle::SlidingWindow, :average => 1, :burst => 5
77
+
78
+ ### Combining various throttling constraints into one overall policy
79
+
80
+ use Rack::Throttle::Daily, :max => 1000 # requests
81
+ use Rack::Throttle::Hourly, :max => 100 # requests
82
+ use Rack::Throttle::Interval, :min => 3.0 # seconds
83
+
84
+ ### Storing the rate-limiting counters in a GDBM database
85
+
86
+ require 'gdbm'
87
+
88
+ use Rack::Throttle::Interval, :cache => GDBM.new('tmp/throttle.db')
89
+
90
+ ### Storing the rate-limiting counters on a Memcached server
91
+
92
+ require 'memcached'
93
+
94
+ use Rack::Throttle::Interval, :cache => Memcached.new, :key_prefix => :throttle
95
+
96
+ ### Storing the rate-limiting counters on a Redis server
97
+
98
+ require 'redis'
99
+
100
+ use Rack::Throttle::Interval, :cache => Redis.new, :key_prefix => :throttle
101
+
102
+ ### Scoping the rate-limit to a specific path and method
103
+
104
+ use Rack::Throttle::Interval, :rules => {:url => /api/, :method => :post}
105
+
106
+ Throttling Strategies
107
+ ---------------------
108
+
109
+ `Rack::Throttle` supports four built-in throttling strategies:
110
+
111
+ * `Rack::Throttle::Interval`: Throttles the application by enforcing a
112
+ minimum interval (by default, 1 second) between subsequent HTTP requests.
113
+ * `Rack::Throttle::Hourly`: Throttles the application by defining a
114
+ maximum number of allowed HTTP requests per hour (by default, 3,600
115
+ requests per 60 minutes, which works out to an average of 1 request per
116
+ second).
117
+ * `Rack::Throttle::Daily`: Throttles the application by defining a
118
+ maximum number of allowed HTTP requests per day (by default, 86,400
119
+ requests per 24 hours, which works out to an average of 1 request per
120
+ second).
121
+ * `Rack::Throttle::SlidingWindow`: Throttles the application by defining
122
+ an average request rate, and a burst allowance that clients can hit
123
+ (as long as they stay within the average). By default, this is 1
124
+ request per second, with bursts of up to 5 requests at a time. Users
125
+ who exceed the average and who have used up their burst will have all
126
+ of their requests denied until they comply with the policy.
127
+
128
+ You can fully customize the implementation details of any of these strategies
129
+ by simply subclassing one of the aforementioned default implementations.
130
+ And, of course, should your application-specific requirements be
131
+ significantly more complex than what we've provided for, you can also define
132
+ entirely new kinds of throttling strategies by subclassing the
133
+ `Rack::Throttle::Limiter` base class directly.
134
+
135
+ Scoping Rules
136
+ -------------
137
+ Rack::Throttle ships with a Rack::Throttle::Matcher base class, and three
138
+ implementations. Rack::Throttle::UrlMatcher and
139
+ Rack::Throttle::UserAgentMatcher allow you to pass in regular expressions
140
+ for request path and user agent, while Rack::Throttle::MethodMatcher
141
+ will filter by request method when passed a Symbol :get, :post, :put, or
142
+ :delete. These rules are additive, so you can throttle just POST
143
+ requests to your '/login' page, for example.
144
+
145
+
146
+ HTTP Client Identification
147
+ --------------------------
148
+
149
+ The rate-limiting counters stored and maintained by `Rack::Throttle` are
150
+ keyed to unique HTTP clients.
151
+
152
+ By default, HTTP clients are uniquely identified by their IP address as
153
+ returned by `Rack::Request#ip`. If you wish to instead use a more granular,
154
+ application-specific identifier such as a session key or a user account
155
+ name, you can subclass a throttling strategy implementation and
156
+ override the `#client_identifier` method.
157
+
158
+ HTTP Response Codes and Headers
159
+ -------------------------------
160
+
161
+ ### 403 Forbidden (Rate Limit Exceeded)
162
+
163
+ When a client exceeds their rate limit, `Rack::Throttle` by default returns
164
+ a "403 Forbidden" response with an associated "Rate Limit Exceeded" message
165
+ in the response body.
166
+
167
+ An HTTP 403 response means that the server understood the request, but is
168
+ refusing to respond to it and an accompanying message will explain why.
169
+ This indicates an error on the client's part in exceeding the rate limits
170
+ outlined in the acceptable use policy for the site, service, or API.
171
+
172
+ ### 503 Service Unavailable (Rate Limit Exceeded)
173
+
174
+ However, there exists a widespread practice of instead returning a "503
175
+ Service Unavailable" response when a client exceeds the set rate limits.
176
+ This is technically dubious because it indicates an error on the server's
177
+ part, which is certainly not the case with rate limiting - it was the client
178
+ that committed the oops, not the server.
179
+
180
+ An HTTP 503 response would be correct in situations where the server was
181
+ genuinely overloaded and couldn't handle more requests, but for rate
182
+ limiting an HTTP 403 response is more appropriate. Nonetheless, if you think
183
+ otherwise, `Rack::Throttle` does allow you to override the returned HTTP
184
+ status code by passing in a `:code => 503` option when constructing a
185
+ `Rack::Throttle::Limiter` instance.
186
+
187
+ Documentation
188
+ -------------
189
+ <http://rubydoc.info/gems/improved-rack-throttle>
190
+
191
+ * {Rack::Throttle}
192
+ * {Rack::Throttle::Interval}
193
+ * {Rack::Throttle::Daily}
194
+ * {Rack::Throttle::Hourly}
195
+ * {Rack::Throttle::SlidingWindow}
196
+ * {Rack::Throttle::Matcher}
197
+ * {Rack::Throttle::MethodMatcher}
198
+ * {Rack::Throttle::UrlMatcher}
199
+ * {Rack::Throttle::UserAgentMatcher}
200
+
201
+ Dependencies
202
+ ------------
203
+
204
+ * [Rack](http://rubygems.org/gems/rack) (>= 1.0.0)
205
+
206
+ Installation
207
+ ------------
208
+
209
+ The recommended installation method is via [RubyGems](http://rubygems.org/).
210
+ To install the latest official release of the gem, do:
211
+
212
+ % [sudo] gem install improved-rack-throttle
213
+
214
+ Download
215
+ --------
216
+
217
+ To get a local working copy of the development repository, do:
218
+
219
+ % git clone git://github.com/rooktone/improved-rack-throttle-w-expiry
220
+
221
+ Alternatively, you can download the latest development version as a tarball
222
+ as follows:
223
+
224
+ % wget https://github.com/rooktone/improved-rack-throttle-w-expiry/tarball/master
225
+
226
+ Authors
227
+ -------
228
+ * [Ben Somers](mailto:somers.ben@gmail.com) - <http://www.somanyrobots.com>
229
+ * [Arto Bendiken](mailto:arto.bendiken@gmail.com) - <http://ar.to/>
230
+ * [Brendon Murphy](mailto:disposable.20.xternal@spamourmet.com>) - <http://www.techfreak.net/>
231
+ * [Shane Moore](mailto:shane@ninja.ie>) - <http://ninja.ie/>
232
+
233
+ License
234
+ -------
235
+
236
+ `Rack::Throttle` is free and unencumbered public domain software. For more
237
+ information, see <http://unlicense.org/> or the accompanying UNLICENSE file.
238
+
239
+ [Rack]: http://rack.rubyforge.org/
240
+ [gdbm]: http://ruby-doc.org/stdlib/libdoc/gdbm/rdoc/classes/GDBM.html
241
+ [memcached]: http://rubygems.org/gems/memcached
242
+ [memcache-client]: http://rubygems.org/gems/memcache-client
243
+ [memcache]: http://rubygems.org/gems/memcache
244
+ [redis]: http://rubygems.org/gems/redis
245
+ [Heroku]: http://heroku.com/
246
+ [Heroku memcache]: http://docs.heroku.com/memcache
247
+
data/ROADMAP.md ADDED
@@ -0,0 +1,14 @@
1
+ ## ROADMAP
2
+
3
+ #### 0.7.1
4
+ Original fork from https://github.com/bensomers/improved-rack-throttle
5
+
6
+ #### 0.8.0 +
7
+ The main problem with this middleware is that it doesnt scale well. Especially with redis. The keys are stored in the cache and they increase indefinitely (unless you're using an LRU mechanism).
8
+ This is a nightmare for scalability. So the main feature added will be the concept of an expiring key. Using redis's built in expiry mechanism will speed this up considerably.
9
+ Initial addition of an expiry mechanism. Only redis will be supported initially.
10
+ Memcache and GDBM support later.
11
+
12
+ #### 0.9.0 +
13
+ Anyones guess
14
+
data/Rakefile ADDED
@@ -0,0 +1,43 @@
1
+ # encoding: utf-8
2
+ $:.push File.join(File.dirname(__FILE__),'lib')
3
+
4
+ require 'rubygems'
5
+ require 'bundler'
6
+ begin
7
+ Bundler.setup(:default, :development)
8
+ rescue Bundler::BundlerError => e
9
+ $stderr.puts e.message
10
+ $stderr.puts "Run `bundle install` to install missing gems"
11
+ exit e.status_code
12
+ end
13
+ require 'rake'
14
+
15
+ require 'rack/throttle'
16
+ require 'jeweler'
17
+ Jeweler::Tasks.new do |gem|
18
+ # gem is a Gem::Specification... see http://docs.rubygems.org/read/chapter/20 for more options
19
+ gem.name = "improved-rack-throttle"
20
+ gem.version = Rack::Throttle::VERSION
21
+ gem.homepage = "http://github.com/bensomers/improved-rack-throttle"
22
+ gem.license = "Public Domain"
23
+ gem.summary = %Q{HTTP request rate limiter for Rack applications.}
24
+ gem.description = %Q{Rack middleware for rate-limiting incoming HTTP requests.}
25
+ gem.email = "somers.ben@gmail.com"
26
+ gem.authors = ["Ben Somers", "Arto Bendiken", "Brendon Murphy"]
27
+ # dependencies defined in Gemfile
28
+ end
29
+ Jeweler::RubygemsDotOrgTasks.new
30
+
31
+ require 'rdoc/task'
32
+ Rake::RDocTask.new do |rdoc|
33
+ version = File.exist?('VERSION') ? File.read('VERSION') : ""
34
+
35
+ rdoc.rdoc_dir = 'rdoc'
36
+ rdoc.title = "improved-rack-throttle #{version}"
37
+ rdoc.rdoc_files.include('README*')
38
+ rdoc.rdoc_files.include('lib/**/*.rb')
39
+ end
40
+
41
+ require 'rspec/core/rake_task'
42
+ RSpec::Core::RakeTask.new(:spec)
43
+ task :default => :spec
data/UNLICENSE 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/doc/.gitignore ADDED
@@ -0,0 +1,2 @@
1
+ rdoc
2
+ yard
data/etc/gdbm.ru ADDED
@@ -0,0 +1,7 @@
1
+ $:.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
2
+ require 'rack/throttle'
3
+ require 'gdbm'
4
+
5
+ use Rack::Throttle::Interval, :min => 3.0, :cache => GDBM.new('/tmp/throttle.db')
6
+
7
+ run lambda { |env| [200, {'Content-Type' => 'text/plain'}, "Hello, world!\n"] }
data/etc/hash.ru ADDED
@@ -0,0 +1,6 @@
1
+ $:.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
2
+ require 'rack/throttle'
3
+
4
+ use Rack::Throttle::Interval, :min => 3.0, :cache => {}
5
+
6
+ run lambda { |env| [200, {'Content-Type' => 'text/plain'}, "Hello, world!\n"] }
@@ -0,0 +1,8 @@
1
+ $:.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
2
+ require 'rack/throttle'
3
+ gem 'memcache-client'
4
+ require 'memcache'
5
+
6
+ use Rack::Throttle::Interval, :min => 3.0, :cache => MemCache.new('localhost:11211')
7
+
8
+ run lambda { |env| [200, {'Content-Type' => 'text/plain'}, "Hello, world!\n"] }