-
Notifications
You must be signed in to change notification settings - Fork 211
/
map.go
158 lines (121 loc) · 2.62 KB
/
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
package noise
import (
"container/list"
"errors"
"math"
"sync"
)
type clientMapEntry struct {
el *list.Element
client *Client
}
type clientMap struct {
sync.Mutex
cap uint
order *list.List
entries map[string]clientMapEntry
}
func newClientMap(cap uint) *clientMap {
return &clientMap{
cap: cap,
order: list.New(),
entries: make(map[string]clientMapEntry, cap),
}
}
func (c *clientMap) get(n *Node, addr string) (*Client, bool) {
c.Lock()
defer c.Unlock()
entry, exists := c.entries[addr]
if !exists {
if uint(len(c.entries)) == n.maxInboundConnections {
el := c.order.Back()
evicted := c.order.Remove(el).(string)
e := c.entries[evicted]
delete(c.entries, evicted)
e.client.close()
e.client.waitUntilClosed()
}
entry.el = c.order.PushFront(addr)
entry.client = newClient(n)
c.entries[addr] = entry
} else {
c.order.MoveToFront(entry.el)
}
return entry.client, exists
}
func (c *clientMap) remove(addr string) {
c.Lock()
defer c.Unlock()
entry, exists := c.entries[addr]
if !exists {
return
}
c.order.Remove(entry.el)
delete(c.entries, addr)
}
func (c *clientMap) release() {
c.Lock()
entries := c.entries
c.entries = make(map[string]clientMapEntry, c.cap)
c.order.Init()
c.Unlock()
for _, e := range entries {
e.client.close()
e.client.waitUntilClosed()
}
}
func (c *clientMap) slice() []*Client {
c.Lock()
defer c.Unlock()
clients := make([]*Client, 0, len(c.entries))
for el := c.order.Front(); el != nil; el = el.Next() {
clients = append(clients, c.entries[el.Value.(string)].client)
}
return clients
}
type requestMap struct {
sync.Mutex
entries map[uint64]chan message
nonce uint64
}
func newRequestMap() *requestMap {
return &requestMap{entries: make(map[uint64]chan message)}
}
func (r *requestMap) nextNonce() (<-chan message, uint64, error) {
r.Lock()
defer r.Unlock()
if r.nonce == math.MaxUint64 {
r.nonce = 0
}
r.nonce++
nonce := r.nonce
if _, exists := r.entries[nonce]; exists {
return nil, 0, errors.New("ran out of available nonce to use for making a new request")
}
ch := make(chan message, 1)
r.entries[nonce] = ch
return ch, nonce, nil
}
func (r *requestMap) markRequestFailed(nonce uint64) {
r.Lock()
defer r.Unlock()
close(r.entries[nonce])
delete(r.entries, nonce)
}
func (r *requestMap) findRequest(nonce uint64) chan<- message {
r.Lock()
defer r.Unlock()
ch, exists := r.entries[nonce]
if exists {
delete(r.entries, nonce)
}
return ch
}
func (r *requestMap) close() {
r.Lock()
defer r.Unlock()
for nonce := range r.entries {
close(r.entries[nonce])
delete(r.entries, nonce)
}
}