#450: Use one rate-limit counter source#490
Conversation
edmoffo
left a comment
There was a problem hiding this comment.
The change consolidates the search-quota source in the rate-limit middleware and removes the last_response parsing in off_quota?, which is the fix the linked issue asked for. The new update step folds the lower x-ratelimit-remaining header into the cached count, which keeps the cached rate_limit response conservative between refreshes. The two added tests cover the search-quota fallback path and the header-driven decrement. CI is green.
edmoffo
left a comment
There was a problem hiding this comment.
Scope creep: this branch reverts unrelated upgrades on master rather than only addressing #450. The diff downgrades crate-ci/typos from v1.47.1 to v1.46.3 in .github/workflows/typos.yml, drops TargetRubyVersion from 3.3 back to 3.2 in .rubocop.yml, and removes several Elegant/* cop entries that exist on master. Gemfile.lock, fbe.gemspec, test/fbe/test_iterate.rb, test/fbe/test_overwrite.rb, and test/fbe/middleware/test_sqlite_store.rb are also touched without any link to the rate-limit work described in the title and body. Rebase onto current master and drop everything that does not belong to the #450 fix, then resubmit.
Separately on the core change: update at lib/fbe/middleware/rate_limit.rb:122 takes the min of the stored count and the value from x-ratelimit-remaining, so @remaining becomes monotonically non-increasing between /rate_limit polls. This is fine if every reset is observed by the next /rate_limit refresh (every 100 requests in handle_rate_limit_request), but please add a test that exercises the reset path so a future change does not turn the conservative-min into a permanent floor.
edmoffo
left a comment
There was a problem hiding this comment.
The change folds two divergent counter sources into one and the test pins the regression off_quota? could fall through to. CI is green on every checker and the rate-limit suite passes locally (17/17). The new constructor signature stays backward-compatible via the optional tracker arg, and the middleware retains its tracked search count when last_response loses the resources.search hash.
edmoffo
left a comment
There was a problem hiding this comment.
Read the diff against issue #450. The middleware now owns both @remaining and @searchleft, and off_quota? reads them through the new remaining(resource) accessor instead of reparsing last_response.body. The change matches the stated intent and removes the divergence the issue describes.
Local validation on the checked-out head:
bundle exec ruby -Itest test/fbe/middleware/test_rate_limit.rb— 17 tests, 34 assertions, 0 failures.bundle exec ruby -Itest test/fbe/test_octo.rb -n test_off_quota_search_uses_middleware_count_when_last_response_loses_search_resource— 1 test, 1 assertion, 0 failures.bundle exec ruby -Itest test/fbe/test_octo.rb -n test_pauses_when_quota_is_exceeded— 1 test, 2 assertions, 0 failures.
All 10 exposed checks are green. No inline comments.
bibonix
left a comment
There was a problem hiding this comment.
Two notes inline: the new search test does not exercise the failure mode named in its method name, since the new off_quota? path never reads last_response; and update's min-fold on @Remaining keeps it pinned at the zero initial value until a /rate_limit request seeds it, mirroring the nil-guard used for @searchleft.
| if path&.start_with?('/search/') | ||
| @searchleft = @searchleft.nil? ? count : [@searchleft, count].min | ||
| else | ||
| @remaining = [@remaining, count].min |
There was a problem hiding this comment.
If a non /rate_limit request lands before any /rate_limit call, @Remaining is still 0 from initialize and [0, count].min stays 0 forever, even though the response header reports the real remaining count. The :search path avoids this with a nil sentinel and the @searchleft.nil? ? count : [@searchleft, count].min idiom one line above; the :core path needs the same shape, either by initializing @Remaining to nil or by special-casing the zero case here.
| ) | ||
| end | ||
|
|
||
| def test_off_quota_search_uses_middleware_count_when_last_response_loses_search_resource |
There was a problem hiding this comment.
The name promises coverage of the case where last_response loses resources.search, but off_quota? in this PR no longer reads last_response at all. The monkey-patched rate_limit! / last_response on the client is dead weight; the assertion passes purely because the middleware tracked search=1 from the initial /rate_limit stub. Either rename the test to what it actually verifies (middleware count flows through to off_quota?) or drop the singleton method overrides.
|
@edmoffo Hey! Nice work on that review 👍 You snagged +13 points this time: started with the standard +18 base points, but lost -5 since we only saw 2 comments (our policy encourages more discussion to help everyone improve). Your running score is now +1549 - keep those reviews coming and don't forget to peek at your Zerocracy account for the latest updates! |
|
Addressed both review points. Latest head is Changes:
Validation:
I also tried the full |
GHX5T-SOL
left a comment
There was a problem hiding this comment.
Reviewed current head e7b18625f4b66c702f3e89ffb78071093ebdd780 after the follow-up for the earlier review notes.
The branch is now scoped to #450: Fbe::Middleware::RateLimit owns the core/search counters, Fbe.octo.off_quota? reads through that middleware instead of reparsing last_response, and the non-/rate_limit response-header path seeds the counter when the cached /rate_limit payload has not done so yet. The new search regression now makes a real search call first, so the middleware-owned search count is what drives the later off_quota?(resource: :search) decision.
Validation:
bundle checkruby -c lib/fbe/middleware/rate_limit.rb && ruby -c lib/fbe/octo.rb && ruby -c test/fbe/middleware/test_rate_limit.rb && ruby -c test/fbe/test_octo.rbgit diff --check origin/master...HEADbundle exec ruby -Itest test/fbe/middleware/test_rate_limit.rb -- --no-cov-> 17 tests, 33 assertions, 0 failures, 0 errorsbundle exec ruby -Itest test/fbe/test_octo.rb -- --no-cov-> 64 tests, 182 assertions, 0 failures, 0 errors, 2 skipsbundle exec rake test-> 285 tests, 805 assertions, 0 failures, 0 errors, 9 skipsbundle exec rubocop-> 65 files inspected, no offenses
Hosted checks are green on this head, including both rake jobs.
|
@GHX5T-SOL Hey! Nice work on that review! 🎉 You snagged +13 points this time: started with the standard +18 base reward, but lost -5 for having only 2 comments (we need at least 9 to avoid the penalty). Your running total is now +1143 - keep those detailed reviews coming to maximize your bonus points! Don't forget to peek at your Zerocracy account too. |
|
@akmhatey-ai merge conflicts here |
|
Please fix the merge conflicts so this pull request can be merged. |
|
Merged current Notes:
Validation:
I did not claim the full local Windows rake run as green because the unrelated SQLite temp-file cleanup tests still hit Windows-specific |
What changed
Fbe::Middleware::RateLimitexpose its tracked remaining count for core and search resources.off_quota?(resource: :search)read the search count from that middleware-owned source instead of reparsinglast_response.body./rate_limitpayload has noresources.searchentry.X-RateLimit-Remainingvalues from normal API responses into the middleware counter so the cached/rate_limitresponse stays conservative.Why
off_quota?andFbe::Middleware::RateLimitwere tracking search quota in two places. Iflast_response.bodyno longer hadresources.search,off_quota?(resource: :search)could fall back to the core count even though the middleware had already tracked the search count.Validation
bundle exec ruby -Itest test/fbe/test_octo.rb -n test_off_quota_search_uses_middleware_count_when_last_response_loses_search_resource- passedbundle exec ruby -Itest test/fbe/test_octo.rb -n test_print_trace- passedbundle exec ruby -Itest test/fbe/test_octo.rb -n test_pauses_when_quota_is_exceeded- passedbundle exec ruby -Itest test/fbe/middleware/test_rate_limit.rb- passed, 17 tests / 34 assertionsbundle exec rubocop lib/fbe/middleware/rate_limit.rb lib/fbe/octo.rb test/fbe/test_octo.rb test/fbe/middleware/test_rate_limit.rb- passed, no offensesgit diff --check- passedgitleaks detect --source . --redact --no-banner- passed, no leaks foundLimitation
bundle exec ruby -Itest test/fbe/test_octo.rbstill has six Windows-localErrno::EACCESerrors while deleting SQLite temp DB files under%LOCALAPPDATA%\Temp; the non-SQLite tests in that file pass.