leaky_bucket 0.2.4 → 0.2.5
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +31 -5
- data/lib/leaky_bucket/version.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 5cca7ae9bd77604a22863e2c3ee3dab9a7c35320
|
4
|
+
data.tar.gz: 69563a92883c7275ab6e5b6a949c37d534e50046
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: bc97b9bebf560779a0827c0d952261c0586d9c00baa7e984bc991659f18495c614e0a454cfddbca58a649963d11e27c3d6047c89516a195f97349b1f4a6e4f76
|
7
|
+
data.tar.gz: d4849fa6fccfce9fd889b04f4d73a13b454ea08a74f121737380a45b9c1370fb23b9fc933231bf018831bc00fb8bd443574f22fb92dd8017a2f00e066d1134ef
|
data/README.md
CHANGED
@@ -1,8 +1,6 @@
|
|
1
1
|
# LeakyBucket
|
2
2
|
|
3
|
-
|
4
|
-
|
5
|
-
TODO: Delete this and the text above, and describe your gem
|
3
|
+
If you are running a busy application in production, you should limit the number of requests a user can make per time unit. LeakyBucket helps you do just that, easily.
|
6
4
|
|
7
5
|
## Installation
|
8
6
|
|
@@ -20,13 +18,41 @@ Or install it yourself as:
|
|
20
18
|
|
21
19
|
$ gem install leaky_bucket
|
22
20
|
|
21
|
+
Then create a `leaky_bucket.rb` initializer file in `config/initializers` with :
|
22
|
+
|
23
|
+
```ruby
|
24
|
+
LeakyBucket.cache = Rails.cache # or memcached
|
25
|
+
```
|
26
|
+
|
27
|
+
|
23
28
|
## Usage
|
24
29
|
|
25
|
-
|
30
|
+
to limit the requests to an action, use the `throttle` method:
|
31
|
+
|
32
|
+
```ruby
|
33
|
+
def create
|
34
|
+
LeakyBucket::Throttler.throttle(request.ip)
|
35
|
+
#other code that will run if the user has not reached the maximum threshold.
|
36
|
+
end
|
37
|
+
```
|
38
|
+
|
39
|
+
the `throttle` method will raise an exception and return and 429 response code if the user has made too many requests.
|
40
|
+
|
41
|
+
By default, the threshold is 100 requests/hour, to override it, just provide the params:
|
42
|
+
|
43
|
+
```ruby
|
44
|
+
# allow maximum 50 requests per minute.
|
45
|
+
LeakyBucket::Throttler.throttle(ip, threshold: 50, interval: 60, burst: 10)
|
46
|
+
```
|
47
|
+
|
48
|
+
`threhold` is the number of requests.
|
49
|
+
|
50
|
+
`interval` is in seconds.
|
51
|
+
|
52
|
+
`burst` is the number of requests the user is allowed to make at once, when the burst is exhausted, the user will have to wait until the sometime has passed to make another request.
|
26
53
|
|
27
54
|
## Development
|
28
55
|
|
29
|
-
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
30
56
|
|
31
57
|
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
32
58
|
|
data/lib/leaky_bucket/version.rb
CHANGED