From 4a9bd8676d20ce65b7cb09934d4fc1e5a1d85e8f Mon Sep 17 00:00:00 2001 From: Yegor Bugayenko Date: Sat, 10 Aug 2024 08:38:13 +0300 Subject: [PATCH] #59 fixed --- lib/fbe/middleware/quota.rb | 11 +++++++---- lib/fbe/octo.rb | 6 +----- test/fbe/middleware/test_quota.rb | 4 ++-- 3 files changed, 10 insertions(+), 11 deletions(-) diff --git a/lib/fbe/middleware/quota.rb b/lib/fbe/middleware/quota.rb index 09c05d3..0504b78 100644 --- a/lib/fbe/middleware/quota.rb +++ b/lib/fbe/middleware/quota.rb @@ -26,13 +26,16 @@ # 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 @@ -40,7 +43,7 @@ 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) diff --git a/lib/fbe/octo.rb b/lib/fbe/octo.rb index 70c1371..cc78f03 100644 --- a/lib/fbe/octo.rb +++ b/lib/fbe/octo.rb @@ -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) diff --git a/test/fbe/middleware/test_quota.rb b/test/fbe/middleware/test_quota.rb index 4a6af41..3cbc04e 100644 --- a/test/fbe/middleware/test_quota.rb +++ b/test/fbe/middleware/test_quota.rb @@ -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( @@ -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,