fix: call OnDiscard exactly once per item on encode failure#13
Merged
Conversation
Previously flush() called invokeOnDiscard immediately when json.Encode failed, but left the item in the buffer. On every HTTP retry the same item was re-encoded (failing again) and the callback fired a second (third, …) time. Two related issues fixed together: - Collect encode failures in a local slice; call invokeOnDiscard only after a successful HTTP response so retries cannot double-fire it. - If every item in a batch is unencodable the encoded buffer is empty. Guard against sending a zero-byte body (which the server may reject, causing an infinite retry loop): return true immediately and discard all failures once, without making an HTTP request. Co-Authored-By: Claude Sonnet 4.6 (1M context) <noreply@anthropic.com>
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
flushpreviously calledinvokeOnDiscardduring JSON encoding, but left the item in the buffer slice. On every HTTP retry the same item was re-encoded (failing again) and the callback fired a second time. Fixed by collecting encode failures in a local slice and only callinginvokeOnDiscardafter a successful HTTP response.flushstill POSTed a zero-byte body; if the server rejected itretryFlushlooped indefinitely, re-discarding items each iteration. Fixed by returningtrueearly (and discarding all failures once) whenbuf.Len() == 0, skipping the HTTP request entirely.Test plan
TestIngest_EncodeError_DiscardedExactlyOnce_OnHTTPFailure— server fails twice then succeeds; verifiesOnDiscardfires exactly once for the unencodable item despite multiple retriesTestIngest_AllEncodeErrors_NoHTTPRequest— all items unencodable; verifies zero HTTP requests are made andOnDiscardfires once per itemTestIngest_JSONEncodeError_CallsOnDiscardstill passes (happy-path encode error with successful HTTP)go test ./...passes🤖 Generated with Claude Code