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 (yegor256@gmail.com)

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

Returns a new instance of Quota.



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/fbe/middleware/quota.rb', line 15

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) ⇒ Object



32
33
34
35
36
37
38
39
40
41
# File 'lib/fbe/middleware/quota.rb', line 32

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