From 14eaafe0abddf23a2989854c0c2afceead0c6838 Mon Sep 17 00:00:00 2001 From: VasilevNStas Date: Sat, 11 Jul 2026 10:05:34 +0300 Subject: [PATCH] Add untrack_request to Elegant/GoodMethodName allowed list --- .rubocop.yml | 1 + lib/fbe/middleware/rate_limit.rb | 14 ++++++++++++++ 2 files changed, 15 insertions(+) diff --git a/.rubocop.yml b/.rubocop.yml index d23c68fd..66fb3b66 100644 --- a/.rubocop.yml +++ b/.rubocop.yml @@ -100,6 +100,7 @@ Elegant/GoodMethodName: - total_issues_created - total_releases_published - track_request + - untrack_request - unmask_repos - update_organization_membership - user_name_by_id diff --git a/lib/fbe/middleware/rate_limit.rb b/lib/fbe/middleware/rate_limit.rb index 2088be15..1b4373df 100644 --- a/lib/fbe/middleware/rate_limit.rb +++ b/lib/fbe/middleware/rate_limit.rb @@ -50,6 +50,9 @@ def call(env) @lock.synchronize { sync(response_env, env.url.path) } end end + rescue StandardError + @lock.synchronize { untrack_request(env.url.path) } unless env.url.path == '/rate_limit' + raise end # Returns the remaining requests count tracked by this middleware. @@ -91,6 +94,17 @@ def track_request(path = nil) end end + # Reverts the counter decrement when the request fails. + def untrack_request(path = nil) + return unless @counter&.positive? + @counter -= 1 + if path&.start_with?('/search/') + @searchleft += 1 if @searchleft&.positive? + elsif @remaining&.positive? + @remaining += 1 + end + end + # Syncs the internal remaining count from a real API response header. # # When the response was served by Faraday::HttpCache from cache