-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathstriped_lock.go
55 lines (45 loc) · 1.43 KB
/
striped_lock.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
package sync
import (
"fmt"
base "sync"
)
const (
hashEntriesPerLock = 200
)
// StripedLock is a partitioned locking mechanism that consistently maps a key
// space to a set of locks. This provides concurrent data access while also
// limiting the total memory footprint.
type StripedLock struct {
locks []base.RWMutex
hashRing *ring
}
// NewStripedLock returns a new StripedLock with a static number of stripes.
func NewStripedLock(stripes uint) *StripedLock {
return newStripedLockGroup(stripes, 1)[0]
}
// newStripedLockGroup returns a new group of StripedLocks which share a common
// hash ring. This should be used in cases where multiple striped locks are
// needed which share the same sharding domain in order to avoid allocating
// entire hash rings for each.
//
// todo: Add tests, then expose publicly
func newStripedLockGroup(stripes, groupSize uint) []*StripedLock {
stripedLocks := make([]*StripedLock, 0, groupSize)
ringEntries := make(map[string]interface{})
for i := 0; i < int(stripes); i++ {
ringEntries[fmt.Sprintf("lock%d", i)] = i
}
ring := newRing(ringEntries, hashEntriesPerLock)
for i := 0; i < int(groupSize); i++ {
stripedLocks = append(stripedLocks, &StripedLock{
locks: make([]base.RWMutex, stripes),
hashRing: ring,
})
}
return stripedLocks
}
// Get gets the lock for a key
func (l *StripedLock) Get(key []byte) *base.RWMutex {
sharded := l.hashRing.shard(key).(int)
return &l.locks[sharded]
}