#447: make off_quota? aware of GitHub Search API quota#448
#447: make off_quota? aware of GitHub Search API quota#448igor-belousov wants to merge 2 commits intozerocracy:masterfrom
Conversation
|
@yegor256 review please |
| stringed = body.is_a?(String) | ||
| return body unless body.is_a?(String) | ||
| JSON.parse(body) | ||
| rescue JSON::ParserError |
There was a problem hiding this comment.
@igor-belousov two terrible design ideas in one line: exception swallowing and returning NULL
There was a problem hiding this comment.
@yegor256 the rescue is there because test_ignores_non_hash_response_body on master feeds 'invalid json' to /rate_limit and expects the middleware to pass it through. Without rescue, that test crashes. Want me to delete the test, or keep one rescue at the call site that logs and raises Fbe::Error?
There was a problem hiding this comment.
feeds 'invalid json' to /rate_limit and expects the middleware to pass it through
why should it pass an invalid JSON through?
There was a problem hiding this comment.
Fair point, removing the test and inlining JSON.parse without rescue
Closes #447. Downstream consumer: zerocracy/judges-action#589.
GitHub has two independent rate limits: core (5000/hr) and search (30/min).
off_quota?previously only checked core, so search endpoints proceededeven when search quota was exhausted, then crashed mid-judge with 403.
Changes:
Fbe::SEARCH_METHODSlists the 6 Octokit search methods.off_quota?acceptsresource: :core | :searchandthreshold:(defaults: 50 for core, 5 for search). For
:searchit readsresources.search.remainingfromlast_response, falls back to corewith a warning if the search payload is unavailable.
interceptedblock routesSEARCH_METHODScalls through thesearch-quota check.
Fbe::Middleware::RateLimitnow tracks@searchleftseparately anddecrements it for
/search/*paths, so the cached/rate_limitresponse reflects accurate search-remaining counts within the
100-call cache window.
builds a fresh body via
patched_body(deep-dup via JSON round-trip)and a
response_envthat dups the original Faraday env, preservinginvariants like
finished?.Tests: 11 new tests across
test/fbe/test_octo.rbandtest/fbe/middleware/test_rate_limit.rbcovering quota routing,threshold defaults, fallback paths, cached-stale scenario across the
100-request window, the cache-body-leak regression, and the
JSON-string body decrement path. 268 tests total, all green, rubocop
clean.
Known follow-ups (separate issues to be filed):
.send/.public_sendon theFbe.octowrapper bypasses thequota guard. The wrapper extends
BasicObject, so:sendlandsin
method_missingwithm = :send. Affects both core andsearch paths, predates this change.
@searchleftwhileoff_quota?reads
last_response. Should consolidate so middleware is thesingle source. Current design works but is fragile (depends on
call ordering).
judges-actionruns sequentially today, so not an immediate issue,but should be guarded with a mutex if concurrent callers are ever
introduced. Marked with an inline NOT thread-safe comment.