3 ms·
> I think that implies the isDone polling loop never completes If the first request -- the one which initiates the fetch -- ends up being canceled for some rea
by kentonv 4y ago
> I think that implies the isDone polling loop never completes
If the first request -- the one which initiates the fetch -- ends up being canceled for some reason, then yeah, the later ones will all end up looping forever waiting for a state change that will never happen. :( You might need some additional code to check if the pending fetch was started unreasonably long ago and if so, assume it is dead and have a new request context take over attempting the fetch.
That said, keep in mind that Cloudflare Workers runs many, many copies of your worker around the world, and as a result caching in the global scope may not be very effective. It's better if you can leverage the Cloudflare cache (either through the Cache API or by formulating your fetch() to be cachable) as that will be shared across the colo (or even region, for tiered caching).
- yencabulator 4y agoYeah, the fetch should be cacheable, I was personally mostly trying to avoid repeating parse+transform work on the fetch results. (I'm neither the author of the blog, or active in the linked issue/project, I just found the blog by googling for the error message I got independently.)
- kentonv 4y agoYeah, I think we need to introduce some sort of API for caching parsed objects in a way that avoids all the caveats of using globals...
- _panva 4y agoHi, @panva here. I've previously had Erwin effectively propose the existing code (not the one from the linked article, the one that's in the library today https://github.com/panva/jose/blob/v4.13.0/src/jwks/remote.ts#L106-L118 https://github.com/panva/jose/blob/v4.13.0/src/jwks/remote.t...) acknowledging that there is an even more unlikely edge case. Well, someone was bound to hit that second edge case. But please help me out on this, the code does not await the _pendingFetch promise and neither does it touch any runtime globals. So what gives? Please reach out on the cloudflare discord PanvA#6705.
- kentonv 4y agoThe code will usually work, but it's possible that the request which initializes `_pendingFetch` gets canceled before the fetch completes, in which case all other requests will loop forever waiting for it.