feat(core): integrate TAPI backoff v2 into upload flow (3/3)#1151
Closed
feat(core): integrate TAPI backoff v2 into upload flow (3/3)#1151
Conversation
This was referenced Mar 6, 2026
Contributor
Author
|
Note: Integration tests for the upload flow will be added in a separate PR to keep the review focused on the implementation. |
Integrates backoff infrastructure and error classification into the SegmentDestination upload flow. This completes the TAPI backoff v2 implementation. SegmentDestination changes: - Upload gate: Check canUpload() before processing batches - Batch lifecycle: Create metadata on first failure only - Error handling: - 429 rate limit: Call handle429() and halt upload loop - Transient errors: Create/update batch metadata for retry - Permanent errors: Drop batch immediately - Network errors: Treat as transient - Retry count header: Send X-Retry-Count (batch or global) - Dynamic import: Lazy load backoff module to avoid circular deps - Clean initialization: Info-level logs for normal operations - Error logging: All catch blocks now log errors appropriately API changes: - Add keepalive: true to fetch for background completion - Add retryCount parameter for X-Retry-Count header Logger changes: - Default to disabled in production (process.env.NODE_ENV check) Key improvements from code review: - No console.log statements - Simplified upload gate logic - Proper error logging throughout - Appropriate log levels (info for normal, error for failures) This PR depends on PRs #1 (infrastructure) and #2 (error classification). Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
8aab67f to
a0f8449
Compare
This was referenced Mar 6, 2026
abueide
added a commit
that referenced
this pull request
Mar 6, 2026
Adds comprehensive extended test suites for edge cases and implementation details that supplement the core tests in the feature PRs: - UploadStateMachine.extended.test.ts: disabled config, multiple retries, getter tests, persistence tests - BatchUploadManager.extended.test.ts: unique IDs, disabled config, getter tests, detailed exponential backoff algorithm tests, persistence tests - SegmentDestination.extended.test.ts: state reset after success, legacy behavior, Retry-After header parsing These tests provide thorough coverage of edge cases, backwards compatibility, and implementation details without adding bulk to the core feature PRs. Related to PRs: #1150, #1151, #1152, #1153 Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
Removed nice-to-have TAPI integration tests: - resets state after successful upload (happy path detail) - uses legacy behavior when httpConfig.enabled = false (backwards compat) - parses Retry-After header correctly (implementation detail) - uses default retry-after when header missing (edge case) These tests now live in SegmentDestination.extended.test.ts on the feature/tapi-extended-tests branch for separate review. Reduces test file from 945 to 821 lines (-124 lines). Related: feature/tapi-extended-tests Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
Removed all inline comments from SegmentDestination.ts and errors.ts. Documentation should live in docs, not in code comments. Reduces SegmentDestination.ts from 409 to 372 lines (-37 lines) Reduces errors.ts from 123 to 119 lines (-4 lines) Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
Contributor
Author
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.
Overview
This PR integrates the backoff infrastructure and error classification system into the SegmentDestination upload flow, completing the TAPI backoff v2 implementation.
Related PRs:
SegmentDestination Changes
Upload Gate
Before processing any batches, the upload flow now checks if uploads are allowed based on the rate limit state:
Batch Metadata Lifecycle
Batch metadata is now created lazily to avoid unnecessary persistence overhead:
createBatch()handleRetry()removeBatch()Error Handling by Type
Rate Limit (429):
When a 429 response is received, the upload state machine is updated with the retry-after duration and the upload loop halts immediately to prevent wasted API calls.
Transient Errors (5xx, network failures):
Batch metadata is created or updated with exponential backoff timing. The upload loop continues to the next batch, allowing other batches to be attempted.
Permanent Errors (4xx):
The batch is immediately dropped and removed from the queue. No retry is attempted.
Retry Count Header
The implementation now sends an
X-Retry-Countheader with each upload request, using either the per-batch retry count or the global retry count:Dynamic Module Import
The backoff module is loaded dynamically to avoid circular dependencies:
This approach ensures the settings promise only resolves after backoff components are initialized, preventing race conditions.
API Changes
keepalive Flag
The
fetchrequest now includes thekeepaliveflag to ensure uploads can complete even if the app is backgrounded or closed:X-Retry-Count Header
A new header is included in upload requests to help with debugging and monitoring:
Logger Changes
The logger constructor now defaults to disabled in production environments:
This prevents unexpected logging output in production builds while maintaining debug capability in development.
Code Quality Improvements
This PR includes several improvements from code review:
Initialization Flow
backoffInitializedflag is set to trueIf initialization fails at any step, uploads proceed without backoff (graceful degradation).
Testing
Manual testing has verified the following scenarios:
Dependencies
This PR depends on #1149 and #1150 being merged first, as it integrates the components added in those PRs.