Skip to content

Commit

Permalink
#59 fixed
Browse files Browse the repository at this point in the history
  • Loading branch information
yegor256 committed Aug 10, 2024
1 parent 57246eb commit 4a9bd86
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 11 deletions.
11 changes: 7 additions & 4 deletions lib/fbe/middleware/quota.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,21 +26,24 @@

# Faraday Middleware that monitors GitHub API rate limits.
class Fbe::Middleware::Quota < Faraday::Middleware
def initialize(app, logger: Loog::NULL, pause: 60, limit: 100, rate: 5)
def initialize(app, loog: Loog::NULL, pause: 60, limit: 100, rate: 5)
super(app)
@limit = limit
@requests = 0
@app = app
@logger = logger
@loog = loog
raise 'The "pause" cannot be nil' if pause.nil?
@pause = pause
raise 'The "limit" cannot be nil' if limit.nil?
@limit = limit
raise 'The "rate" cannot be nil' if rate.nil?
@rate = rate
end

def call(env)
@requests += 1
response = @app.call(env)
if out_of_limit?(env)
@logger.info(
@loog.info(
"Too much GitHub API quota consumed, pausing for #{@pause} seconds"
)
sleep(@pause)
Expand Down
6 changes: 1 addition & 5 deletions lib/fbe/octo.rb
Original file line number Diff line number Diff line change
Expand Up @@ -79,11 +79,7 @@ def Fbe.octo(options: $options, global: $global, loog: $loog)
methods: [:get],
backoff_factor: 2
)
builder.use(
Fbe::Middleware::Quota,
logger: loog,
pause: options.github_api_pause
)
builder.use(Fbe::Middleware::Quota, loog:, pause: options.github_api_pause || 60)
builder.use(Faraday::HttpCache, serializer: Marshal, shared_cache: false, logger: Loog::NULL)
builder.use(Octokit::Response::RaiseError)
builder.use(Faraday::Response::Logger, Loog::NULL)
Expand Down
4 changes: 2 additions & 2 deletions test/fbe/middleware/test_quota.rb
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ def test_quota_middleware_pauses_when_quota_low
loog = Loog::NULL
pause = 0
app = FakeApp.new
middleware = Fbe::Middleware::Quota.new(app, logger: loog, pause:)
middleware = Fbe::Middleware::Quota.new(app, loog:, pause:)
start_time = Time.now
105.times do
env = Judges::Options.new(
Expand All @@ -66,7 +66,7 @@ def test_quota_middleware_logs_when_quota_low
log_output = StringIO.new
loog = Logger.new(log_output)
app = FakeApp.new
middleware = Fbe::Middleware::Quota.new(app, logger: loog, pause:)
middleware = Fbe::Middleware::Quota.new(app, loog:, pause:)
105.times do
env = Judges::Options.new(
'method' => :get,
Expand Down

0 comments on commit 4a9bd86

Please sign in to comment.