faraday-throttler-rx 0.0.4 → 0.0.7
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/.gitignore +1 -0
- data/examples/client.rb +1 -1
- data/lib/faraday_throttler/cache.rb +1 -1
- data/lib/faraday_throttler/middleware.rb +17 -6
- data/lib/faraday_throttler/redis_cache.rb +5 -5
- data/lib/faraday_throttler/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 010e832af2e263e60bc76346f22620976f8b0a9a169d916c9929a004909ec5c0
|
4
|
+
data.tar.gz: dc4043fe5b8a4333ef456981b2a8bc3d7ecef535aba2bb11471d6e3088639b8e
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 99503113453a6c9107d68a5e1240cfb3507b1376fa1a7bb91fafd62949628326f2a9040d6c7047d4ca78ef1d31055244d3a35a958c3da7fcbc70c8c5c183d34e
|
7
|
+
data.tar.gz: c1eb7322b9240e8abc6ef153ee8aca7c4ca28fd878e9da984158b73ebd9fe9e83a1dd94ff6643a1ce1bf8fa60e07954f6c757a2dff85529b95e035d710ce7929
|
data/.gitignore
CHANGED
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
|
@@ -97,9 +97,7 @@ module FaradayThrottler
|
|
97
97
|
def fetch_and_check_rate_limit(request_env, cache_key, start)
|
98
98
|
app.call(request_env).on_complete do |response_env|
|
99
99
|
if rate_limit_response_checker.call(response_env)
|
100
|
-
|
101
|
-
logger.debug logline(cache_key, "C.1.1. Rate limited on backend. Took #{Time.now - start}")
|
102
|
-
fetch_and_check_rate_limit(request_env, cache_key, start)
|
100
|
+
wait_and_replay_call(request_env, cache_key, start)
|
103
101
|
else
|
104
102
|
# Everything alright
|
105
103
|
logger.debug logline(cache_key, "C.1.2. Everything alright, request finished. Took #{Time.now - start}")
|
@@ -107,6 +105,20 @@ module FaradayThrottler
|
|
107
105
|
release_request_stick(cache_key)
|
108
106
|
end
|
109
107
|
end
|
108
|
+
rescue Faraday::Error => e
|
109
|
+
if e.is_a?(Faraday::ClientError) && rate_limit_response_checker.call(e.response)
|
110
|
+
wait_and_replay_call(request_env, cache_key, start)
|
111
|
+
else
|
112
|
+
release_request_stick(cache_key)
|
113
|
+
raise e
|
114
|
+
end
|
115
|
+
end
|
116
|
+
|
117
|
+
def wait_and_replay_call(request_env, cache_key, start)
|
118
|
+
# Replay request call
|
119
|
+
sleep wait
|
120
|
+
logger.debug logline(cache_key, "C.1.1. Rate limited on backend. Took #{Time.now - start}")
|
121
|
+
fetch_and_check_rate_limit(request_env, cache_key, start)
|
110
122
|
end
|
111
123
|
|
112
124
|
def validate_dep!(dep, dep_name, *methods)
|
@@ -131,8 +143,7 @@ module FaradayThrottler
|
|
131
143
|
end
|
132
144
|
|
133
145
|
def request_stick?(cache_key)
|
134
|
-
counter = cache.get(cache_key
|
135
|
-
p "#{counter}, #{cache_key}"
|
146
|
+
counter = cache.get(cache_key).to_i
|
136
147
|
if counter < rate
|
137
148
|
cache.set(cache_key, counter + 1)
|
138
149
|
true
|
@@ -142,7 +153,7 @@ module FaradayThrottler
|
|
142
153
|
end
|
143
154
|
|
144
155
|
def release_request_stick(cache_key)
|
145
|
-
counter = cache.get(cache_key
|
156
|
+
counter = cache.get(cache_key).to_i
|
146
157
|
cache.set(cache_key, counter - 1) if counter > 0
|
147
158
|
end
|
148
159
|
|
@@ -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.7
|
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-02 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: faraday
|