Skip to content
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

fix cdn race condition #424

Merged
merged 1 commit into from
Apr 8, 2022
Merged

fix cdn race condition #424

merged 1 commit into from
Apr 8, 2022

Conversation

silesky
Copy link
Contributor

@silesky silesky commented Apr 8, 2022

  • I'm still evaluating if it's safe to remove the setGlobalCDNUrl call from brower-umd.ts and standalone.ts (if anyone has any thoughts / I'm interested!).

@silesky silesky requested a review from chrisradek April 8, 2022 01:32
const [settings, setSettings] = useState<AnalyticsSettings | undefined>(
undefined
)
const [writeKey, setWriteKey] = useLocalStorage<string>('__dev_writeKey', '')
Copy link
Contributor Author

@silesky silesky Apr 8, 2022

Choose a reason for hiding this comment

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

This is a somewhat unrelated change that I'm sneaking in -- I got a bit tired of having the writeKey input not persist between refreshes / server restarts / etc.

Copy link
Contributor

Choose a reason for hiding this comment

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

love it!

@@ -42,6 +39,7 @@ export async function loadIntegration(
): Promise<LegacyIntegration> {
const pathName = normalizeName(name)
const obfuscatedPathName = obfuscatePathName(pathName, obfuscate)
const path = getNextIntegrationsURL()
Copy link
Contributor Author

@silesky silesky Apr 8, 2022

Choose a reason for hiding this comment

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

removing this method call from the global scope makes it easier to reason about, especially since it's a wrapper around a mutable global; we don't need to worry about stale references.

@silesky silesky requested a review from pooyaj April 8, 2022 01:54
Comment on lines 82 to 88
const [writeKey, setWriteKey] = useLocalStorage<string>('__dev_writeKey', '')
const [settings, setSettings] = useState<
AnalyticsBrowserSettings | undefined
>({
writeKey,
cdnURL: 'https://cdn.segment.com',
})
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
const [writeKey, setWriteKey] = useLocalStorage<string>('__dev_writeKey', '')
const [settings, setSettings] = useState<
AnalyticsBrowserSettings | undefined
>({
writeKey,
cdnURL: 'https://cdn.segment.com',
})
const [writeKey, setWriteKey] = useLocalStorage<string>('__dev_writeKey', '')
const [settings, setSettings] = useState<
AnalyticsBrowserSettings | undefined
>({
writeKey: process.env.NEXT_PUBLIC_SEGMENT_WRITE_KEY,
cdnURL: process.env.NEXT_PUBLIC_SEGMENT_CDN_URL,
})

I would not like to teach people to do this ... it will cause issues like caching the wrong value or somebody blocking the localStorage ... you named it.

You do you, but I rather teach people to put such information in the env file since that is probably where people should be injecting such thing, and it will fix your issue

Copy link
Contributor Author

@silesky silesky Apr 8, 2022

Choose a reason for hiding this comment

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

  • I didn't even consider that people might want to override cdnURL, but that's a good idea!
  • on the subject of writeKey -- on a regular application, I agree. However, this is a playground for debugging (run make dev if you haven't yet to see what I mean!) -- so there's a writeKey input field that allows people to swap out writeKeys at runtime. This just makes it easier for people to use their most recent one.

Copy link
Contributor

Choose a reason for hiding this comment

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

I didn't even consider that people might want to override cdnURL, but that's a good idea!

Well, we have to do a proxy to Segment to bypass AdBlocker ... so probably most companies that know a bit about infra would do that most of the time 😄

Copy link
Contributor

Choose a reason for hiding this comment

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

run make dev if you haven't yet to see what I mean!

I see I see, would call the directory "playground" because people are getting used to the idea of using examples directory to learn how to get up to speed with this ... which is my actually worry (not you, but others reading code out of context)

Copy link
Contributor Author

@silesky silesky Apr 8, 2022

Choose a reason for hiding this comment

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

I didn't even consider that people might want to override cdnURL, but that's a good idea!

Well, we have to do a proxy to Segment to bypass AdBlocker ... so probably most companies that know a bit about infra would do that most of the time 😄

Yep, I just meant the few of us who use the playground (just me?) — I’d just seen it used to demo segment and develop locally directly against the default segment cdn. Anyway, now it’s overridable!

Copy link
Contributor Author

Choose a reason for hiding this comment

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

run make dev if you haven't yet to see what I mean!

I see I see, would call the directory "playground" because people are getting used to the idea of using examples directory to learn how to get up to speed with this ... which is my actually worry (not you, but others reading code out of context)

I think it’s meant to serve a duel role, but that’s a good point — it’s not typically what you think of as an ‘example’ in an examples repo!

@@ -17,14 +17,17 @@ const getCDNUrlFromScriptTag = (): string | undefined => {
return cdn
}

let _globalCDN: string | undefined // set globalCDN as in-memory singleton
Copy link
Contributor

Choose a reason for hiding this comment

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

I LOVE IT

src/lib/parse-cdn.ts Outdated Show resolved Hide resolved
@pooyaj
Copy link
Contributor

pooyaj commented Apr 8, 2022

@silesky I think the purpose of the setGlobalCDNUrl in the standalone etc. is to avoid consistently hitting the getCDNUrlFromScriptTag when calling getCDN, and only using getCDNUrlFromScriptTag once ( because it is a relatively costly DOM scan ).

@silesky silesky force-pushed the bug/fix-cdn-race-condition branch 3 times, most recently from 691267f to 2d8f259 Compare April 8, 2022 04:37
@silesky
Copy link
Contributor Author

silesky commented Apr 8, 2022

@silesky I think the purpose of the setGlobalCDNUrl in the standalone etc. is to avoid consistently hitting the getCDNUrlFromScriptTag when calling getCDN, and only using getCDNUrlFromScriptTag once ( because it is a relatively costly DOM scan ).

@pooyaj Ah, If that's true that it's a cache, do you mind if we get rid of it? It seems like an unneeded premature optimization, particularly for a function that's only called during a couple initialization steps. It would make things a lot cleaner and remove the possibility of race conditions (which shouldn't happen in practice, but you have to know about the lifecycle to discount that possibility).

We can do this in additional PRs FWIW

@silesky silesky force-pushed the bug/fix-cdn-race-condition branch 3 times, most recently from 952b810 to c4c2f2c Compare April 8, 2022 19:40
Copy link
Contributor

@chrisradek chrisradek left a comment

Choose a reason for hiding this comment

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

Looks great!

With regards to your question on if we need to setCdn for standalone/browser.umd, I think @pooyaj's explanation makes sense. If we can guarantee that we would only do the script tag lookup once with the changes you've made we can likely remove it - but I think to keep the scope of this PR focused I'd rather create a new issue or JIRA to track making that change/testing it.

],
})
expect(fetchCalls[0][0]).toContain(overriddenCDNUrl)
expect.assertions(3)
Copy link
Contributor

Choose a reason for hiding this comment

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

That's a neat trick 😄

@silesky silesky force-pushed the bug/fix-cdn-race-condition branch from c4c2f2c to 8c04016 Compare April 8, 2022 19:43
@silesky silesky force-pushed the bug/fix-cdn-race-condition branch from 8c04016 to 1266c93 Compare April 8, 2022 19:47
@silesky silesky merged commit 781e2d6 into master Apr 8, 2022
@silesky silesky deleted the bug/fix-cdn-race-condition branch April 8, 2022 19:58
adisbanda pushed a commit to juneHQ/june-analytics-next that referenced this pull request Jul 13, 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.

None yet

5 participants