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

pollInterval is broken, skips every other interval #4786

Closed
voxtex opened this issue May 7, 2019 · 2 comments
Closed

pollInterval is broken, skips every other interval #4786

voxtex opened this issue May 7, 2019 · 2 comments

Comments

@voxtex
Copy link

voxtex commented May 7, 2019

Intended outcome:
Query pollInterval should refresh data every x ms.

Actual outcome:
Every other interval is skipped due to a bug in the polling logic.

How to reproduce the issue:
https://codesandbox.io/s/9jwwplw56y

Versions
2.5.1

pollInterval is broken as demonstrated by the example above (open up your network console). A 5 second pollInterval is only polling every 10 seconds due to the way it is implemented.

Because lastPollTime is updated asynchronously after the query completes while the setTimeout is queued up immediately, lastPollTime will always be now() - (timeLimitMs + fetchDuration) which will never be <= (now - timeLimitMs) on the subsequent timeout. It will always skip the immediately following timeout execution and only pass the condition on the next one.

https://github.com/apollographql/apollo-client/blob/master/packages/apollo-client/src/core/QueryManager.ts#L1604

For a concrete example, imagine pollInterval = 5s, a query takes 1s to execute, and now = 0. We execute the query, queue up a timeout execution 5s from now (5s), and then the query comes back and we update lastPollTime (1s). So on timeout execution (now - lastPollTime) >= interval is (5 - 1) >= 5 which is false and we have to wait for another timeout execution before anything will happen.

Finally, is the batched polling functionality documented anywhere? I feel like this needs to be documented and also have an option to opt out. If I have 2 queries on the page with separate intervals of 9s and 10s, it will take 18s (!) for the 10s poll interval to actually execute. This does not seem like a good default behavior and was completely unexpected when I was working with the client.

@benjamn benjamn self-assigned this May 8, 2019
@benjamn benjamn added this to the Release 2.6.0 milestone May 10, 2019
benjamn added a commit that referenced this issue May 10, 2019
The last time I touched this code in #4243, I thought it was worthwhile to
preserve the behavior of sometimes batching polling query fetches in the
same tick of the event loop.

However, as @voxtex points out in #4786, almost any batching strategy will
lead to unpredictable fetch timing, skipped fetches, poor accounting for
fetch duration (which could exceed the polling interval), and so on. On
top of that, even the premise that "batching is good" comes into question
if it doesn't happen consistently.

An implementation which uses independent timers seems much simpler and
more predictable.
benjamn added a commit that referenced this issue May 10, 2019
The last time I touched this code in #4243, I thought it was worthwhile to
preserve the behavior of sometimes batching polling query fetches in the
same tick of the event loop.

However, as @voxtex points out in #4786, almost any batching strategy will
lead to unpredictable fetch timing, skipped fetches, poor accounting for
fetch duration (which could exceed the polling interval), and so on. On
top of that, even the premise that "batching is good" comes into question
if it doesn't happen consistently.

An implementation which uses independent timers seems much simpler and
more predictable.
@voxtex
Copy link
Author

voxtex commented May 11, 2019

Just wanted to say thanks for working on this, very helpful!

@benjamn
Copy link
Member

benjamn commented May 21, 2019

Closing since we just published the final version of apollo-client@2.6.0 to npm, including the fix for this issue! See CHANGELOG.md if you're curious about the other changes in this release.

@benjamn benjamn closed this as completed May 21, 2019
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Feb 16, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

2 participants