2017-05-10 04:42:00 -05:00
|
|
|
// Copyright 2017 Vector Creations Ltd
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
2021-01-08 10:59:06 -06:00
|
|
|
package notifier
|
2017-05-10 04:42:00 -05:00
|
|
|
|
|
|
|
import (
|
2017-09-18 10:52:22 -05:00
|
|
|
"context"
|
2017-05-10 04:42:00 -05:00
|
|
|
"sync"
|
2017-10-26 05:34:54 -05:00
|
|
|
"time"
|
2017-05-10 04:42:00 -05:00
|
|
|
|
2022-09-30 10:07:18 -05:00
|
|
|
"github.com/matrix-org/dendrite/internal/sqlutil"
|
2023-06-12 06:19:25 -05:00
|
|
|
"github.com/matrix-org/dendrite/roomserver/api"
|
2023-04-27 06:54:20 -05:00
|
|
|
rstypes "github.com/matrix-org/dendrite/roomserver/types"
|
2017-05-17 09:38:24 -05:00
|
|
|
"github.com/matrix-org/dendrite/syncapi/storage"
|
2017-05-10 04:42:00 -05:00
|
|
|
"github.com/matrix-org/dendrite/syncapi/types"
|
2023-04-19 09:50:33 -05:00
|
|
|
"github.com/matrix-org/gomatrixserverlib/spec"
|
2017-10-26 05:34:54 -05:00
|
|
|
log "github.com/sirupsen/logrus"
|
2017-05-10 04:42:00 -05:00
|
|
|
)
|
|
|
|
|
2022-04-07 11:08:15 -05:00
|
|
|
// NOTE: ALL FUNCTIONS IN THIS FILE PREFIXED WITH _ ARE NOT THREAD-SAFE
|
|
|
|
// AND MUST ONLY BE CALLED WHEN THE NOTIFIER LOCK IS HELD!
|
|
|
|
|
2017-05-17 09:38:24 -05:00
|
|
|
// Notifier will wake up sleeping requests when there is some new data.
|
2019-07-12 09:59:53 -05:00
|
|
|
// It does not tell requests what that data is, only the sync position which
|
2017-05-17 09:38:24 -05:00
|
|
|
// they can use to get at it. This is done to prevent races whereby we tell the caller
|
|
|
|
// the event, but the token has already advanced by the time they fetch it, resulting
|
|
|
|
// in missed events.
|
2017-05-10 04:42:00 -05:00
|
|
|
type Notifier struct {
|
2023-06-12 06:19:25 -05:00
|
|
|
lock *sync.RWMutex
|
|
|
|
rsAPI api.SyncRoomserverAPI
|
2017-05-17 09:38:24 -05:00
|
|
|
// A map of RoomID => Set<UserID> : Must only be accessed by the OnNewEvent goroutine
|
2022-04-13 06:35:30 -05:00
|
|
|
roomIDToJoinedUsers map[string]*userIDSet
|
2020-09-10 08:39:18 -05:00
|
|
|
// A map of RoomID => Set<UserID> : Must only be accessed by the OnNewEvent goroutine
|
|
|
|
roomIDToPeekingDevices map[string]peekingDeviceSet
|
2019-07-12 09:59:53 -05:00
|
|
|
// The latest sync position
|
2020-05-13 06:14:50 -05:00
|
|
|
currPos types.StreamingToken
|
2020-05-28 04:05:04 -05:00
|
|
|
// A map of user_id => device_id => UserStream which can be used to wake a given user's /sync request.
|
|
|
|
userDeviceStreams map[string]map[string]*UserDeviceStream
|
2017-10-26 05:34:54 -05:00
|
|
|
// The last time we cleaned out stale entries from the userStreams map
|
|
|
|
lastCleanUpTime time.Time
|
2022-04-06 09:04:41 -05:00
|
|
|
// This map is reused to prevent allocations and GC pressure in SharedUsers.
|
2022-04-07 11:08:15 -05:00
|
|
|
_sharedUserMap map[string]struct{}
|
2022-10-07 07:42:35 -05:00
|
|
|
_wakeupUserMap map[string]struct{}
|
2017-05-10 04:42:00 -05:00
|
|
|
}
|
|
|
|
|
2019-07-12 09:59:53 -05:00
|
|
|
// NewNotifier creates a new notifier set to the given sync position.
|
2017-05-17 09:38:24 -05:00
|
|
|
// In order for this to be of any use, the Notifier needs to be told all rooms and
|
2017-05-17 11:29:26 -05:00
|
|
|
// the joined users within each of them by calling Notifier.Load(*storage.SyncServerDatabase).
|
2023-06-12 06:19:25 -05:00
|
|
|
func NewNotifier(rsAPI api.SyncRoomserverAPI) *Notifier {
|
2017-05-10 04:42:00 -05:00
|
|
|
return &Notifier{
|
2023-06-12 06:19:25 -05:00
|
|
|
rsAPI: rsAPI,
|
2022-04-13 06:35:30 -05:00
|
|
|
roomIDToJoinedUsers: make(map[string]*userIDSet),
|
2020-09-10 08:39:18 -05:00
|
|
|
roomIDToPeekingDevices: make(map[string]peekingDeviceSet),
|
|
|
|
userDeviceStreams: make(map[string]map[string]*UserDeviceStream),
|
2022-04-07 11:08:15 -05:00
|
|
|
lock: &sync.RWMutex{},
|
2020-09-10 08:39:18 -05:00
|
|
|
lastCleanUpTime: time.Now(),
|
2022-04-07 11:08:15 -05:00
|
|
|
_sharedUserMap: map[string]struct{}{},
|
2022-10-07 07:42:35 -05:00
|
|
|
_wakeupUserMap: map[string]struct{}{},
|
2017-05-10 04:42:00 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-06 06:11:19 -05:00
|
|
|
// SetCurrentPosition sets the current streaming positions.
|
|
|
|
// This must be called directly after NewNotifier and initialising the streams.
|
|
|
|
func (n *Notifier) SetCurrentPosition(currPos types.StreamingToken) {
|
2022-04-07 11:08:15 -05:00
|
|
|
n.lock.Lock()
|
|
|
|
defer n.lock.Unlock()
|
2022-04-06 09:04:41 -05:00
|
|
|
|
2022-04-06 06:11:19 -05:00
|
|
|
n.currPos = currPos
|
|
|
|
}
|
|
|
|
|
2017-05-10 04:42:00 -05:00
|
|
|
// OnNewEvent is called when a new event is received from the room server. Must only be
|
|
|
|
// called from a single goroutine, to avoid races between updates which could set the
|
2019-07-12 09:59:53 -05:00
|
|
|
// current sync position incorrectly.
|
2023-05-03 04:21:27 -05:00
|
|
|
// Chooses which user sync streams to update by a provided gomatrixserverlib.PDU
|
2019-07-12 09:59:53 -05:00
|
|
|
// (based on the users in the event's room),
|
|
|
|
// a roomID directly, or a list of user IDs, prioritised by parameter ordering.
|
|
|
|
// posUpdate contains the latest position(s) for one or more types of events.
|
|
|
|
// If a position in posUpdate is 0, it means no updates are available of that type.
|
|
|
|
// Typically a consumer supplies a posUpdate with the latest sync position for the
|
|
|
|
// event type it handles, leaving other fields as 0.
|
|
|
|
func (n *Notifier) OnNewEvent(
|
2023-04-27 06:54:20 -05:00
|
|
|
ev *rstypes.HeaderedEvent, roomID string, userIDs []string,
|
2020-05-13 06:14:50 -05:00
|
|
|
posUpdate types.StreamingToken,
|
2019-07-12 09:59:53 -05:00
|
|
|
) {
|
2017-05-17 09:38:24 -05:00
|
|
|
// update the current position then notify relevant /sync streams.
|
|
|
|
// This needs to be done PRIOR to waking up users as they will read this value.
|
2022-04-07 11:08:15 -05:00
|
|
|
n.lock.Lock()
|
|
|
|
defer n.lock.Unlock()
|
2020-12-18 05:11:21 -06:00
|
|
|
n.currPos.ApplyUpdates(posUpdate)
|
2022-04-07 11:08:15 -05:00
|
|
|
n._removeEmptyUserStreams()
|
2017-10-26 05:34:54 -05:00
|
|
|
|
2017-08-02 10:21:35 -05:00
|
|
|
if ev != nil {
|
|
|
|
// Map this event's room_id to a list of joined users, and wake them up.
|
2022-04-07 11:08:15 -05:00
|
|
|
usersToNotify := n._joinedUsers(ev.RoomID())
|
2020-09-10 08:39:18 -05:00
|
|
|
// Map this event's room_id to a list of peeking devices, and wake them up.
|
2022-04-07 11:08:15 -05:00
|
|
|
peekingDevicesToNotify := n._peekingDevices(ev.RoomID())
|
2017-08-02 10:21:35 -05:00
|
|
|
// If this is an invite, also add in the invitee to this list.
|
|
|
|
if ev.Type() == "m.room.member" && ev.StateKey() != nil {
|
2023-06-12 06:19:25 -05:00
|
|
|
targetUserID, err := n.rsAPI.QueryUserIDForSender(context.Background(), ev.RoomID(), spec.SenderID(*ev.StateKey()))
|
2017-08-02 10:21:35 -05:00
|
|
|
if err != nil {
|
|
|
|
log.WithError(err).WithField("event_id", ev.EventID()).Errorf(
|
2023-06-12 06:19:25 -05:00
|
|
|
"Notifier.OnNewEvent: Failed to find the userID for this event",
|
2017-08-02 10:21:35 -05:00
|
|
|
)
|
|
|
|
} else {
|
2023-06-12 06:19:25 -05:00
|
|
|
membership, err := ev.Membership()
|
|
|
|
if err != nil {
|
|
|
|
log.WithError(err).WithField("event_id", ev.EventID()).Errorf(
|
|
|
|
"Notifier.OnNewEvent: Failed to unmarshal member event",
|
|
|
|
)
|
|
|
|
} else {
|
|
|
|
// Keep the joined user map up-to-date
|
|
|
|
switch membership {
|
|
|
|
case spec.Invite:
|
|
|
|
usersToNotify = append(usersToNotify, targetUserID.String())
|
|
|
|
case spec.Join:
|
|
|
|
// Manually append the new user's ID so they get notified
|
|
|
|
// along all members in the room
|
|
|
|
usersToNotify = append(usersToNotify, targetUserID.String())
|
|
|
|
n._addJoinedUser(ev.RoomID(), targetUserID.String())
|
|
|
|
case spec.Leave:
|
|
|
|
fallthrough
|
|
|
|
case spec.Ban:
|
|
|
|
n._removeJoinedUser(ev.RoomID(), targetUserID.String())
|
|
|
|
}
|
2017-08-02 10:21:35 -05:00
|
|
|
}
|
2017-05-17 09:38:24 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-07 11:08:15 -05:00
|
|
|
n._wakeupUsers(usersToNotify, peekingDevicesToNotify, n.currPos)
|
2019-07-12 09:59:53 -05:00
|
|
|
} else if roomID != "" {
|
2022-04-07 11:08:15 -05:00
|
|
|
n._wakeupUsers(n._joinedUsers(roomID), n._peekingDevices(roomID), n.currPos)
|
2019-07-12 09:59:53 -05:00
|
|
|
} else if len(userIDs) > 0 {
|
2022-04-07 11:08:15 -05:00
|
|
|
n._wakeupUsers(userIDs, nil, n.currPos)
|
2019-07-12 09:59:53 -05:00
|
|
|
} else {
|
|
|
|
log.WithFields(log.Fields{
|
|
|
|
"posUpdate": posUpdate.String,
|
|
|
|
}).Warn("Notifier.OnNewEvent called but caller supplied no user to wake up")
|
2017-05-17 09:38:24 -05:00
|
|
|
}
|
2017-05-10 04:42:00 -05:00
|
|
|
}
|
|
|
|
|
2021-01-08 10:59:06 -06:00
|
|
|
func (n *Notifier) OnNewAccountData(
|
|
|
|
userID string, posUpdate types.StreamingToken,
|
|
|
|
) {
|
2022-04-07 11:08:15 -05:00
|
|
|
n.lock.Lock()
|
|
|
|
defer n.lock.Unlock()
|
2021-01-08 10:59:06 -06:00
|
|
|
|
|
|
|
n.currPos.ApplyUpdates(posUpdate)
|
2022-04-07 11:08:15 -05:00
|
|
|
n._wakeupUsers([]string{userID}, nil, posUpdate)
|
2021-01-08 10:59:06 -06:00
|
|
|
}
|
|
|
|
|
2020-09-10 08:39:18 -05:00
|
|
|
func (n *Notifier) OnNewPeek(
|
|
|
|
roomID, userID, deviceID string,
|
2021-01-08 10:59:06 -06:00
|
|
|
posUpdate types.StreamingToken,
|
2020-09-10 08:39:18 -05:00
|
|
|
) {
|
2022-04-07 11:08:15 -05:00
|
|
|
n.lock.Lock()
|
|
|
|
defer n.lock.Unlock()
|
2020-09-10 08:39:18 -05:00
|
|
|
|
2021-01-08 10:59:06 -06:00
|
|
|
n.currPos.ApplyUpdates(posUpdate)
|
2022-04-07 11:08:15 -05:00
|
|
|
n._addPeekingDevice(roomID, userID, deviceID)
|
2020-09-10 08:39:18 -05:00
|
|
|
|
|
|
|
// we don't wake up devices here given the roomserver consumer will do this shortly afterwards
|
|
|
|
// by calling OnNewEvent.
|
|
|
|
}
|
|
|
|
|
2020-12-03 05:11:46 -06:00
|
|
|
func (n *Notifier) OnRetirePeek(
|
|
|
|
roomID, userID, deviceID string,
|
2021-01-08 10:59:06 -06:00
|
|
|
posUpdate types.StreamingToken,
|
2020-12-03 05:11:46 -06:00
|
|
|
) {
|
2022-04-07 11:08:15 -05:00
|
|
|
n.lock.Lock()
|
|
|
|
defer n.lock.Unlock()
|
2020-12-03 05:11:46 -06:00
|
|
|
|
2021-01-08 10:59:06 -06:00
|
|
|
n.currPos.ApplyUpdates(posUpdate)
|
2022-04-07 11:08:15 -05:00
|
|
|
n._removePeekingDevice(roomID, userID, deviceID)
|
2020-12-03 05:11:46 -06:00
|
|
|
|
|
|
|
// we don't wake up devices here given the roomserver consumer will do this shortly afterwards
|
|
|
|
// by calling OnRetireEvent.
|
|
|
|
}
|
|
|
|
|
Send-to-device support (#1072)
* Groundwork for send-to-device messaging
* Update sample config
* Add unstable routing for now
* Send to device consumer in sync API
* Start the send-to-device consumer
* fix indentation in dendrite-config.yaml
* Create send-to-device database tables, other tweaks
* Add some logic for send-to-device messages, add them into sync stream
* Handle incoming send-to-device messages, count them with EDU stream pos
* Undo changes to test
* pq.Array
* Fix sync
* Logging
* Fix a couple of transaction things, fix client API
* Add send-to-device test, hopefully fix bugs
* Comments
* Refactor a bit
* Fix schema
* Fix queries
* Debug logging
* Fix storing and retrieving of send-to-device messages
* Try to avoid database locks
* Update sync position
* Use latest sync position
* Jiggle about sync a bit
* Fix tests
* Break out the retrieval from the update/delete behaviour
* Comments
* nolint on getResponseWithPDUsForCompleteSync
* Try to line up sync tokens again
* Implement wildcard
* Add all send-to-device tests to whitelist, what could possibly go wrong?
* Only care about wildcard when targeted locally
* Deduplicate transactions
* Handle tokens properly, return immediately if waiting send-to-device messages
* Fix sync
* Update sytest-whitelist
* Fix copyright notice (need to do more of this)
* Comments, copyrights
* Return errors from Do, fix dendritejs
* Review comments
* Comments
* Constructor for TransactionWriter
* defletions
* Update gomatrixserverlib, sytest-blacklist
2020-06-01 11:50:19 -05:00
|
|
|
func (n *Notifier) OnNewSendToDevice(
|
|
|
|
userID string, deviceIDs []string,
|
|
|
|
posUpdate types.StreamingToken,
|
|
|
|
) {
|
2022-04-07 11:08:15 -05:00
|
|
|
n.lock.Lock()
|
|
|
|
defer n.lock.Unlock()
|
Send-to-device support (#1072)
* Groundwork for send-to-device messaging
* Update sample config
* Add unstable routing for now
* Send to device consumer in sync API
* Start the send-to-device consumer
* fix indentation in dendrite-config.yaml
* Create send-to-device database tables, other tweaks
* Add some logic for send-to-device messages, add them into sync stream
* Handle incoming send-to-device messages, count them with EDU stream pos
* Undo changes to test
* pq.Array
* Fix sync
* Logging
* Fix a couple of transaction things, fix client API
* Add send-to-device test, hopefully fix bugs
* Comments
* Refactor a bit
* Fix schema
* Fix queries
* Debug logging
* Fix storing and retrieving of send-to-device messages
* Try to avoid database locks
* Update sync position
* Use latest sync position
* Jiggle about sync a bit
* Fix tests
* Break out the retrieval from the update/delete behaviour
* Comments
* nolint on getResponseWithPDUsForCompleteSync
* Try to line up sync tokens again
* Implement wildcard
* Add all send-to-device tests to whitelist, what could possibly go wrong?
* Only care about wildcard when targeted locally
* Deduplicate transactions
* Handle tokens properly, return immediately if waiting send-to-device messages
* Fix sync
* Update sytest-whitelist
* Fix copyright notice (need to do more of this)
* Comments, copyrights
* Return errors from Do, fix dendritejs
* Review comments
* Comments
* Constructor for TransactionWriter
* defletions
* Update gomatrixserverlib, sytest-blacklist
2020-06-01 11:50:19 -05:00
|
|
|
|
2020-12-18 05:11:21 -06:00
|
|
|
n.currPos.ApplyUpdates(posUpdate)
|
2022-04-07 11:08:15 -05:00
|
|
|
n._wakeupUserDevice(userID, deviceIDs, n.currPos)
|
2020-12-18 05:11:21 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// OnNewReceipt updates the current position
|
|
|
|
func (n *Notifier) OnNewTyping(
|
|
|
|
roomID string,
|
|
|
|
posUpdate types.StreamingToken,
|
|
|
|
) {
|
2022-04-07 11:08:15 -05:00
|
|
|
n.lock.Lock()
|
|
|
|
defer n.lock.Unlock()
|
2020-12-18 05:11:21 -06:00
|
|
|
|
|
|
|
n.currPos.ApplyUpdates(posUpdate)
|
2022-04-07 11:08:15 -05:00
|
|
|
n._wakeupUsers(n._joinedUsers(roomID), nil, n.currPos)
|
Send-to-device support (#1072)
* Groundwork for send-to-device messaging
* Update sample config
* Add unstable routing for now
* Send to device consumer in sync API
* Start the send-to-device consumer
* fix indentation in dendrite-config.yaml
* Create send-to-device database tables, other tweaks
* Add some logic for send-to-device messages, add them into sync stream
* Handle incoming send-to-device messages, count them with EDU stream pos
* Undo changes to test
* pq.Array
* Fix sync
* Logging
* Fix a couple of transaction things, fix client API
* Add send-to-device test, hopefully fix bugs
* Comments
* Refactor a bit
* Fix schema
* Fix queries
* Debug logging
* Fix storing and retrieving of send-to-device messages
* Try to avoid database locks
* Update sync position
* Use latest sync position
* Jiggle about sync a bit
* Fix tests
* Break out the retrieval from the update/delete behaviour
* Comments
* nolint on getResponseWithPDUsForCompleteSync
* Try to line up sync tokens again
* Implement wildcard
* Add all send-to-device tests to whitelist, what could possibly go wrong?
* Only care about wildcard when targeted locally
* Deduplicate transactions
* Handle tokens properly, return immediately if waiting send-to-device messages
* Fix sync
* Update sytest-whitelist
* Fix copyright notice (need to do more of this)
* Comments, copyrights
* Return errors from Do, fix dendritejs
* Review comments
* Comments
* Constructor for TransactionWriter
* defletions
* Update gomatrixserverlib, sytest-blacklist
2020-06-01 11:50:19 -05:00
|
|
|
}
|
|
|
|
|
2020-11-09 12:46:11 -06:00
|
|
|
// OnNewReceipt updates the current position
|
|
|
|
func (n *Notifier) OnNewReceipt(
|
2020-12-18 05:11:21 -06:00
|
|
|
roomID string,
|
2020-11-09 12:46:11 -06:00
|
|
|
posUpdate types.StreamingToken,
|
|
|
|
) {
|
2022-04-07 11:08:15 -05:00
|
|
|
n.lock.Lock()
|
|
|
|
defer n.lock.Unlock()
|
2020-12-18 05:11:21 -06:00
|
|
|
|
|
|
|
n.currPos.ApplyUpdates(posUpdate)
|
2022-04-07 11:08:15 -05:00
|
|
|
n._wakeupUsers(n._joinedUsers(roomID), nil, n.currPos)
|
2020-11-09 12:46:11 -06:00
|
|
|
}
|
|
|
|
|
2020-07-30 05:15:46 -05:00
|
|
|
func (n *Notifier) OnNewKeyChange(
|
|
|
|
posUpdate types.StreamingToken, wakeUserID, keyChangeUserID string,
|
|
|
|
) {
|
2022-04-07 11:08:15 -05:00
|
|
|
n.lock.Lock()
|
|
|
|
defer n.lock.Unlock()
|
2020-12-18 05:11:21 -06:00
|
|
|
|
|
|
|
n.currPos.ApplyUpdates(posUpdate)
|
2022-04-07 11:08:15 -05:00
|
|
|
n._wakeupUsers([]string{wakeUserID}, nil, n.currPos)
|
2020-12-18 05:11:21 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
func (n *Notifier) OnNewInvite(
|
|
|
|
posUpdate types.StreamingToken, wakeUserID string,
|
|
|
|
) {
|
2022-04-07 11:08:15 -05:00
|
|
|
n.lock.Lock()
|
|
|
|
defer n.lock.Unlock()
|
2020-12-18 05:11:21 -06:00
|
|
|
|
|
|
|
n.currPos.ApplyUpdates(posUpdate)
|
2022-04-07 11:08:15 -05:00
|
|
|
n._wakeupUsers([]string{wakeUserID}, nil, n.currPos)
|
2020-07-30 05:15:46 -05:00
|
|
|
}
|
|
|
|
|
2022-03-03 05:40:53 -06:00
|
|
|
func (n *Notifier) OnNewNotificationData(
|
|
|
|
userID string,
|
|
|
|
posUpdate types.StreamingToken,
|
|
|
|
) {
|
2022-04-07 11:08:15 -05:00
|
|
|
n.lock.Lock()
|
|
|
|
defer n.lock.Unlock()
|
2022-03-03 05:40:53 -06:00
|
|
|
|
|
|
|
n.currPos.ApplyUpdates(posUpdate)
|
2022-04-07 11:08:15 -05:00
|
|
|
n._wakeupUsers([]string{userID}, nil, n.currPos)
|
2022-03-03 05:40:53 -06:00
|
|
|
}
|
|
|
|
|
2022-04-06 06:11:19 -05:00
|
|
|
func (n *Notifier) OnNewPresence(
|
|
|
|
posUpdate types.StreamingToken, userID string,
|
|
|
|
) {
|
2022-04-07 11:08:15 -05:00
|
|
|
n.lock.Lock()
|
|
|
|
defer n.lock.Unlock()
|
2022-04-06 06:11:19 -05:00
|
|
|
|
|
|
|
n.currPos.ApplyUpdates(posUpdate)
|
2022-04-07 11:08:15 -05:00
|
|
|
sharedUsers := n._sharedUsers(userID)
|
2022-04-06 06:11:19 -05:00
|
|
|
sharedUsers = append(sharedUsers, userID)
|
|
|
|
|
2022-04-07 11:08:15 -05:00
|
|
|
n._wakeupUsers(sharedUsers, nil, n.currPos)
|
2022-04-06 06:11:19 -05:00
|
|
|
}
|
|
|
|
|
2022-04-06 09:04:41 -05:00
|
|
|
func (n *Notifier) SharedUsers(userID string) []string {
|
2022-04-07 11:08:15 -05:00
|
|
|
n.lock.RLock()
|
|
|
|
defer n.lock.RUnlock()
|
|
|
|
return n._sharedUsers(userID)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (n *Notifier) _sharedUsers(userID string) []string {
|
|
|
|
n._sharedUserMap[userID] = struct{}{}
|
2022-04-06 06:11:19 -05:00
|
|
|
for roomID, users := range n.roomIDToJoinedUsers {
|
2022-04-13 06:35:30 -05:00
|
|
|
if ok := users.isIn(userID); !ok {
|
2022-04-06 09:04:41 -05:00
|
|
|
continue
|
2022-04-06 06:11:19 -05:00
|
|
|
}
|
2022-04-07 11:08:15 -05:00
|
|
|
for _, userID := range n._joinedUsers(roomID) {
|
|
|
|
n._sharedUserMap[userID] = struct{}{}
|
2022-04-06 09:04:41 -05:00
|
|
|
}
|
|
|
|
}
|
2022-04-07 11:08:15 -05:00
|
|
|
sharedUsers := make([]string, 0, len(n._sharedUserMap)+1)
|
|
|
|
for userID := range n._sharedUserMap {
|
2022-04-06 09:04:41 -05:00
|
|
|
sharedUsers = append(sharedUsers, userID)
|
2022-04-07 11:08:15 -05:00
|
|
|
delete(n._sharedUserMap, userID)
|
2022-04-06 06:11:19 -05:00
|
|
|
}
|
|
|
|
return sharedUsers
|
|
|
|
}
|
|
|
|
|
2022-04-06 07:31:44 -05:00
|
|
|
func (n *Notifier) IsSharedUser(userA, userB string) bool {
|
2022-04-07 11:08:15 -05:00
|
|
|
n.lock.RLock()
|
|
|
|
defer n.lock.RUnlock()
|
2022-04-06 07:31:44 -05:00
|
|
|
var okA, okB bool
|
|
|
|
for _, users := range n.roomIDToJoinedUsers {
|
2022-04-13 06:35:30 -05:00
|
|
|
okA = users.isIn(userA)
|
|
|
|
if !okA {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
okB = users.isIn(userB)
|
2022-04-06 07:31:44 -05:00
|
|
|
if okA && okB {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2017-10-26 05:34:54 -05:00
|
|
|
// GetListener returns a UserStreamListener that can be used to wait for
|
|
|
|
// updates for a user. Must be closed.
|
|
|
|
// notify for anything before sincePos
|
2021-01-08 10:59:06 -06:00
|
|
|
func (n *Notifier) GetListener(req types.SyncRequest) UserDeviceStreamListener {
|
2017-05-17 09:38:24 -05:00
|
|
|
// Do what synapse does: https://github.com/matrix-org/synapse/blob/v0.20.0/synapse/notifier.py#L298
|
|
|
|
// - Bucket request into a lookup map keyed off a list of joined room IDs and separately a user ID
|
|
|
|
// - Incoming events wake requests for a matching room ID
|
|
|
|
// - Incoming events wake requests for a matching user ID (needed for invites)
|
|
|
|
|
|
|
|
// TODO: v1 /events 'peeking' has an 'explicit room ID' which is also tracked,
|
|
|
|
// but given we don't do /events, let's pretend it doesn't exist.
|
|
|
|
|
2022-04-07 11:08:15 -05:00
|
|
|
n.lock.Lock()
|
|
|
|
defer n.lock.Unlock()
|
2017-05-17 09:38:24 -05:00
|
|
|
|
2022-04-07 11:08:15 -05:00
|
|
|
n._removeEmptyUserStreams()
|
2017-05-17 09:38:24 -05:00
|
|
|
|
2022-04-07 11:08:15 -05:00
|
|
|
return n._fetchUserDeviceStream(req.Device.UserID, req.Device.ID, true).GetListener(req.Context)
|
2017-05-17 09:38:24 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// Load the membership states required to notify users correctly.
|
2020-01-03 08:07:05 -06:00
|
|
|
func (n *Notifier) Load(ctx context.Context, db storage.Database) error {
|
2022-04-07 11:08:15 -05:00
|
|
|
n.lock.Lock()
|
|
|
|
defer n.lock.Unlock()
|
2022-09-30 06:48:10 -05:00
|
|
|
|
|
|
|
snapshot, err := db.NewDatabaseSnapshot(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-09-30 10:07:18 -05:00
|
|
|
var succeeded bool
|
|
|
|
defer sqlutil.EndTransactionWithCheck(snapshot, &succeeded, &err)
|
2022-09-30 06:48:10 -05:00
|
|
|
|
|
|
|
roomToUsers, err := snapshot.AllJoinedUsersInRooms(ctx)
|
2017-05-17 09:38:24 -05:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
n.setUsersJoinedToRooms(roomToUsers)
|
2020-09-10 08:39:18 -05:00
|
|
|
|
2022-09-30 06:48:10 -05:00
|
|
|
roomToPeekingDevices, err := snapshot.AllPeekingDevicesInRooms(ctx)
|
2020-09-10 08:39:18 -05:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
n.setPeekingDevices(roomToPeekingDevices)
|
|
|
|
|
2022-09-30 10:07:18 -05:00
|
|
|
succeeded = true
|
2017-05-17 09:38:24 -05:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-04-28 11:53:28 -05:00
|
|
|
// LoadRooms loads the membership states required to notify users correctly.
|
|
|
|
func (n *Notifier) LoadRooms(ctx context.Context, db storage.Database, roomIDs []string) error {
|
|
|
|
n.lock.Lock()
|
|
|
|
defer n.lock.Unlock()
|
|
|
|
|
2022-09-30 06:48:10 -05:00
|
|
|
snapshot, err := db.NewDatabaseSnapshot(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-09-30 10:07:18 -05:00
|
|
|
var succeeded bool
|
|
|
|
defer sqlutil.EndTransactionWithCheck(snapshot, &succeeded, &err)
|
2022-09-30 06:48:10 -05:00
|
|
|
|
|
|
|
roomToUsers, err := snapshot.AllJoinedUsersInRoom(ctx, roomIDs)
|
2022-04-28 11:53:28 -05:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
n.setUsersJoinedToRooms(roomToUsers)
|
|
|
|
|
2022-09-30 10:07:18 -05:00
|
|
|
succeeded = true
|
2022-04-28 11:53:28 -05:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-07-12 09:59:53 -05:00
|
|
|
// CurrentPosition returns the current sync position
|
2020-05-13 06:14:50 -05:00
|
|
|
func (n *Notifier) CurrentPosition() types.StreamingToken {
|
2022-04-07 11:08:15 -05:00
|
|
|
n.lock.RLock()
|
|
|
|
defer n.lock.RUnlock()
|
2019-07-12 09:59:53 -05:00
|
|
|
|
2017-10-16 07:34:08 -05:00
|
|
|
return n.currPos
|
|
|
|
}
|
|
|
|
|
2017-05-17 09:38:24 -05:00
|
|
|
// setUsersJoinedToRooms marks the given users as 'joined' to the given rooms, such that new events from
|
|
|
|
// these rooms will wake the given users /sync requests. This should be called prior to ANY calls to
|
|
|
|
// OnNewEvent (eg on startup) to prevent racing.
|
|
|
|
func (n *Notifier) setUsersJoinedToRooms(roomIDToUserIDs map[string][]string) {
|
|
|
|
// This is just the bulk form of addJoinedUser
|
|
|
|
for roomID, userIDs := range roomIDToUserIDs {
|
|
|
|
if _, ok := n.roomIDToJoinedUsers[roomID]; !ok {
|
2022-04-13 06:35:30 -05:00
|
|
|
n.roomIDToJoinedUsers[roomID] = newUserIDSet(len(userIDs))
|
2017-05-17 09:38:24 -05:00
|
|
|
}
|
|
|
|
for _, userID := range userIDs {
|
|
|
|
n.roomIDToJoinedUsers[roomID].add(userID)
|
|
|
|
}
|
2022-04-13 06:35:30 -05:00
|
|
|
n.roomIDToJoinedUsers[roomID].precompute()
|
2017-05-17 09:38:24 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-10 08:39:18 -05:00
|
|
|
// setPeekingDevices marks the given devices as peeking in the given rooms, such that new events from
|
|
|
|
// these rooms will wake the given devices' /sync requests. This should be called prior to ANY calls to
|
|
|
|
// OnNewEvent (eg on startup) to prevent racing.
|
|
|
|
func (n *Notifier) setPeekingDevices(roomIDToPeekingDevices map[string][]types.PeekingDevice) {
|
|
|
|
// This is just the bulk form of addPeekingDevice
|
|
|
|
for roomID, peekingDevices := range roomIDToPeekingDevices {
|
|
|
|
if _, ok := n.roomIDToPeekingDevices[roomID]; !ok {
|
2022-04-06 09:04:41 -05:00
|
|
|
n.roomIDToPeekingDevices[roomID] = make(peekingDeviceSet, len(peekingDevices))
|
2020-09-10 08:39:18 -05:00
|
|
|
}
|
|
|
|
for _, peekingDevice := range peekingDevices {
|
|
|
|
n.roomIDToPeekingDevices[roomID].add(peekingDevice)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-07 11:08:15 -05:00
|
|
|
// _wakeupUsers will wake up the sync strems for all of the devices for all of the
|
2020-09-10 08:39:18 -05:00
|
|
|
// specified user IDs, and also the specified peekingDevices
|
2022-04-07 11:08:15 -05:00
|
|
|
func (n *Notifier) _wakeupUsers(userIDs []string, peekingDevices []types.PeekingDevice, newPos types.StreamingToken) {
|
2019-07-12 09:59:53 -05:00
|
|
|
for _, userID := range userIDs {
|
2022-10-07 07:42:35 -05:00
|
|
|
n._wakeupUserMap[userID] = struct{}{}
|
|
|
|
}
|
|
|
|
for userID := range n._wakeupUserMap {
|
2022-04-07 11:08:15 -05:00
|
|
|
for _, stream := range n._fetchUserStreams(userID) {
|
2020-05-28 04:05:04 -05:00
|
|
|
if stream == nil {
|
|
|
|
continue
|
|
|
|
}
|
2019-07-12 09:59:53 -05:00
|
|
|
stream.Broadcast(newPos) // wake up all goroutines Wait()ing on this stream
|
|
|
|
}
|
2022-10-07 07:42:35 -05:00
|
|
|
delete(n._wakeupUserMap, userID)
|
2017-05-17 09:38:24 -05:00
|
|
|
}
|
2020-09-10 08:39:18 -05:00
|
|
|
|
|
|
|
for _, peekingDevice := range peekingDevices {
|
|
|
|
// TODO: don't bother waking up for devices whose users we already woke up
|
2022-04-07 11:08:15 -05:00
|
|
|
if stream := n._fetchUserDeviceStream(peekingDevice.UserID, peekingDevice.DeviceID, false); stream != nil {
|
2020-09-10 08:39:18 -05:00
|
|
|
stream.Broadcast(newPos) // wake up all goroutines Wait()ing on this stream
|
|
|
|
}
|
|
|
|
}
|
2017-05-17 09:38:24 -05:00
|
|
|
}
|
|
|
|
|
2022-04-07 11:08:15 -05:00
|
|
|
// _wakeupUserDevice will wake up the sync stream for a specific user device. Other
|
2020-05-28 04:05:04 -05:00
|
|
|
// device streams will be left alone.
|
|
|
|
// nolint:unused
|
2022-04-07 11:08:15 -05:00
|
|
|
func (n *Notifier) _wakeupUserDevice(userID string, deviceIDs []string, newPos types.StreamingToken) {
|
Send-to-device support (#1072)
* Groundwork for send-to-device messaging
* Update sample config
* Add unstable routing for now
* Send to device consumer in sync API
* Start the send-to-device consumer
* fix indentation in dendrite-config.yaml
* Create send-to-device database tables, other tweaks
* Add some logic for send-to-device messages, add them into sync stream
* Handle incoming send-to-device messages, count them with EDU stream pos
* Undo changes to test
* pq.Array
* Fix sync
* Logging
* Fix a couple of transaction things, fix client API
* Add send-to-device test, hopefully fix bugs
* Comments
* Refactor a bit
* Fix schema
* Fix queries
* Debug logging
* Fix storing and retrieving of send-to-device messages
* Try to avoid database locks
* Update sync position
* Use latest sync position
* Jiggle about sync a bit
* Fix tests
* Break out the retrieval from the update/delete behaviour
* Comments
* nolint on getResponseWithPDUsForCompleteSync
* Try to line up sync tokens again
* Implement wildcard
* Add all send-to-device tests to whitelist, what could possibly go wrong?
* Only care about wildcard when targeted locally
* Deduplicate transactions
* Handle tokens properly, return immediately if waiting send-to-device messages
* Fix sync
* Update sytest-whitelist
* Fix copyright notice (need to do more of this)
* Comments, copyrights
* Return errors from Do, fix dendritejs
* Review comments
* Comments
* Constructor for TransactionWriter
* defletions
* Update gomatrixserverlib, sytest-blacklist
2020-06-01 11:50:19 -05:00
|
|
|
for _, deviceID := range deviceIDs {
|
2022-04-07 11:08:15 -05:00
|
|
|
if stream := n._fetchUserDeviceStream(userID, deviceID, false); stream != nil {
|
2020-05-28 04:05:04 -05:00
|
|
|
stream.Broadcast(newPos) // wake up all goroutines Wait()ing on this stream
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-07 11:08:15 -05:00
|
|
|
// _fetchUserDeviceStream retrieves a stream unique to the given device. If makeIfNotExists is true,
|
2020-05-28 04:05:04 -05:00
|
|
|
// a stream will be made for this device if one doesn't exist and it will be returned. This
|
2017-05-17 09:38:24 -05:00
|
|
|
// function does not wait for data to be available on the stream.
|
2022-04-07 11:08:15 -05:00
|
|
|
func (n *Notifier) _fetchUserDeviceStream(userID, deviceID string, makeIfNotExists bool) *UserDeviceStream {
|
2020-05-28 04:05:04 -05:00
|
|
|
_, ok := n.userDeviceStreams[userID]
|
|
|
|
if !ok {
|
|
|
|
if !makeIfNotExists {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
n.userDeviceStreams[userID] = map[string]*UserDeviceStream{}
|
|
|
|
}
|
|
|
|
stream, ok := n.userDeviceStreams[userID][deviceID]
|
|
|
|
if !ok {
|
|
|
|
if !makeIfNotExists {
|
|
|
|
return nil
|
|
|
|
}
|
2017-05-17 09:38:24 -05:00
|
|
|
// TODO: Unbounded growth of streams (1 per user)
|
2020-05-28 04:05:04 -05:00
|
|
|
if stream = NewUserDeviceStream(userID, deviceID, n.currPos); stream != nil {
|
|
|
|
n.userDeviceStreams[userID][deviceID] = stream
|
|
|
|
}
|
2017-05-17 09:38:24 -05:00
|
|
|
}
|
|
|
|
return stream
|
|
|
|
}
|
|
|
|
|
2022-04-07 11:08:15 -05:00
|
|
|
// _fetchUserStreams retrieves all streams for the given user. If makeIfNotExists is true,
|
2020-05-28 04:05:04 -05:00
|
|
|
// a stream will be made for this user if one doesn't exist and it will be returned. This
|
|
|
|
// function does not wait for data to be available on the stream.
|
2022-04-07 11:08:15 -05:00
|
|
|
func (n *Notifier) _fetchUserStreams(userID string) []*UserDeviceStream {
|
2020-05-28 04:05:04 -05:00
|
|
|
user, ok := n.userDeviceStreams[userID]
|
|
|
|
if !ok {
|
|
|
|
return []*UserDeviceStream{}
|
|
|
|
}
|
2022-04-06 09:04:41 -05:00
|
|
|
streams := make([]*UserDeviceStream, 0, len(user))
|
2020-05-28 04:05:04 -05:00
|
|
|
for _, stream := range user {
|
|
|
|
streams = append(streams, stream)
|
|
|
|
}
|
|
|
|
return streams
|
|
|
|
}
|
|
|
|
|
2022-04-07 11:08:15 -05:00
|
|
|
func (n *Notifier) _addJoinedUser(roomID, userID string) {
|
2017-05-17 09:38:24 -05:00
|
|
|
if _, ok := n.roomIDToJoinedUsers[roomID]; !ok {
|
2022-04-13 06:35:30 -05:00
|
|
|
n.roomIDToJoinedUsers[roomID] = newUserIDSet(8)
|
2017-05-17 09:38:24 -05:00
|
|
|
}
|
|
|
|
n.roomIDToJoinedUsers[roomID].add(userID)
|
2022-04-13 06:35:30 -05:00
|
|
|
n.roomIDToJoinedUsers[roomID].precompute()
|
2017-05-17 09:38:24 -05:00
|
|
|
}
|
|
|
|
|
2022-04-07 11:08:15 -05:00
|
|
|
func (n *Notifier) _removeJoinedUser(roomID, userID string) {
|
2017-05-17 09:38:24 -05:00
|
|
|
if _, ok := n.roomIDToJoinedUsers[roomID]; !ok {
|
2022-04-13 06:35:30 -05:00
|
|
|
n.roomIDToJoinedUsers[roomID] = newUserIDSet(8)
|
2017-05-17 09:38:24 -05:00
|
|
|
}
|
|
|
|
n.roomIDToJoinedUsers[roomID].remove(userID)
|
2022-04-13 06:35:30 -05:00
|
|
|
n.roomIDToJoinedUsers[roomID].precompute()
|
2017-05-17 09:38:24 -05:00
|
|
|
}
|
|
|
|
|
2022-04-06 06:11:19 -05:00
|
|
|
func (n *Notifier) JoinedUsers(roomID string) (userIDs []string) {
|
2022-04-07 11:08:15 -05:00
|
|
|
n.lock.RLock()
|
|
|
|
defer n.lock.RUnlock()
|
|
|
|
return n._joinedUsers(roomID)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (n *Notifier) _joinedUsers(roomID string) (userIDs []string) {
|
2017-05-17 09:38:24 -05:00
|
|
|
if _, ok := n.roomIDToJoinedUsers[roomID]; !ok {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
return n.roomIDToJoinedUsers[roomID].values()
|
|
|
|
}
|
|
|
|
|
2022-04-07 11:08:15 -05:00
|
|
|
func (n *Notifier) _addPeekingDevice(roomID, userID, deviceID string) {
|
2020-09-10 08:39:18 -05:00
|
|
|
if _, ok := n.roomIDToPeekingDevices[roomID]; !ok {
|
|
|
|
n.roomIDToPeekingDevices[roomID] = make(peekingDeviceSet)
|
|
|
|
}
|
|
|
|
n.roomIDToPeekingDevices[roomID].add(types.PeekingDevice{UserID: userID, DeviceID: deviceID})
|
|
|
|
}
|
|
|
|
|
2022-04-07 11:08:15 -05:00
|
|
|
func (n *Notifier) _removePeekingDevice(roomID, userID, deviceID string) {
|
2020-09-10 08:39:18 -05:00
|
|
|
if _, ok := n.roomIDToPeekingDevices[roomID]; !ok {
|
|
|
|
n.roomIDToPeekingDevices[roomID] = make(peekingDeviceSet)
|
|
|
|
}
|
|
|
|
// XXX: is this going to work as a key?
|
|
|
|
n.roomIDToPeekingDevices[roomID].remove(types.PeekingDevice{UserID: userID, DeviceID: deviceID})
|
|
|
|
}
|
|
|
|
|
|
|
|
func (n *Notifier) PeekingDevices(roomID string) (peekingDevices []types.PeekingDevice) {
|
2022-04-07 11:08:15 -05:00
|
|
|
n.lock.RLock()
|
|
|
|
defer n.lock.RUnlock()
|
|
|
|
return n._peekingDevices(roomID)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (n *Notifier) _peekingDevices(roomID string) (peekingDevices []types.PeekingDevice) {
|
2020-09-10 08:39:18 -05:00
|
|
|
if _, ok := n.roomIDToPeekingDevices[roomID]; !ok {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
return n.roomIDToPeekingDevices[roomID].values()
|
|
|
|
}
|
|
|
|
|
2022-04-07 11:08:15 -05:00
|
|
|
// _removeEmptyUserStreams iterates through the user stream map and removes any
|
2017-10-26 05:34:54 -05:00
|
|
|
// that have been empty for a certain amount of time. This is a crude way of
|
|
|
|
// ensuring that the userStreams map doesn't grow forver.
|
|
|
|
// This should be called when the notifier gets called for whatever reason,
|
|
|
|
// the function itself is responsible for ensuring it doesn't iterate too
|
|
|
|
// often.
|
2022-04-07 11:08:15 -05:00
|
|
|
func (n *Notifier) _removeEmptyUserStreams() {
|
2017-10-26 05:34:54 -05:00
|
|
|
// Only clean up now and again
|
|
|
|
now := time.Now()
|
|
|
|
if n.lastCleanUpTime.Add(time.Minute).After(now) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
n.lastCleanUpTime = now
|
|
|
|
|
|
|
|
deleteBefore := now.Add(-5 * time.Minute)
|
2020-05-28 04:05:04 -05:00
|
|
|
for user, byUser := range n.userDeviceStreams {
|
|
|
|
for device, stream := range byUser {
|
|
|
|
if stream.TimeOfLastNonEmpty().Before(deleteBefore) {
|
|
|
|
delete(n.userDeviceStreams[user], device)
|
|
|
|
}
|
|
|
|
if len(n.userDeviceStreams[user]) == 0 {
|
|
|
|
delete(n.userDeviceStreams, user)
|
|
|
|
}
|
2017-10-26 05:34:54 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-17 09:38:24 -05:00
|
|
|
// A string set, mainly existing for improving clarity of structs in this file.
|
2022-04-13 06:35:30 -05:00
|
|
|
type userIDSet struct {
|
|
|
|
sync.Mutex
|
|
|
|
set map[string]struct{}
|
|
|
|
precomputed []string
|
|
|
|
}
|
|
|
|
|
|
|
|
func newUserIDSet(cap int) *userIDSet {
|
|
|
|
return &userIDSet{
|
|
|
|
set: make(map[string]struct{}, cap),
|
|
|
|
precomputed: nil,
|
|
|
|
}
|
|
|
|
}
|
2017-05-17 09:38:24 -05:00
|
|
|
|
2022-04-13 06:35:30 -05:00
|
|
|
func (s *userIDSet) add(str string) {
|
|
|
|
s.Lock()
|
|
|
|
defer s.Unlock()
|
|
|
|
s.set[str] = struct{}{}
|
|
|
|
s.precomputed = s.precomputed[:0] // invalidate cache
|
2017-05-17 09:38:24 -05:00
|
|
|
}
|
|
|
|
|
2022-04-13 06:35:30 -05:00
|
|
|
func (s *userIDSet) remove(str string) {
|
|
|
|
s.Lock()
|
|
|
|
defer s.Unlock()
|
|
|
|
delete(s.set, str)
|
|
|
|
s.precomputed = s.precomputed[:0] // invalidate cache
|
2017-05-17 09:38:24 -05:00
|
|
|
}
|
|
|
|
|
2022-04-13 06:35:30 -05:00
|
|
|
func (s *userIDSet) precompute() {
|
|
|
|
s.Lock()
|
|
|
|
defer s.Unlock()
|
|
|
|
s.precomputed = s.values()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *userIDSet) isIn(str string) bool {
|
|
|
|
s.Lock()
|
|
|
|
defer s.Unlock()
|
|
|
|
_, ok := s.set[str]
|
|
|
|
return ok
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *userIDSet) values() (vals []string) {
|
|
|
|
if len(s.precomputed) > 0 {
|
|
|
|
return s.precomputed // only return if not invalidated
|
|
|
|
}
|
|
|
|
vals = make([]string, 0, len(s.set))
|
|
|
|
for str := range s.set {
|
2017-05-17 09:38:24 -05:00
|
|
|
vals = append(vals, str)
|
2017-05-10 04:42:00 -05:00
|
|
|
}
|
2017-05-17 09:38:24 -05:00
|
|
|
return
|
2017-05-10 04:42:00 -05:00
|
|
|
}
|
2020-09-10 08:39:18 -05:00
|
|
|
|
|
|
|
// A set of PeekingDevices, similar to userIDSet
|
|
|
|
|
2022-04-06 09:04:41 -05:00
|
|
|
type peekingDeviceSet map[types.PeekingDevice]struct{}
|
2020-09-10 08:39:18 -05:00
|
|
|
|
|
|
|
func (s peekingDeviceSet) add(d types.PeekingDevice) {
|
2022-04-06 09:04:41 -05:00
|
|
|
s[d] = struct{}{}
|
2020-09-10 08:39:18 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// nolint:unused
|
|
|
|
func (s peekingDeviceSet) remove(d types.PeekingDevice) {
|
|
|
|
delete(s, d)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s peekingDeviceSet) values() (vals []types.PeekingDevice) {
|
2022-04-06 07:31:44 -05:00
|
|
|
vals = make([]types.PeekingDevice, 0, len(s))
|
2020-09-10 08:39:18 -05:00
|
|
|
for d := range s {
|
|
|
|
vals = append(vals, d)
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|