grape-throttler 1.0.0 → 1.0.1
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 +4 -4
- data/README.md +42 -43
- data/lib/grape-throttler/middleware/throttle_middleware.rb +1 -1
- data/lib/grape-throttler/version.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: c02d5c5ef081337c20539acf09bafd5c9ae35fedeeb16a92c38b16e5839fed04
|
4
|
+
data.tar.gz: b49635981a48db4d77ffb3ce56e539b1b33aa42727f931e20e5324dea0320645
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: bbcca0e55d60925b0afb472459c88580e8dfe214c27059fa26e92a825b5f72ac3d6e5225d00ed8e38e3ca0f439f99d2334a0d2631907763453ee783d1c6f4fa3
|
7
|
+
data.tar.gz: 40644a93838ebe1e019bbfee16cf11606b4bbc375f569c554d5ed52ee99689c2e9da100ec69ad85799caf95180151c75d76ae96c9e0d1b25aef25065102eab35
|
data/README.md
CHANGED
@@ -1,40 +1,38 @@
|
|
1
|
-
|
2
|
-
==============
|
1
|
+
# GrapeThrottler
|
3
2
|
|
4
|
-
[](http://badge.fury.io/rb/grape-throttler)
|
4
|
+
[](https://travis-ci.org/drexed/grape-throttler)
|
6
5
|
|
7
|
-
|
6
|
+
GrapeThrottler provides a simple endpoint-specific throttling mechanism for Grape.
|
8
7
|
|
9
|
-
|
8
|
+
## Installation
|
10
9
|
|
11
|
-
|
10
|
+
Add this line to your application's Gemfile:
|
12
11
|
|
13
|
-
|
14
|
-
|
15
|
-
|
12
|
+
```ruby
|
13
|
+
gem 'grape-throttler'
|
14
|
+
```
|
16
15
|
|
17
|
-
|
18
|
-
* Redis
|
16
|
+
And then execute:
|
19
17
|
|
20
|
-
|
18
|
+
$ bundle
|
21
19
|
|
22
|
-
|
20
|
+
Or install it yourself as:
|
23
21
|
|
24
|
-
|
22
|
+
$ gem install grape-throttler
|
25
23
|
|
26
|
-
|
27
|
-
gem 'grape-throttle'
|
28
|
-
```
|
24
|
+
## Table of Contents
|
29
25
|
|
30
|
-
|
26
|
+
* [Configuration](#configuration)
|
27
|
+
* [Endpoint](#endpoint)
|
31
28
|
|
32
|
-
|
29
|
+
## Configuration
|
30
|
+
In your Grape API, install the middleware which will do the throttling. At a minimum, it requires a Redis instance for caching as the `cache` parameter.
|
33
31
|
|
34
32
|
**Simple Case**
|
35
33
|
|
36
34
|
```ruby
|
37
|
-
use
|
35
|
+
use GrapeThrottler::Middleware::ThrottleMiddleware, cache: Redis.new
|
38
36
|
```
|
39
37
|
|
40
38
|
In this simple case, you just set up the middleware, and pass it a Redis instance.
|
@@ -42,7 +40,7 @@ In this simple case, you just set up the middleware, and pass it a Redis instanc
|
|
42
40
|
**Advanced Case**
|
43
41
|
|
44
42
|
```ruby
|
45
|
-
use
|
43
|
+
use GrapeThrottler::Middleware::ThrottleMiddleware, cache: $redis, user_key: ->(env) do
|
46
44
|
# Use the current_user's id as an identifier
|
47
45
|
user = current_user
|
48
46
|
user.nil? ? nil : user.id
|
@@ -58,10 +56,10 @@ The `user_key` parameter is a function that can be used to determine a custom id
|
|
58
56
|
The gem will log errors to STDOUT by default. If you prefer a different logger you can use the `logger` option to pass in your own logger.
|
59
57
|
|
60
58
|
```ruby
|
61
|
-
use
|
59
|
+
use GrapeThrottler::Middleware::ThrottleMiddleware, cache: Redis.new, logger: Logger.new('my_custom_log.log')
|
62
60
|
```
|
63
61
|
|
64
|
-
|
62
|
+
## Endpoint
|
65
63
|
|
66
64
|
This gem adds a `throttle` DSL-like method that can be used to throttle different endpoints differently.
|
67
65
|
|
@@ -72,56 +70,57 @@ Supported predefined periods are: `:hourly`, `:daily`, `:monthly`.
|
|
72
70
|
Example:
|
73
71
|
|
74
72
|
```ruby
|
75
|
-
class API <
|
73
|
+
class API < GrapeThrottler::API
|
76
74
|
resources :users do
|
77
75
|
|
78
76
|
# Allow start of competition only every 10 minutes
|
79
|
-
desc
|
77
|
+
desc 'Start competition'
|
80
78
|
throttle period: 10.minutes, limit: 1
|
81
79
|
params do
|
82
|
-
requires :id, type: Integer, desc:
|
80
|
+
requires :id, type: Integer, desc: 'id'
|
83
81
|
end
|
84
|
-
post
|
82
|
+
post '/:id/competition' do
|
85
83
|
User.find(params[:id]).start_competition
|
86
84
|
end
|
87
85
|
|
88
86
|
# 3 times a day max
|
89
|
-
desc
|
87
|
+
desc 'Fetch a user'
|
90
88
|
throttle daily: 3
|
91
89
|
params do
|
92
|
-
requires :id, type: Integer, desc:
|
90
|
+
requires :id, type: Integer, desc: 'id'
|
93
91
|
end
|
94
|
-
get
|
92
|
+
get '/:id' do
|
95
93
|
User.find(params[:id])
|
96
94
|
end
|
97
95
|
|
98
96
|
# Once a month or the user will go crazy
|
99
|
-
desc
|
97
|
+
desc 'Poke a user'
|
100
98
|
throttle monthly: 1
|
101
99
|
params do
|
102
|
-
requires :id, type: Integer, desc:
|
100
|
+
requires :id, type: Integer, desc: 'id'
|
103
101
|
end
|
104
|
-
post
|
102
|
+
post '/:id/poke' do
|
105
103
|
User.find(params[:id]).poke
|
106
104
|
end
|
107
105
|
|
108
106
|
# No limit to the amount we can annoy users
|
109
|
-
desc
|
107
|
+
desc 'Annoy a user'
|
110
108
|
params do
|
111
|
-
requires :id, type: Integer, desc:
|
109
|
+
requires :id, type: Integer, desc: 'id'
|
112
110
|
end
|
113
|
-
post
|
111
|
+
post '/:id/annoy' do
|
114
112
|
User.find(params[:id]).annoy
|
115
113
|
end
|
116
114
|
end
|
117
115
|
end
|
118
116
|
```
|
119
117
|
|
120
|
-
##
|
121
|
-
|
122
|
-
* Custom error handling and error strings, status etc.
|
123
|
-
* Allow use of something other than Redis for caching
|
118
|
+
## Contributing
|
124
119
|
|
125
|
-
|
120
|
+
Your contribution is welcome.
|
126
121
|
|
127
|
-
|
122
|
+
1. Fork it
|
123
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
124
|
+
3. Commit your changes (`git commit -am 'Added some feature'`)
|
125
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
126
|
+
5. Create new Pull Request
|
@@ -29,7 +29,7 @@ module GrapeThrottler
|
|
29
29
|
current = redis.get(rate_key).to_i
|
30
30
|
|
31
31
|
if !current.nil? && current >= limit
|
32
|
-
endpoint.error!('
|
32
|
+
endpoint.error!('Too Many Requests', 429)
|
33
33
|
else
|
34
34
|
redis.multi do
|
35
35
|
redis.set(rate_key, COUNTER_START, { nx: true, ex: period.to_i })
|