Skip to content
This repository has been archived by the owner on Aug 1, 2020. It is now read-only.

Commit

Permalink
Allow locking state store completely
Browse files Browse the repository at this point in the history
  • Loading branch information
tulir committed Dec 16, 2018
1 parent c432f77 commit edcf9d4
Showing 1 changed file with 35 additions and 0 deletions.
35 changes: 35 additions & 0 deletions statestore.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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:"-"`
Expand All @@ -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()
Expand Down

0 comments on commit edcf9d4

Please sign in to comment.