Class: TocDoc::Middleware::RateLimiter Private

Inherits:
Faraday::Middleware
  • Object
show all
Defined in:
lib/toc_doc/http/middleware/rate_limiter.rb

Overview

This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.

Faraday middleware that enforces a client-side rate limit via a RateLimiter::TokenBucket.

Placed between the retry middleware and the JSON middleware so each retry attempt is individually rate-limited.

Examples:

bucket = TocDoc::RateLimiter::TokenBucket.new(rate: 5)
builder.use TocDoc::Middleware::RateLimiter, bucket: bucket

Instance Method Summary collapse

Constructor Details

#initialize(app, bucket:) ⇒ RateLimiter

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns a new instance of RateLimiter.

Parameters:



19
20
21
22
# File 'lib/toc_doc/http/middleware/rate_limiter.rb', line 19

def initialize(app, bucket:)
  super(app)
  @bucket = bucket
end

Instance Method Details

#call(env) ⇒ Faraday::Response

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Acquires a token before forwarding the request downstream.

Parameters:

  • env (Faraday::Env)

    the request environment

Returns:

  • (Faraday::Response)


28
29
30
31
# File 'lib/toc_doc/http/middleware/rate_limiter.rb', line 28

def call(env)
  @bucket.acquire
  @app.call(env)
end