Class: Fbe::Middleware::Quota
- Inherits:
-
Faraday::Middleware
- Object
- Faraday::Middleware
- Fbe::Middleware::Quota
- 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
-
#call(env) ⇒ Faraday::Response
Process the request and handle rate limiting.
-
#initialize(app, loog: Loog::NULL, pause: 60, limit: 100, rate: 5) ⇒ Quota
constructor
Constructor.
Constructor Details
#initialize(app, loog: Loog::NULL, pause: 60, limit: 100, rate: 5) ⇒ Quota
Constructor.
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.
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 |