From edcf9d415342721a03cda9ba0bac0b84dd221b34 Mon Sep 17 00:00:00 2001 From: Tulir Asokan Date: Sun, 16 Dec 2018 21:27:05 +0200 Subject: [PATCH] Allow locking state store completely --- statestore.go | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) diff --git a/statestore.go b/statestore.go index f75c1ca..77b942d 100644 --- a/statestore.go +++ b/statestore.go @@ -23,6 +23,11 @@ type StateStore interface { GetPowerLevel(roomID, userID string) int GetPowerLevelRequirement(roomID string, eventType gomatrix.EventType) int HasPowerLevel(roomID, userID string, eventType gomatrix.EventType) bool + + Lock() + Unlock() + RLock() + RUnlock() } func (as *AppService) UpdateState(evt *gomatrix.Event) { @@ -35,6 +40,8 @@ func (as *AppService) UpdateState(evt *gomatrix.Event) { } type BasicStateStore struct { + globalLock sync.RWMutex `json:"-"` + registrationsLock sync.RWMutex `json:"-"` Registrations map[string]bool `json:"registrations"` membershipsLock sync.RWMutex `json:"-"` @@ -55,6 +62,34 @@ func NewBasicStateStore() StateStore { } } +func (store *BasicStateStore) RLock() { + store.registrationsLock.RLock() + store.membershipsLock.RLock() + store.powerLevelsLock.RLock() + store.typingLock.RLock() +} + +func (store *BasicStateStore) RUnlock() { + store.typingLock.RUnlock() + store.powerLevelsLock.RUnlock() + store.membershipsLock.RUnlock() + store.registrationsLock.RUnlock() +} + +func (store *BasicStateStore) Lock() { + store.registrationsLock.Lock() + store.membershipsLock.Lock() + store.powerLevelsLock.Lock() + store.typingLock.Lock() +} + +func (store *BasicStateStore) Unlock() { + store.typingLock.Unlock() + store.powerLevelsLock.Unlock() + store.membershipsLock.Unlock() + store.registrationsLock.Unlock() +} + func (store *BasicStateStore) IsRegistered(userID string) bool { store.registrationsLock.RLock() defer store.registrationsLock.RUnlock()