faraday_throttler 0.0.2 → 0.0.3

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 8f579e07afd3e9eaef5e05bfad056a6334cf5f32
4
- data.tar.gz: 0d714c032c901eb44c75974a6fdfe7ab69946e4c
3
+ metadata.gz: 0751994072b42e01e211a07884eed4e8e1e3a2e9
4
+ data.tar.gz: 8591da828d84bf43b0bccff3df9785819529d999
5
5
  SHA512:
6
- metadata.gz: a401855bca195c3b855b31160df610490395238ad6f626b5561d93db05ed616c5bf922f70cd994a83043b7c10bf690c7f676e15fc5d1ad9065912028eb10c43e
7
- data.tar.gz: 274a82b0fed4d985b230298725b0e3ef10aa8c0634a986294d7c5b92228e594074d816d1b8a4bc0f757ba359a10da02731d0384290df09b333f383ed655a5dc2
6
+ metadata.gz: 6393d8dc8915d6a9434ec83f85df65e19baff9d430da5ac0aed2be0383b5b5432e3d7c8f1566a63d54725d2a6e2f7d1b1b5fd4aac78cf120aed4a1f61e1ed06b
7
+ data.tar.gz: eaebf1976fcb54977e419f012f475de3ee0f2a7ec1b21b58e29a4afbb4827ec9635a5b20aa63e908b0716702acb355f6668b5ca784537e0ed81fb7e36dcb9628
@@ -1,5 +1,6 @@
1
1
  require 'timeout'
2
2
  require 'faraday'
3
+ require 'logger'
3
4
  require 'faraday_throttler/key_resolver'
4
5
  require 'faraday_throttler/mem_lock'
5
6
  require 'faraday_throttler/cache'
@@ -89,7 +90,18 @@ module FaradayThrottler
89
90
  # Otherwise the fallback response will be returned.
90
91
  # The main difference is that, when async: false, a fresh request will block until it gets data from the server.
91
92
  # When async: true, a fresh request will try to respond with (possibly stale) cached data ASAP while the new response is cached in the background.
92
- async: false
93
+ async: false,
94
+
95
+ # Pass your own Logger instance (for example Rails.logger in a Rails app).
96
+ # Defaults to STDOUT.
97
+ # http://ruby-doc.org/stdlib-2.1.0/libdoc/logger/rdoc/Logger.html
98
+ # Interface:
99
+ # #debug(msg String, &block)
100
+ # #warn(msg String, &block)
101
+ # #error(msg String, &block)
102
+ # #info(msg String, &block)
103
+ # #fatal(msg String, &block)
104
+ logger: Logger.new(STDOUT)
93
105
  )
94
106
 
95
107
  validate_dep! lock, :lock, :set
@@ -97,6 +109,7 @@ module FaradayThrottler
97
109
  validate_dep! lock_key_resolver, :lock_key_resolver, :call
98
110
  validate_dep! cache_key_resolver, :cache_key_resolver, :call
99
111
  validate_dep! fallbacks, :fallbacks, :call
112
+ validate_dep! logger, :info, :error, :warn, :debug
100
113
 
101
114
  @lock = lock
102
115
  @cache = cache
@@ -108,6 +121,7 @@ module FaradayThrottler
108
121
  @fallbacks = fallbacks
109
122
  @gauge = gauge || Gauge.new(rate: @rate, wait: @wait)
110
123
  @async = async
124
+ @logger = logger
111
125
  validate_dep! @gauge, :gauge, :start, :update, :finish
112
126
 
113
127
  super app
@@ -124,28 +138,32 @@ module FaradayThrottler
124
138
  gauge.start cache_key, start
125
139
 
126
140
  if lock.set(lock_key, gauge.rate(cache_key))
141
+ logger.debug logline(cache_key, "A.1. start backend request. async: #{async?}")
127
142
  if async?
128
143
  handle_async(request_env, cache_key, start)
129
144
  else
130
145
  handle_sync(request_env, cache_key, start)
131
146
  end
132
147
  else
148
+ logger.debug logline(cache_key, "A.2. lock already set. Wait for cache.")
133
149
  serve_from_cache_or_fallback request_env, cache_key, start
134
150
  end
135
151
  end
136
152
 
137
153
  private
138
- attr_reader :app, :lock, :cache, :lock_key_resolver, :cache_key_resolver, :rate, :wait, :timeout, :fallbacks, :gauge
154
+ attr_reader :app, :lock, :cache, :lock_key_resolver, :cache_key_resolver, :rate, :wait, :timeout, :fallbacks, :gauge, :logger
139
155
 
140
156
  def async?
141
157
  @async
142
158
  end
143
159
 
144
160
  def handle_sync(request_env, cache_key, start)
161
+ logger.debug logline(cache_key, "B.1.1. handle sync. Timeout: #{timeout}")
145
162
  with_timeout(timeout) {
146
163
  fetch_and_cache(request_env, cache_key, start)
147
164
  }
148
165
  rescue ::Timeout::Error => e
166
+ logger.error logline(cache_key, "B.1.2. timeout error. Timeout: #{timeout}. Message: #{e.message}")
149
167
  gauge.update cache_key, :timeout
150
168
  serve_from_cache_or_fallback request_env, cache_key, start
151
169
  end
@@ -153,6 +171,7 @@ module FaradayThrottler
153
171
  def handle_async(request_env, cache_key, start)
154
172
  Thread.new do
155
173
  fetch_and_cache(request_env, cache_key, start)
174
+ logger.debug logline(cache_key, "B.2.1. finished async fetch and cache")
156
175
  end
157
176
  serve_from_cache_or_fallback request_env, cache_key, start
158
177
  end
@@ -161,15 +180,18 @@ module FaradayThrottler
161
180
  app.call(request_env).on_complete do |response_env|
162
181
  cache.set cache_key, response_env
163
182
  gauge.finish cache_key, :fresh
183
+ logger.debug logline(cache_key, "C.1.1. finished fetch and cache. Took #{Time.now - start}")
164
184
  debug_headers response_env, :fresh, start
165
185
  end
166
186
  end
167
187
 
168
188
  def serve_from_cache_or_fallback(request_env, cache_key, start)
169
189
  if cached_response = cache.get(cache_key, gauge.wait(cache_key))
190
+ logger.debug logline(cache_key, "C.2.1. serving cached response. Took #{Time.now - start}")
170
191
  gauge.finish cache_key, :cached
171
192
  resp cached_response, :cached, start
172
193
  else
194
+ logger.debug logline(cache_key, "C.2.2. no cached response. Serving fallback response")
173
195
  gauge.finish cache_key, :fallback
174
196
  resp fallbacks.call(request_env), :fallback, start
175
197
  end
@@ -201,6 +223,10 @@ module FaradayThrottler
201
223
  ::Timeout.timeout(seconds, &block)
202
224
  end
203
225
  end
226
+
227
+ def logline(cache_key, line)
228
+ "[Throttler:#{cache_key}] #{line}"
229
+ end
204
230
  end
205
231
 
206
232
  Faraday::Middleware.register_middleware throttler: ->{ Middleware }
@@ -1,3 +1,3 @@
1
1
  module FaradayThrottler
2
- VERSION = "0.0.2"
2
+ VERSION = "0.0.3"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: faraday_throttler
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.0.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ismael Celis
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2016-03-09 00:00:00.000000000 Z
11
+ date: 2016-03-10 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: faraday