Skip to content

Commit

Permalink
Batch destroy conns
Browse files Browse the repository at this point in the history
  • Loading branch information
David Robertson committed Nov 7, 2023
1 parent 78b1e59 commit 4011e38
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 11 deletions.
18 changes: 11 additions & 7 deletions sync3/connmap.go
Original file line number Diff line number Diff line change
Expand Up @@ -181,17 181,21 @@ func (m *ConnMap) connIDsForDevice(userID, deviceID string) []ConnID {
return connIDs
}

// CloseConnsForUser closes all conns for a given user. Returns the number of conns closed.
func (m *ConnMap) CloseConnsForUser(userID string) int {
// CloseConnsForUsers closes all conns for a given slice of users. Returns the number of
// conns closed.
func (m *ConnMap) CloseConnsForUsers(userIDs []string) (closed int) {
m.mu.Lock()
defer m.mu.Unlock()
conns := m.userIDToConn[userID]
logger.Trace().Str("user", userID).Int("num_conns", len(conns)).Msg("closing all device connections due to CloseConn()")
for _, userID := range userIDs {
conns := m.userIDToConn[userID]
logger.Trace().Str("user", userID).Int("num_conns", len(conns)).Msg("closing all device connections due to CloseConn()")

for _, cid := range conns {
m.cache.Remove(cid.String()) // this will fire TTL callbacks which calls closeConn
for _, conn := range conns {
m.cache.Remove(conn.String()) // this will fire TTL callbacks which calls closeConn
}
closed = len(conns)
}
return len(conns)
return closed
}

func (m *ConnMap) closeConnExpires(connID string, value interface{}) {
Expand Down
6 changes: 2 additions & 4 deletions sync3/handler/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -869,10 869,8 @@ func (h *SyncLiveHandler) OnInvalidateRoom(p *pubsub.V2InvalidateRoom) {
}

// 4. Destroy involved users' connections.
var destroyed int
for _, userID := range involvedUsers {
destroyed = h.ConnMap.CloseConnsForUser(userID)
}
// Since creating a conn creates a user cache, it is safe to loop over
destroyed := h.ConnMap.CloseConnsForUsers(unregistered)
if h.destroyedConns != nil {
h.destroyedConns.Add(float64(destroyed))
}
Expand Down

0 comments on commit 4011e38

Please sign in to comment.