README.md in pause-0.0.4 vs README.md in pause-0.0.5
- old
+ new
@@ -1,13 +1,19 @@
Pause
======
[![Build status](https://secure.travis-ci.org/wanelo/pause.png)](http://travis-ci.org/wanelo/pause)
-Pause is a Redis-backed rate-limiting client. Use it to track events, with
+Pause is a flexible Redis-backed rate-limiting client. Use it to track events, with
rules around how often they are allowed to occur within configured time checks.
+Because Pause is Redis-based, multiple ruby processes (even distributed across multiple servers) can track and report
+events together, and then query whether a particular identifier should be rate limited or not.
+
+Sample applications include IP-based blocking based on HTTP request volume (see related gem "spanx"),
+throttling push notifications as to not overwhelm the user with too much frequency, etc.
+
## Installation
Add this line to your application's Gemfile:
gem 'pause'
@@ -34,20 +40,19 @@
```ruby
Pause.configure do |config|
config.redis_host = "127.0.0.1"
config.redis_port = 6379
config.redis_db = 1
-
- config.resolution = 600
- config.history = 86400
+ config.resolution = 600 # aggregate all events into 10 minute blocks
+ config.history = 86400 # discard all events older than 1 day
end
```
### Actions
Define local actions for your application. Actions define a scope by
-which they are identified in the persistent store, and a set of checks. Checks define various
+which they are identified in the persistent store (aka "namespace"), and a set of checks. Checks define various
thresholds (`max_allowed`) against periods of time (`period_seconds`). When a threshold it triggered,
the action is rate limited, and stays rate limited for the duration of `block_ttl` seconds.
#### Checks
@@ -60,13 +65,20 @@
#### Scope
Scope is simple string used to identify this action in the Redis store, and is appended to all keys.
Therefore it is advised to keep scope as short as possible to reduce memory requirements of the store.
+If you are using the same Redis store to rate limit multiple actions, you must ensure that each action
+has a unique scope.
+
#### Resolution
-Note that your resolution must be less than or equal to the smallest `period_seconds` value in your checks.
+Resolution is the period of aggregation. As events come in, Pause aggregates them in time blocks
+of this length. If you set resolution to 10 minutes, all events arriving within a 10 minute block
+are aggregated.
+
+Resolution must be less than or equal to the smallest `period_seconds` value in your checks.
In other words, if your shortest check is 1 minute, you could set resolution to 1 minute or smaller.
#### Example
```ruby
@@ -86,10 +98,11 @@
class FollowsController < ApplicationController
def create
action = FollowAction.new(user.id)
if action.ok?
# do stuff!
+ # and track it...
action.increment!
else
# action is rate limited, either skip
# or show error, depending on the context.
end
@@ -97,43 +110,84 @@
end
class OtherController < ApplicationController
def index
action = OtherAction.new(params[:thing])
- if action.ok?
+ unless action.rate_limited?
+ # perform business logic
+ ....
+ # track it
action.increment!(params[:count].to_i, Time.now.to_i)
end
end
end
```
If more data is needed about why the action is blocked, the `analyze` can be called
```ruby
-action = MyAction.new("thing")
+action = NotifyViaEmailAction.new("thing")
while true
action.increment!
- blocked_action = action.analyze
-
- if blocked_action
- puts blocked_action.identifier
- puts blocked_action.sum
- puts blocked_action.timestamp
-
- puts blocked_aciton.period_check.inspect
+ rate_limit_event = action.analyze
+ if rate_limit_event
+ puts rate_limit_event.identifier # which key got rate limited ("thing")
+ puts rate_limit_event.sum # total count that triggered a rate limit
+ puts rate_limit_event.timestamp # timestamp when rate limiting occurred
+ puts rate_limit_event.period_check # period check object, that triggered this rate limiting event
+ else
+ # not rate-limited, same as action.ok?
end
sleep 1
end
```
+## Enabling/Disabling Actions
+
+Actions have a built-in way by which they can be disabled or enabled.
+
+```ruby
+MyAction.disable
+MyAction.enable
+```
+
+This is persisted to Redis, so state is not process-bound, but shared across all ruby run-times using this
+action (assuming Redis store configuration is the same).
+
+When disabled, Pause does *not* check state in any of its methods, so calls to increment! or ok? still work
+exactly as before. This is because adding extra Redis calls can be expensive in loops. You should check
+whether your action is enabled or disabled if it important to support enabling and disabling of rate limiting in
+your context.
+
+```ruby
+while true
+ if MyAction.enabled?
+ Thing.all.each do |thing|
+ action = MyAction.new(thing.name)
+ action.increment! unless action.rate_limited?
+ end
+ end
+ sleep 10
+end
+```
+
+
## Contributing
Want to make it better? Cool. Here's how:
1. Fork it
2. Create your feature branch (`git checkout -b my-new-feature`)
3. Commit your changes (`git commit -am 'Add some feature'`)
4. Push to the branch (`git push origin my-new-feature`)
5. Create a new pull request
+
+## Authors
+
+This gem was written by Eric Saxby, Atasay Gokkaya and Konstantin Gredeskoul at Wanelo, Inc.
+
+Please see the LICENSE.txt file for further details.
+
+