-
Notifications
You must be signed in to change notification settings - Fork 788
/
shard_map.go
113 lines (100 loc) · 2.42 KB
/
shard_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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
/*
* Copyright 2021 CloudWeGo Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package netpollmux
import (
"sync"
"github.com/cloudwego/kitex/pkg/remote"
)
// EventHandler is used to handle events
type EventHandler interface {
Recv(bufReader remote.ByteBuffer, err error) error
}
// A concurrent safe <seqID,EventHandler> map
// To avoid lock bottlenecks this map is dived to several (SHARD_COUNT) map shards.
type shardMap struct {
size int32
shards []*shard
}
// A "thread" safe string to anything map.
type shard struct {
msgs map[int32]EventHandler
sync.RWMutex
}
// Creates a new concurrent map.
func newShardMap(size int) *shardMap {
m := &shardMap{
size: int32(size),
shards: make([]*shard, size),
}
for i := range m.shards {
m.shards[i] = &shard{
msgs: make(map[int32]EventHandler),
}
}
return m
}
// getShard returns shard under given seq id
func (m *shardMap) getShard(seqID int32) *shard {
return m.shards[abs(seqID)%m.size]
}
// store stores msg under given seq id.
func (m *shardMap) store(seqID int32, msg EventHandler) {
if seqID == 0 {
return
}
// Get map shard.
shard := m.getShard(seqID)
shard.Lock()
shard.msgs[seqID] = msg
shard.Unlock()
}
// load loads the msg under the seq id.
func (m *shardMap) load(seqID int32) (msg EventHandler, ok bool) {
if seqID == 0 {
return nil, false
}
shard := m.getShard(seqID)
shard.RLock()
msg, ok = shard.msgs[seqID]
shard.RUnlock()
return msg, ok
}
// delete deletes the msg under the given seq id.
func (m *shardMap) delete(seqID int32) {
if seqID == 0 {
return
}
shard := m.getShard(seqID)
shard.Lock()
delete(shard.msgs, seqID)
shard.Unlock()
}
// rangeMap iterates over the map.
func (m *shardMap) rangeMap(fn func(seqID int32, msg EventHandler)) {
for _, shard := range m.shards {
shard.Lock()
for k, v := range shard.msgs {
fn(k, v)
}
shard.Unlock()
}
}
func abs(n int32) int32 {
if n < 0 {
return -n
}
return n
}