faraday-throttler-rx 0.0.5 → 0.0.8
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
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: c6d581e747431ec354d2cb30e38fb89af04116942c4144fab4875537f91979bf
|
4
|
+
data.tar.gz: 26e6ff264ae784e8587e19806ecdd2b22fc77f9cc197df02313402289c36094f
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 7b1392f8e2107e14e491ea24a5d3dc6e2279e08cb59032a10d3f2c1fc4896b69295b53882fb2c831f4cf26a6ba88e58014f0b329e35aac063b76224b0fb78d4e
|
7
|
+
data.tar.gz: 7b8ded781b9139d6df514205cc997ba1be6aa677f55fc1a38703ff7f8dd76f881f6653202774c35e8107a0093a7e66bbcea287b47250bd7ac13d732972e91594
|
data/examples/client.rb
CHANGED
@@ -6,7 +6,7 @@ require 'faraday_throttler/redis_cache'
|
|
6
6
|
|
7
7
|
|
8
8
|
redis = Redis.new
|
9
|
-
cache = FaradayThrottler::RedisCache.new(redis: redis,
|
9
|
+
cache = FaradayThrottler::RedisCache.new(redis: redis, default_ttl: 60)
|
10
10
|
|
11
11
|
conn = Faraday.new(:url => 'http://localhost:9800') do |faraday|
|
12
12
|
# faraday.response :logger # log requests to STDOUT
|
@@ -11,6 +11,9 @@ module FaradayThrottler
|
|
11
11
|
# The base Faraday adapter.
|
12
12
|
app,
|
13
13
|
|
14
|
+
# enable the throller
|
15
|
+
enabled: true,
|
16
|
+
|
14
17
|
# Sticks cache.
|
15
18
|
cache: Cache.new,
|
16
19
|
|
@@ -63,7 +66,7 @@ module FaradayThrottler
|
|
63
66
|
end
|
64
67
|
|
65
68
|
def call(request_env)
|
66
|
-
return app.call(request_env) if request_env
|
69
|
+
return app.call(request_env) if skip?(request_env)
|
67
70
|
|
68
71
|
start = Time.now
|
69
72
|
|
@@ -105,10 +108,11 @@ module FaradayThrottler
|
|
105
108
|
release_request_stick(cache_key)
|
106
109
|
end
|
107
110
|
end
|
108
|
-
rescue Faraday::
|
109
|
-
if rate_limit_response_checker.call(e.response)
|
111
|
+
rescue Faraday::Error => e
|
112
|
+
if e.is_a?(Faraday::ClientError) && rate_limit_response_checker.call(e.response)
|
110
113
|
wait_and_replay_call(request_env, cache_key, start)
|
111
114
|
else
|
115
|
+
release_request_stick(cache_key)
|
112
116
|
raise e
|
113
117
|
end
|
114
118
|
end
|
@@ -143,7 +147,6 @@ module FaradayThrottler
|
|
143
147
|
|
144
148
|
def request_stick?(cache_key)
|
145
149
|
counter = cache.get(cache_key).to_i
|
146
|
-
p "#{counter}, #{cache_key}"
|
147
150
|
if counter < rate
|
148
151
|
cache.set(cache_key, counter + 1)
|
149
152
|
true
|
@@ -160,6 +163,10 @@ module FaradayThrottler
|
|
160
163
|
def logline(cache_key, line)
|
161
164
|
"[Throttler:#{cache_key}] #{line}"
|
162
165
|
end
|
166
|
+
|
167
|
+
def skip?(request_env)
|
168
|
+
!enabled || request_env[:method] != :get
|
169
|
+
end
|
163
170
|
end
|
164
171
|
|
165
172
|
Faraday::Middleware.register_middleware throttler: ->{ Middleware }
|
@@ -2,14 +2,14 @@ module FaradayThrottler
|
|
2
2
|
class RedisCache
|
3
3
|
NAMESPACE = 'throttler:cache:'.freeze
|
4
4
|
|
5
|
-
def initialize(redis: Redis.new,
|
5
|
+
def initialize(redis: Redis.new, default_ttl: 60)
|
6
6
|
@redis = redis
|
7
|
-
@
|
7
|
+
@default_ttl = default_ttl
|
8
8
|
end
|
9
9
|
|
10
|
-
def set(key, value)
|
10
|
+
def set(key, value, ex: default_ttl)
|
11
11
|
opts = {}
|
12
|
-
opts[:ex] =
|
12
|
+
opts[:ex] = ex if ex > 0
|
13
13
|
redis.set [NAMESPACE, key].join, value, opts
|
14
14
|
end
|
15
15
|
|
@@ -18,6 +18,6 @@ module FaradayThrottler
|
|
18
18
|
end
|
19
19
|
|
20
20
|
private
|
21
|
-
attr_reader :redis, :
|
21
|
+
attr_reader :redis, :default_ttl
|
22
22
|
end
|
23
23
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: faraday-throttler-rx
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.8
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Boris Koumondji
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2022-07-
|
11
|
+
date: 2022-07-17 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: faraday
|