Repository navigation
fix(proxy): attach usage-recording listeners before streaming responses (#1222) - #1481
Open
lienhoatienphung-svg wants to merge 1 commit into
Conversation
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
Attaches the
res.once('finish')andres.once('close')usage-recording listeners insrc/routes/proxyRoutes.tsbefore streaming or sending the upstream response body so synchronous or fast-completing responses never miss thefinishevent and aborted streams are not billed.Closes #1222
Key Changes
src/routes/proxyRoutes.ts):res.once('finish', recordUsageOnce)andres.once('close', ...)immediately after setting status/headers and before starting the body stream (await pump()/res.send(text)).recordUsageOncewith!res.writableEnded || streamErroredso mid-stream upstream errors or prematurely closed client connections never record billable usage.billing.chargeUsagefailures with full reconciliation context (userId,apiId,endpointId,requestId,amountUsdc).src/__tests__/usageMetering.test.ts):Verification
npx jest src/__tests__/usageMetering.test.ts(13/13 passed).Payout Wallet (Base Mainnet USDC)
0x3806cd592a508d1992Ed007C2F53de251C2E7452