-
Notifications
You must be signed in to change notification settings - Fork 14
/
request_map.go
64 lines (52 loc) · 1.23 KB
/
request_map.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
56
57
58
59
60
61
62
63
64
package tarantool
import "sync"
const requestMapShardNum = 16
type requestMapShard struct {
sync.Mutex
data map[uint64]*request
}
type requestMap struct {
shard []*requestMapShard
}
func newRequestMap() *requestMap {
shard := make([]*requestMapShard, requestMapShardNum)
for i := 0; i < requestMapShardNum; i++ {
shard[i] = &requestMapShard{
data: make(map[uint64]*request),
}
}
return &requestMap{
shard: shard,
}
}
// Put returns old request associated with given key
func (m *requestMap) Put(key uint64, value *request) *request {
shard := m.shard[key%requestMapShardNum]
shard.Lock()
oldValue := shard.data[key]
shard.data[key] = value
shard.Unlock()
return oldValue
}
// Pop returns request associated with given key and remove it from map
func (m *requestMap) Pop(key uint64) *request {
shard := m.shard[key%requestMapShardNum]
shard.Lock()
value, exists := shard.data[key]
if exists {
delete(shard.data, key)
}
shard.Unlock()
return value
}
func (m *requestMap) CleanUp(clearCallback func(*request)) {
for i := 0; i < requestMapShardNum; i++ {
shard := m.shard[i]
shard.Lock()
for requestID, req := range shard.data {
delete(shard.data, requestID)
clearCallback(req)
}
shard.Unlock()
}
}