Batch unregister users

acquire mutex once, rather than N times
This commit is contained in:
David Robertson 2023-11-07 17:13:05 +00:00
parent 041965ffd0
commit 78b1e5970c
No known key found for this signature in database
GPG Key ID: 903ECE108A39DEDD
2 changed files with 17 additions and 4 deletions

View File

@ -55,10 +55,22 @@ func (d *Dispatcher) Startup(roomToJoinedUsers map[string][]string) error {
return nil
}
func (d *Dispatcher) Unregister(userID string) {
// UnregisterBulk accepts a slice of user IDs to unregister. The given users need not
// already be registered (in which case unregistering them is a no-op). Returns the
// list of users that were unregistered.
func (d *Dispatcher) UnregisterBulk(userIDs []string) []string {
d.userToReceiverMu.Lock()
defer d.userToReceiverMu.Unlock()
delete(d.userToReceiver, userID)
unregistered := make([]string)
for _, userID := range userIDs {
_, exists := d.userToReceiver[userID]
if exists {
delete(d.userToReceiver, userID)
unregistered = append(unregistered, userID)
}
}
return unregistered
}
func (d *Dispatcher) Register(ctx context.Context, userID string, r Receiver) error {

View File

@ -862,8 +862,9 @@ func (h *SyncLiveHandler) OnInvalidateRoom(p *pubsub.V2InvalidateRoom) {
h.Dispatcher.OnInvalidateRoom(p.RoomID, joins, invites)
// 3. Destroy involved users' caches.
for _, userID := range involvedUsers {
h.Dispatcher.Unregister(userID)
// We filter to only those users which had a userCache registered to receive updates.
unregistered := h.Dispatcher.UnregisterBulk(involvedUsers)
for _, userID := range unregistered {
h.userCaches.Delete(userID)
}