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

⚡️ Cache latest op version when broadcasting presence #679

Merged
merged 2 commits into from
Aug 27, 2024

Commits on Aug 27, 2024

  1. ⚡️ Cache latest op version when broadcasting presence

    At the moment, when sending a presence update to other subscribers, we
    [call `transformPresenceToLatestVersion()`][1] for every presence update
    which internally [calls `getOps()`][2] for every presence update.
    
    Calls to `getOps()` can be expensive, and rapid presence updates may
    cause undue load on the server, even when the `Doc` has not been
    updated.
    
    This change tries to mitigate this by subscribing to a pubsub stream for
    any `Doc`s that an `Agent` tries to broadcast presence on. We keep an
    in-memory cache of the latest snapshot version sent over this stream,
    which lets us quickly check if a presence broadcast is already current
    without needing to query the database at all.
    
    To avoid leaking streams, the `Agent` will internally handle its stream
    subscription state by:
    
     - subscribing whenever a non-`null` presence update is broadcast
     - unsubscribing whenever a `null` presence update is broadcast
    
    This means that rapid changes in presence being `null` or not can still
    result in database calls, but even in this case they should be less bad
    than before, because we only perform a snapshot fetch instead of ops.
    
    [1]: https://github.com/share/sharedb/blob/297ce5dc66563a5955311793a475768d73ac8b87/lib/agent.js#L804
    [2]: https://github.com/share/sharedb/blob/297ce5dc66563a5955311793a475768d73ac8b87/lib/backend.js#L919
    alecgibson committed Aug 27, 2024
    Configuration menu
    Copy the full SHA
    87ef11d View commit details
    Browse the repository at this point in the history
  2. Review markups

    - Clean up `latestDocVersions` when unsubscribing
    - Fix not setting version if the object already exists
    - Removes `null` presence check, which won't work if the object has been
      destroyed
    alecgibson committed Aug 27, 2024
    Configuration menu
    Copy the full SHA
    4b636fc View commit details
    Browse the repository at this point in the history