Skip to content

Commit

Permalink
rename NRWMutex to NameRWMutex
Browse files Browse the repository at this point in the history
update changelog
  • Loading branch information
fschade committed Jan 19, 2021
1 parent 4f80541 commit 873fbcb
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 31 deletions.
16 changes: 0 additions & 16 deletions changelog/unreleased/account-locking.md

This file was deleted.

17 changes: 17 additions & 0 deletions changelog/unreleased/sync-package.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
Enhancement: add named locks and refactor cache

Tags: ocis-pkg, accounts

We had the case that we needed kind of a named locking mechanism which enables us to lock only under certain conditions.
It's used in the indexer package where we do not need to lock everything, instead just lock the requested parts and differentiate between reads and writes.

This made it possible to entirely remove locks from the accounts service and move them to the ocis-pkg indexer.
Another part of this refactor was to make the cache atomic and write tests for it.

- remove locking from accounts service
- add sync package with named mutex
- add named locking to indexer
- move cache to sync package

https://github.com/owncloud/ocis/pull/1212
https://github.com/owncloud/ocis/issues/966
4 changes: 2 additions & 2 deletions ocis-pkg/indexer/indexer.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import (
type Indexer struct {
config *config.Config
indices typeMap
mu sync.NRWMutex
mu sync.NamedRWMutex
}

// IdxAddResult represents the result of an Add call on an index
Expand All @@ -35,7 +35,7 @@ func CreateIndexer(cfg *config.Config) *Indexer {
return &Indexer{
config: cfg,
indices: typeMap{},
mu: sync.NewNRWMutex(),
mu: sync.NewNamedRWMutex(),
}
}

Expand Down
20 changes: 10 additions & 10 deletions ocis-pkg/sync/nrwmutex.go → ocis-pkg/sync/mutex.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,41 +4,41 @@ import (
"sync"
)

// NRWMutex works the same as RWMutex, the only difference is that it stores mutexes in a map and reuses them.
// NamedRWMutex works the same as RWMutex, the only difference is that it stores mutexes in a map and reuses them.
// It's handy if you want to write-lock, write-unlock, read-lock and read-unlock for specific names only.
type NRWMutex struct {
type NamedRWMutex struct {
pool sync.Pool
mus sync.Map
}

// NewNRWMutex returns a new instance of NRWMutex.
func NewNRWMutex() NRWMutex {
return NRWMutex{pool: sync.Pool{New: func() interface{} {
// NewNamedRWMutex returns a new instance of NamedRWMutex.
func NewNamedRWMutex() NamedRWMutex {
return NamedRWMutex{pool: sync.Pool{New: func() interface{} {
return new(sync.RWMutex)
}}}
}

// Lock locks rw for writing.
func (m *NRWMutex) Lock(name string) {
func (m *NamedRWMutex) Lock(name string) {
m.loadOrStore(name).Lock()
}

// Unlock unlocks rw for writing.
func (m *NRWMutex) Unlock(name string) {
func (m *NamedRWMutex) Unlock(name string) {
m.loadOrStore(name).Unlock()
}

// RLock locks rw for reading.
func (m *NRWMutex) RLock(name string) {
func (m *NamedRWMutex) RLock(name string) {
m.loadOrStore(name).RLock()
}

// RUnlock undoes a single RLock call.
func (m *NRWMutex) RUnlock(name string) {
func (m *NamedRWMutex) RUnlock(name string) {
m.loadOrStore(name).RUnlock()
}

func (m *NRWMutex) loadOrStore(name string) *sync.RWMutex {
func (m *NamedRWMutex) loadOrStore(name string) *sync.RWMutex {
pmu := m.pool.Get()
mmu, loaded := m.mus.LoadOrStore(name, pmu)
if loaded {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"testing"
)

func HammerMutex(m *NRWMutex, loops int, c chan bool) {
func HammerMutex(m *NamedRWMutex, loops int, c chan bool) {
for i := 0; i < loops; i++ {
id := fmt.Sprintf("%v", i)
m.Lock(id)
Expand All @@ -15,12 +15,12 @@ func HammerMutex(m *NRWMutex, loops int, c chan bool) {
c <- true
}

func TestNRWMutex(t *testing.T) {
func TestNamedRWMutex(t *testing.T) {
if n := runtime.SetMutexProfileFraction(1); n != 0 {
t.Logf("got mutexrate %d expected 0", n)
}
defer runtime.SetMutexProfileFraction(0)
m := NewNRWMutex()
m := NewNamedRWMutex()
c := make(chan bool)
r := 10

Expand Down

0 comments on commit 873fbcb

Please sign in to comment.