Class: Fbe::Middleware::Quota

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

Overview

Faraday Middleware that monitors GitHub API rate limits.

Author

Yegor Bugayenko ([email protected])

Copyright

Copyright © 2024-2025 Zerocracy

License

MIT

Instance Method Summary collapse

Constructor Details

#initialize(app, loog: Loog::NULL, pause: 60, limit: 100, rate: 5) ⇒ Quota

Constructor.

Parameters:

  • app (Object)

    The Faraday app

  • loog (Loog) (defaults to: Loog::NULL)

    The logging facility

  • pause (Integer) (defaults to: 60)

    Seconds to pause when rate limit is reached

  • limit (Integer) (defaults to: 100)

    Maximum number of requests before checking rate limit

  • rate (Integer) (defaults to: 5)

    Minimum remaining requests threshold



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/fbe/middleware/quota.rb', line 22

def initialize(app, loog: Loog::NULL, pause: 60, limit: 100, rate: 5)
  super(app)
  @requests = 0
  @app = app
  raise 'The "loog" cannot be nil' if loog.nil?
  @loog = loog
  raise 'The "pause" cannot be nil' if pause.nil?
  raise 'The "pause" must be a positive integer' unless pause.positive?
  @pause = pause
  raise 'The "limit" cannot be nil' if limit.nil?
  raise 'The "limit" must be a positive integer' unless limit.positive?
  @limit = limit
  raise 'The "rate" cannot be nil' if rate.nil?
  raise 'The "rate" must be a positive integer' unless rate.positive?
  @rate = rate
end

Instance Method Details

#call(env) ⇒ Faraday::Response

Process the request and handle rate limiting.

Parameters:

  • env (Faraday::Env)

    The environment

Returns:

  • (Faraday::Response)

    The response



43
44
45
46
47
48
49
50
51
52
# File 'lib/fbe/middleware/quota.rb', line 43

def call(env)
  @requests += 1
  response = @app.call(env)
  if out_of_limit?(env)
    @loog.info("Too much GitHub API quota consumed, pausing for #{@pause} seconds")
    sleep(@pause)
    @requests = 0
  end
  response
end