fix(HTTPClient): always emit RETRY_AFTER for 429s; recognise OpenAI headers#228
Open
plenarius wants to merge 1 commit into
Open
fix(HTTPClient): always emit RETRY_AFTER for 429s; recognise OpenAI headers#228plenarius wants to merge 1 commit into
plenarius wants to merge 1 commit into
Conversation
…eaders Before this change, the plugin only broadcast RETRY_AFTER when the response matched a Discord shape (X-RateLimit-Limit present) or a Slack shape (Retry-After present). OpenAI 429 responses do neither -- especially insufficient_quota 429s, which carry no retry hint at all -- so downstream NWScript handlers read an undefined event tag (logged as "Tried to access event data with invalid tag: 'RETRY_AFTER'"), parsed it to 0, and immediately re-fired requests in a tight loop, crashing the NWN server after sustained 429 traffic. This refactor: - Adds ComputeRetryAfterMs() that probes hint headers in priority order: Retry-After-Ms (OpenAI extension), X-RateLimit-Reset-After (Discord float-seconds, more precise than Retry-After), Retry-After (RFC 7231 delay-seconds), then OpenAI's per-bucket X-RateLimit-Reset-Requests / -Tokens with their "1.5s" / "200ms" / "1m30s" duration format. - Always broadcasts RETRY_AFTER (in milliseconds) for every 429, falling back to a 5s default when no header is parseable. Floors at 1s and caps at 1h so a misbehaving server can never busy-loop callers OR park them for absurd durations. - Wraps every numeric parse in try/catch so a malformed header value cannot terminate the response task with an unhandled exception. - Preserves the existing X-RateLimit-Limit/Remaining/Reset Discord metadata broadcasts unchanged. - Includes the computed retry delay in the rate-limit log line for easier debugging.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
The
HTTPClientplugin only broadcastRETRY_AFTERfor Discord-shaped responses (those carryingX-RateLimit-Limit) or Slack-shaped ones (those carryingRetry-After). OpenAI 429s satisfy neither pattern — especiallyinsufficient_quota429s, which omit retry hints entirely — so the script-side handler read an undefined event tag (which logsTried to access event data with invalid tag: 'RETRY_AFTER'), parsed it to 0, and immediately re-fired the request. Sustained 429 traffic from OpenAI tipped Layonara's nwserver over today after a credit grant expired.What this PR does
ComputeRetryAfterMs(response)helper that probes hint headers in priority order:Retry-After-Ms(OpenAI extension — most precise)X-RateLimit-Reset-After(Discord float-seconds — more precise thanRetry-After)Retry-After(RFC 7231 delay-seconds; HTTP-date form is ignored rather than crashed on)X-RateLimit-Reset-Requests/-Tokensparsed via a newParseDurationToMs()that handles"1.5s"/"200ms"/"1m30s"formatsRETRY_AFTER(in milliseconds) for every 429, falling back to a 5s default when no header is parseable. Floored at 1s and capped at 1h so a misbehaving server can never busy-loop callers OR park them for absurd durations.stof()on SlackRetry-Afterhad the same crash exposure.)X-RateLimit-Limit/Remaining/Resetmetadata broadcasts unchanged — callers can still pace requests preemptively from successful responses.Test plan
cmake --build . --target HTTPClientproducesBinaries/NWNX_HTTPClient.soRETRY_AFTERis emitted with a sensible non-zero valueRATELIMIT_LIMIT/REMAINING/RESETmetadata