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

⚡️ Deduplicate fetch() network requests #610

Merged
merged 1 commit into from
May 16, 2023
Merged

Conversation

alecgibson
Copy link
Collaborator

@alecgibson alecgibson commented May 9, 2023

At the moment, if a client calls doc.fetch() with a fetch request already in progress, the client will send a second request, which probably isn't needed (or wanted).

This change prevents duplicate requests from being sent when there's already an inflight fetch request, and adds an internal force option, which can be used to force a duplicate request to be sent. This is currently used internally for _hardRollback(), which forcibly unsets the doc.version, and therefore requires a separate fetch() call, since fetch requests are handled differently depending on the presence of version.

@coveralls
Copy link

coveralls commented May 9, 2023

Coverage Status

Coverage: 97.512% (-0.003%) from 97.515% when pulling 05a74aa on merge-inflight-fetch into 21750e3 on master.

@Qquanwei
Copy link

This solution looks good, but will it cause a situation where a request will never be sent after a fetch fails. So I feel that some more cases can be added, maybe it would be better to use throttle?

@alecgibson
Copy link
Collaborator Author

@Qquanwei why wouldn't subsequent fetches succeed? We clear inflight even if there's an error, so a second fetch will send another request. Do you have a failing test case?

@Qquanwei
Copy link

Thank you for your reply. I re-checked the code and found that inflightFetch will be cleared when the fetch error occurs. If this logic can work normally (for example, it can work normally without receiving any fetch reply), then this pr Should solve my problem.

At the moment, if a client calls `doc.fetch()` with a fetch request
already in progress, the client will send a second request, which
probably isn't needed (or wanted).

This change prevents duplicate requests from being sent when there's
already an inflight fetch request, and adds an internal `force` option,
which can be used to force a duplicate request to be sent. This is
currently used internally for `_hardRollback()`, which forcibly unsets
the `doc.version`, and therefore requires a separate `fetch()` call,
since fetch requests are [handled differently][1] depending on the
presence of `version`.

[1]: https://github.com/share/sharedb/blob/21750e320f6865ee14620d6bf9610cb7ebc5f18e/lib/agent.js#L607
@alecgibson alecgibson merged commit 0616549 into master May 16, 2023
4 checks passed
@alecgibson alecgibson deleted the merge-inflight-fetch branch May 16, 2023 16:20
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

4 participants