Skip to content

improve Segment.io retries#714

Merged
chrisradek merged 7 commits intomasterfrom
segment-retries
Dec 9, 2022
Merged

improve Segment.io retries#714
chrisradek merged 7 commits intomasterfrom
segment-retries

Conversation

@chrisradek
Copy link
Copy Markdown
Contributor

@chrisradek chrisradek commented Dec 7, 2022

This PR improves retry handling for the Segment.io plugin. It also fixes an issue where PersistedPriorityQueue was no longer storing events in localStorage on pagehide.

Fix PersistedPriorityQueue

The PersistedPriorityQueue listens for the pagehide event then stores all of its events in localStorage. Currently, it listens for the pagehide event to be emitted on document, but pagehide is a Window event. This caused the queue to stop working with pagehide.

Apply backoff to segment retries

Currently, when fetch errors are encountered events are pushed back onto the buffer via buffer.push(ctx). This does not apply any backoff, so this is updated to buffer.pushWithBackoff(ctx). This causes the event to be part of the buffer's todo length. The schedule-flush function already checks buffer.todo > 0, so we're covered here.

Store canceled events

In the scenario where the user navigates away from the page between while there are pending HTTP requests, those events are not currently persisted in localStorage. This change keeps track of events that are 'in-flight' and adds them to the persisted priority queue.

Testing

I was able to add tests for this using playwright. Also tested manually with Chrome and Firefox.

[x] I've included a changeset (psst. run yarn changeset. Read about changesets here).

@changeset-bot
Copy link
Copy Markdown

changeset-bot bot commented Dec 7, 2022

🦋 Changeset detected

Latest commit: f0b02ed

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
@segment/analytics-next Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@chrisradek chrisradek changed the title improve Segment.io retries by improve Segment.io retries Dec 7, 2022
@chrisradek chrisradek marked this pull request as ready for review December 9, 2022 02:28
@chrisradek chrisradek requested a review from a team December 9, 2022 02:28

// using document instead of window because of bug affecting browsers before safari 14 (detail in footnotes https://caniuse.com/?search=visibilitychange)
document.addEventListener('pagehide', () => {
window.addEventListener('pagehide', () => {
Copy link
Copy Markdown
Contributor

@silesky silesky Dec 9, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch on this!

// Buffer may already have items if they were previously stored in localStorage.
// Start flushing them immediately.
if (buffer.todo) {
scheduleFlush(flushing, buffer, segmentio, scheduleFlush)
Copy link
Copy Markdown
Contributor

@silesky silesky Dec 9, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nice! Not much code for a very nice improvement.

@chrisradek chrisradek merged commit 9fc8f43 into master Dec 9, 2022
@chrisradek chrisradek deleted the segment-retries branch December 9, 2022 17:38
@github-actions github-actions bot mentioned this pull request Dec 9, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants