ratelimit-ruby 0.1.0 → 0.1.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 +3 -3
- data/VERSION +1 -1
- data/lib/ratelimit-ruby.rb +26 -11
- data/lib/ratelimit/limit_definition.rb +4 -3
- data/ratelimit-ruby.gemspec +3 -3
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 2f3b746eec2993e0825020e4c703de39a6a3458a
|
4
|
+
data.tar.gz: e49e70c0153e2a0c5aa5d8f8a87382e4664ed412
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 9f18859ffe8e2805dce9597d79c6ee3b9f50a8a642b520e4bd22ef95a10750b5ed3324404ec377a6e72a16506bc20a31fcf50abfb0abc64a9e1a566eccba0354
|
7
|
+
data.tar.gz: 7435ee478458b536a662854e9f99c15eee732b739ce1cfebba7330911bab8b2613fc6627e43cc601d442d58ddba1b68cf257a3f50c8d7d5cb3025fe812c7376a
|
data/README.md
CHANGED
@@ -3,13 +3,13 @@
|
|
3
3
|
Rate Limit your Ruby app using http://www.ratelim.it
|
4
4
|
|
5
5
|
```ruby
|
6
|
-
limiter = RateLimit::Limiter.new(apikey: "APIKEY"
|
6
|
+
limiter = RateLimit::Limiter.new(apikey: "ACCT_ID|APIKEY")
|
7
7
|
|
8
8
|
# only need to do this on startup
|
9
9
|
# limit to 1 per hour
|
10
|
-
limiter.upsert_limit(
|
10
|
+
limiter.upsert_limit("pageload", 1, RateLimIt::HOURLY_ROLLING)
|
11
11
|
|
12
|
-
if limiter.
|
12
|
+
if limiter.pass?("pageload")
|
13
13
|
do_hourly_thing()
|
14
14
|
end
|
15
15
|
```
|
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.1.
|
1
|
+
0.1.1
|
data/lib/ratelimit-ruby.rb
CHANGED
@@ -1,4 +1,6 @@
|
|
1
1
|
module RateLimit
|
2
|
+
WAIT_INCR_MAX = 0.5
|
3
|
+
|
2
4
|
class WaitExceeded < StandardError
|
3
5
|
end
|
4
6
|
|
@@ -7,24 +9,36 @@ module RateLimit
|
|
7
9
|
local ? 'http://localhost:8080' : 'http://www.ratelim.it'
|
8
10
|
end
|
9
11
|
|
10
|
-
def initialize(apikey:,
|
12
|
+
def initialize(apikey:, local: false, debug: false)
|
11
13
|
@conn = Faraday.new(:url => self.base_url(local)) do |faraday|
|
12
14
|
faraday.request :json # form-encode POST params
|
13
15
|
faraday.response :logger if debug
|
14
16
|
faraday.adapter Faraday.default_adapter # make requests with Net::HTTP
|
15
17
|
end
|
16
|
-
|
18
|
+
(username, pass) = apikey.split("|")
|
19
|
+
@conn.basic_auth(username, pass)
|
20
|
+
end
|
21
|
+
|
22
|
+
|
23
|
+
def create_concurrency_limit(group, concurrent, timeout_seconds)
|
24
|
+
upsert_concurrency_limit(group, concurrent, timeout_seconds, method: :post)
|
25
|
+
end
|
26
|
+
|
27
|
+
def upsert_concurrency_limit(group, concurrent, timeout_seconds, method: :put)
|
28
|
+
recharge_rate = (24*60*60)/timeout_seconds
|
29
|
+
recharge_policy = DAILY_ROLLING
|
30
|
+
upsert(LimitDefinition.new(group, recharge_rate, recharge_policy, true, concurrent), method)
|
17
31
|
end
|
18
32
|
|
19
|
-
def
|
20
|
-
upsert(LimitDefinition.new(group, limit, policy,
|
33
|
+
def create_limit(group, limit, policy, burst: nil)
|
34
|
+
upsert(LimitDefinition.new(group, limit, policy, false, burst || limit), :post)
|
21
35
|
end
|
22
36
|
|
23
|
-
def upsert_limit(group, limit, policy)
|
24
|
-
upsert(LimitDefinition.new(group, limit, policy, false))
|
37
|
+
def upsert_limit(group, limit, policy, burst: nil)
|
38
|
+
upsert(LimitDefinition.new(group, limit, policy, false, burst || limit), :put)
|
25
39
|
end
|
26
40
|
|
27
|
-
def
|
41
|
+
def pass?(group)
|
28
42
|
result = acquire(group, 1)
|
29
43
|
return result.passed
|
30
44
|
end
|
@@ -47,7 +61,7 @@ module RateLimit
|
|
47
61
|
if res.passed
|
48
62
|
return res
|
49
63
|
end
|
50
|
-
sleep += rand
|
64
|
+
sleep += rand * WAIT_INCR_MAX
|
51
65
|
end
|
52
66
|
raise RateLimit::WaitExceeded
|
53
67
|
end
|
@@ -57,17 +71,18 @@ module RateLimit
|
|
57
71
|
@conn.post '/api/v1/limitreturn',
|
58
72
|
{ enforcedGroup: limit_result.enforcedGroup,
|
59
73
|
amount: limit_result.amount }.to_json
|
60
|
-
puts result.body
|
74
|
+
puts "RETURN #{result.body} #{result.status}"
|
61
75
|
end
|
62
76
|
|
63
77
|
private
|
64
78
|
|
65
|
-
def upsert(limit_definition)
|
79
|
+
def upsert(limit_definition, method)
|
66
80
|
to_send = { limit: limit_definition.limit,
|
67
81
|
group: limit_definition.group,
|
82
|
+
burst: limit_definition.burst,
|
68
83
|
policyName: limit_definition.policy,
|
69
84
|
returnable: limit_definition.returnable }.to_json
|
70
|
-
result= @conn.
|
85
|
+
result= @conn.send(method, '/api/v1/limits', to_send)
|
71
86
|
end
|
72
87
|
end
|
73
88
|
|
@@ -11,14 +11,15 @@ module RateLimit
|
|
11
11
|
POLICIES = [SECONDLY, MINUTELY, MINUTELY_ROLLING, HOURLY, HOURLY_ROLLING, DAILY, DAILY_ROLLING, MINUTELY, INFINITE]
|
12
12
|
|
13
13
|
class LimitDefinition
|
14
|
-
attr_reader :limit, :group, :policy, :returnable
|
14
|
+
attr_reader :limit, :group, :policy, :returnable, :burst
|
15
15
|
|
16
|
-
def initialize(group, limit, policy, returnable)
|
16
|
+
def initialize(group, limit, policy, returnable, burst)
|
17
17
|
raise "Invalid Policy" unless POLICIES.include? policy
|
18
18
|
@limit = limit
|
19
19
|
@group = group
|
20
20
|
@policy = policy
|
21
|
-
@returnable= returnable
|
21
|
+
@returnable = returnable
|
22
|
+
@burst = burst
|
22
23
|
end
|
23
24
|
end
|
24
25
|
end
|
data/ratelimit-ruby.gemspec
CHANGED
@@ -2,16 +2,16 @@
|
|
2
2
|
# DO NOT EDIT THIS FILE DIRECTLY
|
3
3
|
# Instead, edit Juwelier::Tasks in Rakefile, and run 'rake gemspec'
|
4
4
|
# -*- encoding: utf-8 -*-
|
5
|
-
# stub: ratelimit-ruby 0.1.
|
5
|
+
# stub: ratelimit-ruby 0.1.1 ruby lib
|
6
6
|
|
7
7
|
Gem::Specification.new do |s|
|
8
8
|
s.name = "ratelimit-ruby"
|
9
|
-
s.version = "0.1.
|
9
|
+
s.version = "0.1.1"
|
10
10
|
|
11
11
|
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
12
12
|
s.require_paths = ["lib"]
|
13
13
|
s.authors = ["Jeff Dwyer"]
|
14
|
-
s.date = "2016-
|
14
|
+
s.date = "2016-12-13"
|
15
15
|
s.description = "rate limit your ruby"
|
16
16
|
s.email = "jdwyah@gmail.com"
|
17
17
|
s.extra_rdoc_files = [
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: ratelimit-ruby
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Jeff Dwyer
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2016-
|
11
|
+
date: 2016-12-13 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: faraday
|