Class: FaradayThrottler::Middleware

Inherits:
Faraday::Middleware
  • Object
show all
Defined in:
lib/faraday_throttler/middleware.rb

Instance Method Summary collapse

Constructor Details

#initialize(app, enabled: true, cache: Cache.new, cache_key_resolver: KeyResolver.new, rate: 10, wait: 1, timeout: 0, rate_limit_response_checker: RateLimitResponseChecker.new, logger: Logger.new(STDOUT)) ⇒ Middleware

Returns a new instance of Middleware.



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/faraday_throttler/middleware.rb', line 10

def initialize(
    # The base Faraday adapter.
    app,

    # enable the throller
    enabled: true,

    # Sticks cache.
    cache: Cache.new,

    # Resolves response unique key to use as cache key
    # Interface:
    #   #call(response_env Hash) String
    cache_key_resolver: KeyResolver.new,

    # Maximum requests to sent to the backend api simultanous
    rate: 10,

    # Queued requests will wait for up to 1 seconds for current in-flight request
    # to the same path.
    # If in-flight request hasn't finished after that time, return a default placeholder response.
    wait: 1,

    # Wraps requests to backend service in a timeout block, in seconds.
    # timeout: 0 disables this behaviour.
    timeout: 0,

    # Use to check if backend api response is a rate limit
    rate_limit_response_checker: RateLimitResponseChecker.new,

    # Pass your own Logger instance (for example Rails.logger in a Rails app).
    # Defaults to STDOUT.
    # http://ruby-doc.org/stdlib-2.1.0/libdoc/logger/rdoc/Logger.html
    # Interface:
    #   #debug(msg String, &block)
    #   #warn(msg String, &block)
    #   #error(msg String, &block)
    #   #info(msg String, &block)
    #   #fatal(msg String, &block)
    logger: Logger.new(STDOUT)
)

  validate_dep! cache, :cache, :get, :set
  validate_dep! cache_key_resolver, :cache_key_resolver, :call
  validate_dep! rate_limit_response_checker, :rate_limit_response_checker, :call
  validate_dep! logger, :info, :error, :warn, :debug

  @enabled = enabled
  @cache = cache
  @cache_key_resolver = cache_key_resolver
  @rate = rate.to_i
  @wait = wait.to_i
  @timeout = timeout.to_i
  @rate_limit_response_checker = rate_limit_response_checker
  @logger = logger

  super app
end

Instance Method Details

#call(request_env) ⇒ Object



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/faraday_throttler/middleware.rb', line 69

def call(request_env)
  return app.call(request_env) if skip?(request_env)

  start = Time.now

  cache_key = cache_key_resolver.call(request_env)

  # Wait stick to be available
  until request_stick?(cache_key)
    logger.debug logline(cache_key, "A.1. No stick available. Wait for new one.")
    sleep(wait)
  end

  logger.debug logline(cache_key, "A.2. start backend request.")
  handle_request(request_env, cache_key, start)
end