ratelimit-ruby 0.1.1 → 0.1.2
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 +13 -1
- data/VERSION +1 -1
- data/lib/ratelimit-ruby.rb +44 -2
- 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: ba840b0a4094c38901cdf325bcc69fdc3f6a864e
|
4
|
+
data.tar.gz: 15d19e332a9ed5bd77ec0edd4daa326e157e2a76
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: fa3c25ee0c3ac084e7bc2baf30b896b4cfadf20a0e4e1d35929ada2d4a5c2b0c3f755f9345b953ea162a1d4908a179a209fde19cbb6390186cdcb4fbb3fb40ee
|
7
|
+
data.tar.gz: 262baa3cd4d504c389c2ae6fad3c4c62f6402f1cfd03d471c849d1eb6398c0a4874cf199e4f9c49b6134135840cde011d4046c00d5ef4a44228611d3f93c9829
|
data/README.md
CHANGED
@@ -15,6 +15,7 @@ end
|
|
15
15
|
```
|
16
16
|
See full documentation http://www.ratelim.it/documentation
|
17
17
|
|
18
|
+
|
18
19
|
## Supports
|
19
20
|
|
20
21
|
* RateLimits
|
@@ -24,6 +25,17 @@ See full documentation http://www.ratelim.it/documentation
|
|
24
25
|
* Semaphores
|
25
26
|
* Infinite retention fo deduplication workflows
|
26
27
|
|
28
|
+
## Options and Defaults
|
29
|
+
```ruby
|
30
|
+
limiter = RateLimit::Limiter.new(apikey: "ACCT_ID|APIKEY",
|
31
|
+
on_error: :log_and_pass, # :log_and_pass, :log_and_hit, :throw
|
32
|
+
logger: nil, # pass in your own logger here
|
33
|
+
debug: false #Faraday debugging
|
34
|
+
)
|
35
|
+
|
36
|
+
|
37
|
+
```
|
38
|
+
|
27
39
|
|
28
40
|
## Contributing to ratelimit-ruby
|
29
41
|
|
@@ -37,6 +49,6 @@ See full documentation http://www.ratelim.it/documentation
|
|
37
49
|
|
38
50
|
## Copyright
|
39
51
|
|
40
|
-
Copyright (c)
|
52
|
+
Copyright (c) 2017 Jeff Dwyer. See LICENSE.txt for
|
41
53
|
further details.
|
42
54
|
|
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.1.
|
1
|
+
0.1.2
|
data/lib/ratelimit-ruby.rb
CHANGED
@@ -1,5 +1,6 @@
|
|
1
1
|
module RateLimit
|
2
2
|
WAIT_INCR_MAX = 0.5
|
3
|
+
ON_ERROR = [:log_and_pass, :log_and_hit, :throw]
|
3
4
|
|
4
5
|
class WaitExceeded < StandardError
|
5
6
|
end
|
@@ -9,10 +10,16 @@ module RateLimit
|
|
9
10
|
local ? 'http://localhost:8080' : 'http://www.ratelim.it'
|
10
11
|
end
|
11
12
|
|
12
|
-
def initialize(apikey:,
|
13
|
+
def initialize(apikey:, on_error: :log_and_pass, logger: nil, debug: false, local: false)
|
14
|
+
@logger = (logger || Logger.new($stdout)).tap do |log|
|
15
|
+
log.progname = "RateLimit"
|
16
|
+
end
|
17
|
+
@on_error = on_error
|
13
18
|
@conn = Faraday.new(:url => self.base_url(local)) do |faraday|
|
14
19
|
faraday.request :json # form-encode POST params
|
15
20
|
faraday.response :logger if debug
|
21
|
+
faraday.options[:open_timeout] = 2
|
22
|
+
faraday.options[:timeout] = 5
|
16
23
|
faraday.adapter Faraday.default_adapter # make requests with Net::HTTP
|
17
24
|
end
|
18
25
|
(username, pass) = apikey.split("|")
|
@@ -47,9 +54,12 @@ module RateLimit
|
|
47
54
|
result = @conn.post '/api/v1/limitcheck', { acquireAmount: acquire_amount,
|
48
55
|
groups: [group],
|
49
56
|
allowPartialResponse: allow_partial_response }.to_json
|
57
|
+
handle_failure(result) unless result.success?
|
50
58
|
res =JSON.parse(result.body, object_class: OpenStruct)
|
51
59
|
res.amount ||= 0
|
52
60
|
res
|
61
|
+
rescue => e
|
62
|
+
handle_error(e)
|
53
63
|
end
|
54
64
|
|
55
65
|
def acquire_or_wait(key:, acquire_amount:, max_wait_secs:, init_backoff: 0)
|
@@ -71,7 +81,9 @@ module RateLimit
|
|
71
81
|
@conn.post '/api/v1/limitreturn',
|
72
82
|
{ enforcedGroup: limit_result.enforcedGroup,
|
73
83
|
amount: limit_result.amount }.to_json
|
74
|
-
|
84
|
+
handle_failure(result) unless result.success?
|
85
|
+
rescue => e
|
86
|
+
handle_error(e)
|
75
87
|
end
|
76
88
|
|
77
89
|
private
|
@@ -83,6 +95,35 @@ module RateLimit
|
|
83
95
|
policyName: limit_definition.policy,
|
84
96
|
returnable: limit_definition.returnable }.to_json
|
85
97
|
result= @conn.send(method, '/api/v1/limits', to_send)
|
98
|
+
handle_failure(result) unless result.success?
|
99
|
+
rescue => e
|
100
|
+
handle_error(e)
|
101
|
+
end
|
102
|
+
|
103
|
+
def handle_failure(result)
|
104
|
+
case @on_error
|
105
|
+
when :log_and_pass
|
106
|
+
@logger.warn("returned #{result.status}")
|
107
|
+
OpenStruct.new(passed: true)
|
108
|
+
when :log_and_hit
|
109
|
+
@logger.warn("returned #{result.status}")
|
110
|
+
OpenStruct.new(passed: false)
|
111
|
+
when :throw
|
112
|
+
raise "#{result.status} calling RateLim.it"
|
113
|
+
end
|
114
|
+
end
|
115
|
+
|
116
|
+
def handle_error(e)
|
117
|
+
case @on_error
|
118
|
+
when :log_and_pass
|
119
|
+
@logger.warn(e)
|
120
|
+
OpenStruct.new(passed: true)
|
121
|
+
when :log_and_hit
|
122
|
+
@logger.warn(e)
|
123
|
+
OpenStruct.new(passed: false)
|
124
|
+
when :throw
|
125
|
+
raise e
|
126
|
+
end
|
86
127
|
end
|
87
128
|
end
|
88
129
|
|
@@ -90,4 +131,5 @@ end
|
|
90
131
|
|
91
132
|
require 'faraday'
|
92
133
|
require 'faraday_middleware'
|
134
|
+
require 'logger'
|
93
135
|
require 'ratelimit/limit_definition'
|
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.2 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.2"
|
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-12-
|
14
|
+
s.date = "2016-12-16"
|
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.2
|
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-12-
|
11
|
+
date: 2016-12-16 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: faraday
|