Review comments

This commit is contained in:
Kegan Dougal 2017-05-16 12:27:40 +01:00
parent 474e836b8b
commit d7de6c3f33

View file

@ -35,9 +35,8 @@ type Notifier struct {
// for concurrent reads on /sync requests // for concurrent reads on /sync requests
currPos types.StreamPosition currPos types.StreamPosition
currPosMutex *sync.RWMutex currPosMutex *sync.RWMutex
// A map of RoomID => Set<UserID> : Map access is guarded by roomIDToJoinedUsersMutex. // A map of RoomID => Set<UserID>
roomIDToJoinedUsers map[string]set roomIDToJoinedUsers map[string]set
roomIDToJoinedUsersMutex *sync.Mutex
// A map of user_id => UserStream which can be used to wake a given user's /sync request. // A map of user_id => UserStream which can be used to wake a given user's /sync request.
// Map access is guarded by userStreamsMutex. // Map access is guarded by userStreamsMutex.
userStreams map[string]*UserStream userStreams map[string]*UserStream
@ -52,7 +51,6 @@ func NewNotifier(pos types.StreamPosition) *Notifier {
currPos: pos, currPos: pos,
currPosMutex: &sync.RWMutex{}, currPosMutex: &sync.RWMutex{},
roomIDToJoinedUsers: make(map[string]set), roomIDToJoinedUsers: make(map[string]set),
roomIDToJoinedUsersMutex: &sync.Mutex{},
userStreams: make(map[string]*UserStream), userStreams: make(map[string]*UserStream),
userStreamsMutex: &sync.Mutex{}, userStreamsMutex: &sync.Mutex{},
} }
@ -88,7 +86,7 @@ func (n *Notifier) OnNewEvent(ev *gomatrixserverlib.Event, pos types.StreamPosit
case "leave": case "leave":
fallthrough fallthrough
case "ban": case "ban":
n.userLeft(ev.RoomID(), userID) n.removeJoinedUser(ev.RoomID(), userID)
} }
} }
} }
@ -139,9 +137,7 @@ func (n *Notifier) Load(db *storage.SyncServerDatabase) error {
// these rooms will wake the given users /sync requests. This should be called prior to ANY calls to // 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. // OnNewEvent (eg on startup) to prevent racing.
func (n *Notifier) setUsersJoinedToRooms(roomIDToUserIDs map[string][]string) { func (n *Notifier) setUsersJoinedToRooms(roomIDToUserIDs map[string][]string) {
// This is just the bulk form of userJoined where we only lock once. // This is just the bulk form of addJoinedUser
n.roomIDToJoinedUsersMutex.Lock()
defer n.roomIDToJoinedUsersMutex.Unlock()
for roomID, userIDs := range roomIDToUserIDs { for roomID, userIDs := range roomIDToUserIDs {
if _, ok := n.roomIDToJoinedUsers[roomID]; !ok { if _, ok := n.roomIDToJoinedUsers[roomID]; !ok {
n.roomIDToJoinedUsers[roomID] = make(set) n.roomIDToJoinedUsers[roomID] = make(set)
@ -180,27 +176,24 @@ func (n *Notifier) fetchUserStream(userID string, makeIfNotExists bool) *UserStr
return stream return stream
} }
// Not thread-safe: must be called on the OnNewEvent goroutine only
func (n *Notifier) addJoinedUser(roomID, userID string) { func (n *Notifier) addJoinedUser(roomID, userID string) {
n.roomIDToJoinedUsersMutex.Lock()
defer n.roomIDToJoinedUsersMutex.Unlock()
if _, ok := n.roomIDToJoinedUsers[roomID]; !ok { if _, ok := n.roomIDToJoinedUsers[roomID]; !ok {
n.roomIDToJoinedUsers[roomID] = make(set) n.roomIDToJoinedUsers[roomID] = make(set)
} }
n.roomIDToJoinedUsers[roomID].add(userID) n.roomIDToJoinedUsers[roomID].add(userID)
} }
func (n *Notifier) userLeft(roomID, userID string) { // Not thread-safe: must be called on the OnNewEvent goroutine only
n.roomIDToJoinedUsersMutex.Lock() func (n *Notifier) removeJoinedUser(roomID, userID string) {
defer n.roomIDToJoinedUsersMutex.Unlock()
if _, ok := n.roomIDToJoinedUsers[roomID]; !ok { if _, ok := n.roomIDToJoinedUsers[roomID]; !ok {
n.roomIDToJoinedUsers[roomID] = make(set) n.roomIDToJoinedUsers[roomID] = make(set)
} }
n.roomIDToJoinedUsers[roomID].remove(userID) n.roomIDToJoinedUsers[roomID].remove(userID)
} }
// Not thread-safe: must be called on the OnNewEvent goroutine only
func (n *Notifier) joinedUsers(roomID string) (userIDs []string) { func (n *Notifier) joinedUsers(roomID string) (userIDs []string) {
n.roomIDToJoinedUsersMutex.Lock()
defer n.roomIDToJoinedUsersMutex.Unlock()
if _, ok := n.roomIDToJoinedUsers[roomID]; !ok { if _, ok := n.roomIDToJoinedUsers[roomID]; !ok {
return return
} }
@ -218,11 +211,6 @@ func (s set) remove(str string) {
delete(s, str) delete(s, str)
} }
func (s set) has(str string) bool {
_, ok := s[str]
return ok
}
func (s set) values() (vals []string) { func (s set) values() (vals []string) {
for str := range s { for str := range s {
vals = append(vals, str) vals = append(vals, str)