-
Notifications
You must be signed in to change notification settings - Fork 211
/
Copy pathmap.go
140 lines (106 loc) · 2.3 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
package noise
import (
"errors"
"math"
"sync"
)
type clientMap struct {
sync.Mutex
order []string
entries map[string]*Client
}
func newClientMap(cap int) *clientMap {
return &clientMap{
order: make([]string, 0, cap),
entries: make(map[string]*Client, cap),
}
}
func (c *clientMap) get(n *Node, addr string) (*Client, bool) {
c.Lock()
defer c.Unlock()
client, exists := c.entries[addr]
if !exists {
if len(c.entries) == n.maxInboundConnections {
closing := c.order[0]
c.order = c.order[1:]
client := c.entries[closing]
delete(c.entries, closing)
client.close()
client.waitUntilClosed()
}
client = newClient(n)
c.entries[addr] = client
c.order = append(c.order, addr)
}
return client, exists
}
func (c *clientMap) remove(addr string) {
c.Lock()
defer c.Unlock()
var order []string
for _, entry := range c.order {
if entry != addr {
order = append(order, entry)
}
}
c.order = order
delete(c.entries, addr)
}
func (c *clientMap) release() {
c.Lock()
entries := c.entries
c.entries = make(map[string]*Client, cap(c.order))
c.order = make([]string, 0, cap(c.order))
c.Unlock()
for _, client := range entries {
client.close()
client.waitUntilClosed()
}
}
func (c *clientMap) slice() []*Client {
c.Lock()
defer c.Unlock()
clients := make([]*Client, len(c.entries))
for i := 0; i < len(c.entries); i++ {
clients[i] = c.entries[c.order[i]]
}
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
}