remove locking from accounts service

add a cached named rwlock pkg
use sync.map in the cache pkg
use named rwlock in indexer pkg
use sync.map in indexer pkg
remove husky
This commit is contained in:
Florian Schade
2021-01-19 23:23:41 +01:00
parent f97f0d7342
commit a02fb890f7
11 changed files with 10554 additions and 131 deletions
+67
View File
@@ -0,0 +1,67 @@
package sync
import (
"sync"
)
type NRWMutex struct {
m sync.Mutex
mm map[string]*nrw
}
type nrw struct {
m sync.RWMutex
c int
}
func NewNRWMutex() NRWMutex {
return NRWMutex{mm: make(map[string]*nrw)}
}
func (c *NRWMutex) Lock(k string) {
c.m.Lock()
m := c.get(k)
m.c++
c.m.Unlock()
m.m.Lock()
}
func (c *NRWMutex) Unlock(k string) {
c.m.Lock()
defer c.m.Unlock()
m := c.get(k)
m.m.Unlock()
m.c--
if m.c == 0 {
delete(c.mm, k)
}
}
func (c *NRWMutex) RLock(k string) {
c.m.Lock()
m := c.get(k)
m.c++
c.m.Unlock()
m.m.RLock()
}
func (c *NRWMutex) RUnlock(k string) {
c.m.Lock()
defer c.m.Unlock()
m := c.get(k)
m.m.RUnlock()
m.c--
if m.c == 0 {
delete(c.mm, k)
}
}
func (c *NRWMutex) get(k string) *nrw {
m, ok := c.mm[k]
if !ok {
m = &nrw{}
c.mm[k] = m
}
return m
}
+33
View File
@@ -0,0 +1,33 @@
package sync
import (
"fmt"
"runtime"
"testing"
)
func HammerMutex(m *NRWMutex, loops int, cdone chan bool) {
for i := 0; i < loops; i++ {
id := fmt.Sprintf("%v", i)
m.Lock(id)
m.Unlock(id)
}
cdone <- true
}
func TestMutex(t *testing.T) {
if n := runtime.SetMutexProfileFraction(1); n != 0 {
t.Logf("got mutexrate %d expected 0", n)
}
defer runtime.SetMutexProfileFraction(0)
m := NewNRWMutex()
c := make(chan bool)
r := 10
for i := 0; i < r; i++ {
go HammerMutex(&m, 2000, c)
}
for i := 0; i < r; i++ {
<-c
}
}