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: incorrect local presence #660

Closed
wants to merge 0 commits into from
Closed

Conversation

HolgerHuo
Copy link
Contributor

@HolgerHuo HolgerHuo commented Jan 8, 2025

This pr fixed unstable local presence state.

3 possible optimizations:

  1. As per Matrix spec, presence state sent to client should indicate last_active_ago 0 when user is online, this is done on every client side route in this pr. (src/api/client/sync/v3.rs and src/api/client/presence.rs). Maybe a wrapper could make the conversion more efficient.
  2. For most accurate last online information, refresh_timeout should be disabled entirely, making last_online_timestamp everytime the client sends ping. I hardcoded this to 1m
  3. Some clients (e.g. FluffyChat) seem to be spamming the presence api, causing duplicate timer set up for a single user. Maybe a lock for timer should be implemented.

.services
.users
.list_local_users()
.map(UserId::to_owned)
Copy link
Owner

Choose a reason for hiding this comment

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

I think this can be optimised to avoid needing to call to_owned, but might be wrong. I'll try messing with it and see myself.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I think this can be optimised to avoid needing to call to_owned, but might be wrong. I'll try messing with it and see myself.

I think reference should do the job. I just borrowed this logic from another part of the codebase.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Btw, I just remembered that we should also unset statuses for all users, including those federated. Maybe I could call db for fetching users directly?

Copy link
Owner

Choose a reason for hiding this comment

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

The startup performance impact will be very bad if we include federated users without further significant database optimisations, making this use pure references only instead of copying/to_owned, and optimising this code more.

I think it's best that is left out of this PR for now.

Copy link
Owner

Choose a reason for hiding this comment

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

I think I can optimise enough to allow clearing remote presences on startup for remote users without startup performance taking a toll.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I just realized that the best solution may just be to store presence state in memory, and not persisting it to the database, since they will be removed on startup. Will federated presence information taking up too much memory?

Copy link
Owner

Choose a reason for hiding this comment

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

Yes it will. Presence is designed to track tens of thousands of remotes users. Presence contents can be large such as status messages. Storing them in memory is definitely not a valid option.

Copy link
Collaborator

Choose a reason for hiding this comment

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

I think this can be optimised to avoid needing to call to_owned, but might be wrong. I'll try messing with it and see myself

you have to call to_owned() here before collect()

src/api/client/sync/v3.rs Outdated Show resolved Hide resolved
@girlbossceo
Copy link
Owner

girlbossceo commented Jan 13, 2025

I accidentally force pushed your PR and now I can't edit it to re-open it back it :(

If you'd like to re-open it, use this branch: https://github.com/girlbossceo/conduwuit/tree/strawberry/pr-660-presence-fix

I think the performance impact of clearing all users presence, even remote/federated, is fine after a bit of optimising. So we'll just do that. Subsequent restarts are faster anyways.

@girlbossceo
Copy link
Owner

I've committed my branch with your commits to main branch, effectively merging your PR.

@HolgerHuo
Copy link
Contributor Author

I've committed my branch with your commits to main branch, effectively merging your PR.

Ok! I was on a trip so I didn't reply in time. Thanks for creating such wonderful project.

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.

3 participants