-
Notifications
You must be signed in to change notification settings - Fork 0
/
link.go
317 lines (261 loc) · 5.62 KB
/
link.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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
package glow
import (
"sync"
"time"
)
// Link captures connection between two nodes.
// Data flows from x to y over the [Link].
type Link struct {
x *Node
y *Node
ch *channel
tally int
paused bool
deleted bool
}
type channel struct {
once sync.Once
closed bool
ch chan any
size int
}
type LinkOpt func(*Link)
func (l *Link) apply(opt ...LinkOpt) {
for _, o := range opt {
o(l)
}
}
// Size sets bandwidth for the Link.
func Size(k int) LinkOpt {
return func(l *Link) {
l.ch.size = k
}
}
// From returns the key of the "from" Node connected by this link.
func (l *Link) From() *Node {
return l.x
}
// To returns the key of the "to" Node connected by this link.
func (l *Link) To() *Node {
return l.y
}
// Tally returns the total count of data transmitted over the link thus far.
func (l *Link) Tally() int {
return l.tally
}
func (l *Link) Uptime() time.Duration {
if l.deleted || l.paused {
return 0
}
if !l.x.session.start.IsZero() && !l.y.session.start.IsZero() {
stop := l.x.session.stop
if stop.IsZero() {
stop = l.y.session.stop
}
if stop.IsZero() {
stop = time.Now()
}
return stop.Sub(l.y.session.start)
}
return 0
}
// AddLink connects from-node to to-node.
// Once Link is made, nodes are said to be communicating over the Link from -> to.
// See:
// - RemoveLink
// - PauseLink
// - ResumeLink
func (n *Network) AddLink(from, to string, opt ...LinkOpt) error {
if link, _ := n.link(from, to); link != nil {
if link.deleted {
return ErrNetworkNeedPurging
}
return ErrLinkAlreadyExists
}
xNode, err := n.Node(from)
if err != nil {
return err
}
yNode, err := n.Node(to)
if err != nil {
return err
}
if n.preventCycles && n.checkCycle(from, to) {
return ErrCyclesNotAllowed
}
// check if session is in progress
n.session.mu.RLock()
defer n.session.mu.RUnlock()
n.mu.Lock()
defer n.mu.Unlock()
link := &Link{
x: xNode,
y: yNode,
ch: &channel{},
}
link.apply(opt...)
if xNode.distributor {
// if there exists another egress for node-x then get existing channel
if xEgress := n.egress[xNode.Key()]; len(xEgress) > 0 {
for _, xLink := range xEgress {
link.ch = xLink.ch
break
}
}
}
if link.ch.ch == nil {
link.ch.ch = make(chan any, link.ch.size)
}
if _, ok := n.egress[from]; !ok {
n.egress[from] = make(map[string]*Link)
}
n.egress[from][to] = link
if _, ok := n.ingress[to]; !ok {
n.ingress[to] = make(map[string]*Link)
}
n.ingress[to][from] = link
return nil
}
// RemoveLink disconnects "from" Node and "to" Node.
// See:
// - AddLink
// - PauseLink
// - ResumeLink
func (n *Network) RemoveLink(from, to string) error {
link, err := n.Link(from, to)
if err != nil {
return err
}
// check if session is in progress
n.session.mu.RLock()
defer n.session.mu.RUnlock()
n.mu.Lock()
defer n.mu.Unlock()
link.deleted = true
return nil
}
func (n *Network) removeLink(l *Link) error {
delete(n.ingress[l.y.Key()], l.x.Key())
delete(n.egress[l.x.Key()], l.y.Key())
return nil
}
// PauseLink pauses communication from Node and to Node.
// See:
// - AddLink
// - ResumeLink
// - RemoveLink
func (n *Network) PauseLink(from, to string) error {
link, err := n.Link(from, to)
if err != nil {
return err
}
if link.paused {
// parity with AddLink
return ErrLinkAlreadyPaused
}
// check if session is in progress
n.session.mu.RLock()
defer n.session.mu.RUnlock()
n.mu.Lock()
defer n.mu.Unlock()
link.paused = true
return nil
}
// ResumeLink resumes communication from node and to node.
// See:
// - PauseLink
func (n *Network) ResumeLink(from, to string) error {
link, err := n.Link(from, to)
if err != nil {
return err
}
// check if session is in progress
n.session.mu.RLock()
defer n.session.mu.RUnlock()
n.mu.Lock()
defer n.mu.Unlock()
link.paused = false
return nil
}
// Link returns connection between from and to nodes if any.
func (n *Network) Link(from, to string) (*Link, error) {
n.mu.RLock()
defer n.mu.RUnlock()
link, err := n.link(from, to)
if err != nil {
return nil, err
}
if link.deleted {
return nil, ErrLinkNotFound
}
return link, nil
}
func (n *Network) link(from, to string) (*Link, error) {
outLinks, ok := n.egress[from]
if !ok {
return nil, ErrLinkNotFound
}
link, ok := outLinks[to]
if !ok {
return nil, ErrLinkNotFound
}
return link, nil
}
// Links returns all the links in the Network.
func (n *Network) Links() []*Link {
n.mu.RLock()
defer n.mu.RUnlock()
var links []*Link
for _, outLinks := range n.egress {
for _, link := range outLinks {
links = append(links, link)
}
}
return links
}
// Ingress returns all the ingress links for the Node.
func (n *Network) Ingress(key string) []*Link {
n.mu.RLock()
defer n.mu.RUnlock()
ingress := n.ingress[key]
if len(ingress) < 1 {
return nil
}
var links []*Link
for _, link := range ingress {
links = append(links, link)
}
return links
}
// Egress returns all the egress links for the Node.
func (n *Network) Egress(key string) []*Link {
n.mu.RLock()
defer n.mu.RUnlock()
egress := n.egress[key]
if len(egress) < 1 {
return nil
}
var links []*Link
for _, link := range egress {
links = append(links, link)
}
return links
}
// closeEgress closes all outgoing links for the Node.
func (n *Network) closeEgress(key string) {
for _, link := range n.Egress(key) {
link.ch.once.Do(func() {
close(link.ch.ch)
link.ch.closed = true
})
}
}
func (n *Network) refreshLinks(node *Node) {
for _, link := range n.Egress(node.Key()) {
if link.ch.closed {
link.ch.closed = false
link.ch.once = sync.Once{}
link.ch.ch = make(chan any, link.ch.size)
}
}
}