-
Notifications
You must be signed in to change notification settings - Fork 155
improve Segment.io retries #714
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 6 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
51eb079
improve Segment.io retries by supporting backoff and persisting in-fl…
chrisradek 59b8bd2
add in-flight requests test and support retrying all fetch errors
chrisradek d238154
add changeset
chrisradek cf66c38
update tslib versions
chrisradek 1a77aaf
update yarn.lock
chrisradek 5749ce6
remove debugging console.log
chrisradek f0b02ed
cleanup tests and verify messageId
chrisradek File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| --- | ||
| '@segment/analytics-next': patch | ||
| --- | ||
|
|
||
| Improves Segment.io retries to include exponential backoff and work across page loads. |
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
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| import type { AnalyticsSnippet } from '@segment/analytics-next' | ||
|
|
||
| declare global { | ||
| interface Window { | ||
| analytics: AnalyticsSnippet | ||
| } | ||
| } |
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
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
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
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
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
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -50,12 +50,21 @@ export function segmentio( | |
| settings?: SegmentioSettings, | ||
| integrations?: LegacySettings['integrations'] | ||
| ): Plugin { | ||
| // Attach `pagehide` before buffer is created so that inflight events are added | ||
| // to the buffer before the buffer persists events in its own `pagehide` handler. | ||
| window.addEventListener('pagehide', () => { | ||
| buffer.push(...Array.from(inflightEvents)) | ||
| inflightEvents.clear() | ||
| }) | ||
|
|
||
| const buffer = analytics.options.disableClientPersistence | ||
| ? new PriorityQueue<Context>(analytics.queue.queue.maxAttempts, []) | ||
| : new PersistedPriorityQueue( | ||
| analytics.queue.queue.maxAttempts, | ||
| `dest-Segment.io` | ||
| ) | ||
|
|
||
| const inflightEvents = new Set<Context>() | ||
| const flushing = false | ||
|
|
||
| const apiHost = settings?.apiHost ?? 'api.segment.io/v1' | ||
|
|
@@ -75,6 +84,8 @@ export function segmentio( | |
| return ctx | ||
| } | ||
|
|
||
| inflightEvents.add(ctx) | ||
|
|
||
| const path = ctx.event.type.charAt(0) | ||
| let json = toFacade(ctx.event).json() | ||
|
|
||
|
|
@@ -92,14 +103,15 @@ export function segmentio( | |
| normalize(analytics, json, settings, integrations) | ||
| ) | ||
| .then(() => ctx) | ||
| .catch((err) => { | ||
| if (err.type === 'error' || err.message === 'Failed to fetch') { | ||
| buffer.push(ctx) | ||
| // eslint-disable-next-line @typescript-eslint/no-use-before-define | ||
| scheduleFlush(flushing, buffer, segmentio, scheduleFlush) | ||
| } | ||
| .catch(() => { | ||
| buffer.pushWithBackoff(ctx) | ||
| // eslint-disable-next-line @typescript-eslint/no-use-before-define | ||
| scheduleFlush(flushing, buffer, segmentio, scheduleFlush) | ||
| return ctx | ||
| }) | ||
| .finally(() => { | ||
| inflightEvents.delete(ctx) | ||
| }) | ||
| } | ||
|
|
||
| const segmentio: Plugin = { | ||
|
|
@@ -115,5 +127,11 @@ export function segmentio( | |
| group: send, | ||
| } | ||
|
|
||
| // Buffer may already have items if they were previously stored in localStorage. | ||
| // Start flushing them immediately. | ||
| if (buffer.todo) { | ||
| scheduleFlush(flushing, buffer, segmentio, scheduleFlush) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nice! Not much code for a very nice improvement. |
||
| } | ||
|
|
||
| return segmentio | ||
| } | ||
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -34,6 +34,6 @@ | |
| "dependencies": { | ||
| "@lukeed/uuid": "^2.0.0", | ||
| "dset": "^3.1.2", | ||
| "tslib": "^2.4.0" | ||
| "tslib": "^2.4.1" | ||
| } | ||
| } | ||
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
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
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
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.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good catch on this!