package server // chatPermissionRetainedLimit bounds the dedup set of already-pushed permission // request ids one session retains, so a long turn cannot grow it without limit. const chatPermissionRetainedLimit = 256 // seenPermission records requestID as pushed for the session and reports whether // it is newly seen (true) or a duplicate (false). The retained ids are bounded: // the oldest is dropped once the session exceeds chatPermissionRetainedLimit, so // the same pending request is pushed at most once while the state stays bounded. func (h *chatHub) seenPermission(sessionID, requestID string) bool { if sessionID == "" || requestID == "" { return false } h.mu.Lock() defer h.mu.Unlock() turn := h.turnLocked(sessionID) if turn.permissions == nil { turn.permissions = map[string]struct{}{} } if _, ok := turn.permissions[requestID]; ok { return false } turn.permissions[requestID] = struct{}{} turn.permissionOrder = append(turn.permissionOrder, requestID) if len(turn.permissionOrder) > chatPermissionRetainedLimit { oldest := turn.permissionOrder[0] turn.permissionOrder = turn.permissionOrder[1:] delete(turn.permissions, oldest) } return true } // forgetPermission releases a request id reserved by seenPermission when its // frame could not be written, so a later poll re-pushes it instead of the id // staying "seen" while the phone never received the request. func (h *chatHub) forgetPermission(sessionID, requestID string) { if sessionID == "" || requestID == "" { return } h.mu.Lock() defer h.mu.Unlock() turn := h.turns[sessionID] if turn == nil { return } delete(turn.permissions, requestID) for i, id := range turn.permissionOrder { if id == requestID { turn.permissionOrder = append(turn.permissionOrder[:i], turn.permissionOrder[i+1:]...) break } } }