This repository has been archived by the owner on Dec 7, 2019. It is now read-only.
forked from jbenet/go-peerstream
-
Notifications
You must be signed in to change notification settings - Fork 4
/
swarm.go
440 lines (382 loc) · 11.8 KB
/
swarm.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
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
package peerstream
import (
"errors"
"fmt"
"sync"
"time"
tpt "github.com/libp2p/go-libp2p-transport"
smux "github.com/libp2p/go-stream-muxer"
)
// GarbageCollectTimeout governs the periodic connection closer.
var GarbageCollectTimeout = 5 * time.Second
// Swarm represents a group of streams, connections and listeners which
// are interconnected using a multiplexed transport.
// Swarms keep track of user-added handlers which
// define the actions upon the arrival of new streams and handlers.
type Swarm struct {
// the transport we'll use.
transport smux.Transport
// active streams.
streams map[*Stream]struct{}
streamLock sync.RWMutex
// active connections. generate new Streams
conns map[*Conn]struct{}
connIdx map[Group]map[*Conn]struct{}
connLock sync.RWMutex
// active listeners. generate new Listeners
listeners map[*Listener]struct{}
listenerLock sync.RWMutex
// these handlers should be accessed with their getter/setter
// as this pointer may be changed at any time.
handlerLock sync.RWMutex // protects the functions below
connHandler ConnHandler // receives Conns intiated remotely
streamHandler StreamHandler // receives Streams initiated remotely
selectConn SelectConn // default SelectConn function
// notification listeners
notifiees map[Notifiee]struct{}
notifieeLock sync.Mutex
closed chan struct{}
}
// NewSwarm creates a new swarm with the given multiplexed transport.
func NewSwarm(t smux.Transport) *Swarm {
s := &Swarm{
transport: t,
streams: make(map[*Stream]struct{}),
conns: make(map[*Conn]struct{}),
connIdx: make(map[Group]map[*Conn]struct{}),
listeners: make(map[*Listener]struct{}),
notifiees: make(map[Notifiee]struct{}),
selectConn: SelectRandomConn,
streamHandler: NoOpStreamHandler,
connHandler: NoOpConnHandler,
closed: make(chan struct{}),
}
go s.connGarbageCollect()
return s
}
// String returns a string with various internal stats
func (s *Swarm) String() string {
s.listenerLock.RLock()
ls := len(s.listeners)
s.listenerLock.RUnlock()
s.connLock.RLock()
cs := len(s.conns)
s.connLock.RUnlock()
s.streamLock.RLock()
ss := len(s.streams)
s.streamLock.RUnlock()
str := "<peerstream.Swarm %d listeners %d conns %d streams>"
return fmt.Sprintf(str, ls, cs, ss)
}
// Dump returns a string with all the internal state
func (s *Swarm) Dump() string {
str := s.String() + "\n"
s.listenerLock.RLock()
for l := range s.listeners {
str += fmt.Sprintf("\t%s %v\n", l, l.Groups())
}
s.listenerLock.RUnlock()
s.connLock.RLock()
for c := range s.conns {
str += fmt.Sprintf("\t%s %v\n", c, c.Groups())
}
s.connLock.RUnlock()
s.streamLock.RLock()
for ss := range s.streams {
str += fmt.Sprintf("\t%s %v\n", ss, ss.Groups())
}
s.streamLock.RUnlock()
return str
}
// SetStreamHandler assigns the stream handler in the swarm.
// The handler assumes responsibility for closing the stream.
// This need not happen at the end of the handler, leaving the
// stream open (to be used and closed later) is fine.
// It is also fine to keep a pointer to the Stream.
// This is a threadsafe (atomic) operation
func (s *Swarm) SetStreamHandler(sh StreamHandler) {
s.handlerLock.Lock()
defer s.handlerLock.Unlock()
s.streamHandler = sh
}
// StreamHandler returns the Swarm's current StreamHandler.
// This is a threadsafe (atomic) operation
func (s *Swarm) StreamHandler() StreamHandler {
s.handlerLock.RLock()
defer s.handlerLock.RUnlock()
if s.streamHandler == nil {
return NoOpStreamHandler
}
return s.streamHandler
}
// SetConnHandler assigns the conn handler in the swarm.
// Unlike the StreamHandler, the ConnHandler has less respon-
// ibility for the Connection. The Swarm is still its client.
// This handler is only a notification.
// This is a threadsafe (atomic) operation
func (s *Swarm) SetConnHandler(ch ConnHandler) {
s.handlerLock.Lock()
defer s.handlerLock.Unlock()
s.connHandler = ch
}
// ConnHandler returns the Swarm's current ConnHandler.
// This is a threadsafe (atomic) operation
func (s *Swarm) ConnHandler() ConnHandler {
s.handlerLock.RLock()
defer s.handlerLock.RUnlock()
if s.connHandler == nil {
return NoOpConnHandler
}
return s.connHandler
}
// SetSelectConn assigns the connection selector in the swarm.
// If cs is nil, will use SelectRandomConn
// This is a threadsafe (atomic) operation
func (s *Swarm) SetSelectConn(cs SelectConn) {
s.handlerLock.Lock()
defer s.handlerLock.Unlock()
s.selectConn = cs
}
// SelectConn returns the Swarm's current connection selector.
// SelectConn is used in order to select the best of a set of
// possible connections. The default chooses one at random.
// This is a threadsafe (atomic) operation
func (s *Swarm) SelectConn() SelectConn {
s.handlerLock.RLock()
defer s.handlerLock.RUnlock()
if s.selectConn == nil {
return SelectRandomConn
}
return s.selectConn
}
// Conns returns all the connections associated with this Swarm.
func (s *Swarm) Conns() []*Conn {
s.connLock.RLock()
conns := make([]*Conn, 0, len(s.conns))
for c := range s.conns {
conns = append(conns, c)
}
s.connLock.RUnlock()
open := make([]*Conn, 0, len(conns))
for _, c := range conns {
// TODO: unmuxed connections won't be garbage collected for now.
// This isnt a common usecase and is only here for a few test applications
// in the future, we will fix this
if c.smuxConn != nil && c.smuxConn.IsClosed() {
c.GoClose()
} else {
open = append(open, c)
}
}
return open
}
// ConnsWithGroup returns all the connections with a given Group
func (s *Swarm) ConnsWithGroup(g Group) []*Conn {
s.connLock.RLock()
cs := s.connIdx[g]
conns := make([]*Conn, 0, len(cs))
for c := range cs {
conns = append(conns, c)
}
s.connLock.RUnlock()
for i := 0; i < len(conns); {
c := conns[i]
if c.smuxConn != nil && c.smuxConn.IsClosed() {
c.GoClose()
conns[i] = conns[len(conns)-1]
conns[len(conns)-1] = nil
conns = conns[:len(conns)-1]
} else {
i++
}
}
return conns
}
// Listeners returns all the listeners associated with this Swarm.
func (s *Swarm) Listeners() []*Listener {
s.listenerLock.RLock()
out := make([]*Listener, 0, len(s.listeners))
for c := range s.listeners {
out = append(out, c)
}
s.listenerLock.RUnlock()
return out
}
// Streams returns all the streams associated with this Swarm.
func (s *Swarm) Streams() []*Stream {
s.streamLock.RLock()
out := make([]*Stream, 0, len(s.streams))
for c := range s.streams {
out = append(out, c)
}
s.streamLock.RUnlock()
return out
}
// AddListener adds libp2p-transport Listener to the Swarm,
// and immediately begins accepting incoming connections.
func (s *Swarm) AddListener(l tpt.Listener, groups ...Group) (*Listener, error) {
return s.addListener(l, groups)
}
// AddListenerWithRateLimit adds Listener to the Swarm, and immediately
// begins accepting incoming connections. The rate of connection acceptance
// depends on the RateLimit option
// func (s *Swarm) AddListenerWithRateLimit(net.Listner, RateLimit) // TODO
// AddConn gives the Swarm ownership of tpt.Conn. The Swarm will negotiate an
// appropriate multiplexer for the connection and and begin listening for
// Streams. Returns the resulting Swarm-associated peerstream.Conn.
//
// Do not use the tpt.Conn once you've passed it to this method.
func (s *Swarm) AddConn(tptConn tpt.Conn, groups ...Group) (*Conn, error) {
return s.addConn(tptConn, false, groups)
}
// NewStream opens a new Stream on the best available connection,
// as selected by current swarm.SelectConn.
func (s *Swarm) NewStream() (*Stream, error) {
return s.NewStreamSelectConn(s.SelectConn())
}
func (s *Swarm) newStreamSelectConn(selConn SelectConn, conns []*Conn) (*Stream, error) {
if selConn == nil {
return nil, errors.New("nil SelectConn")
}
best := selConn(conns)
if best == nil || !ConnInConns(best, conns) {
return nil, ErrInvalidConnSelected
}
return s.NewStreamWithConn(best)
}
// NewStreamSelectConn opens a new Stream on a connection selected
// by selConn.
func (s *Swarm) NewStreamSelectConn(selConn SelectConn) (*Stream, error) {
if selConn == nil {
return nil, errors.New("nil SelectConn")
}
conns := s.Conns()
if len(conns) == 0 {
return nil, ErrNoConnections
}
return s.newStreamSelectConn(selConn, conns)
}
// NewStreamWithGroup opens a new Stream on an available connection in
// the given group. Uses the current swarm.SelectConn to pick between
// multiple connections.
func (s *Swarm) NewStreamWithGroup(group Group) (*Stream, error) {
conns := s.ConnsWithGroup(group)
return s.newStreamSelectConn(s.SelectConn(), conns)
}
// NewStreamWithNetConn opens a new Stream on a given libp2p-transport Conn.
// Calls s.AddConn(Conn).
func (s *Swarm) NewStreamWithNetConn(netConn tpt.Conn) (*Stream, error) {
c, err := s.AddConn(netConn)
if err != nil {
return nil, err
}
return s.NewStreamWithConn(c)
}
// NewStreamWithConn opens a new Stream on given Conn.
func (s *Swarm) NewStreamWithConn(conn *Conn) (*Stream, error) {
if conn == nil {
return nil, errors.New("nil Conn")
}
if conn.Swarm() != s {
return nil, errors.New("connection not associated with swarm")
}
if conn.smuxConn == nil {
return nil, errors.New("connection does not support multiplexing streams")
}
if conn.smuxConn.IsClosed() {
go conn.Close()
return nil, errors.New("conn is closed")
}
s.connLock.RLock()
if _, found := s.conns[conn]; !found {
s.connLock.RUnlock()
return nil, errors.New("connection not associated with swarm")
}
s.connLock.RUnlock()
return s.createStream(conn)
}
// StreamsWithGroup returns all the streams with a given Group
func (s *Swarm) StreamsWithGroup(g Group) []*Stream {
return StreamsWithGroup(g, s.Streams())
}
// Close shuts down the Swarm, and it's listeners.
func (s *Swarm) Close() error {
defer close(s.closed)
// automatically close everything new we get.
s.SetConnHandler(func(c *Conn) { c.Close() })
s.SetStreamHandler(func(s *Stream) { s.Reset() })
var wgl sync.WaitGroup
for _, l := range s.Listeners() {
wgl.Add(1)
go func(list *Listener) {
list.Close()
wgl.Done()
}(l)
}
wgl.Wait()
var wgc sync.WaitGroup
for _, c := range s.Conns() {
wgc.Add(1)
go func(conn *Conn) {
conn.Close()
wgc.Done()
}(c)
}
wgc.Wait()
return nil
}
// connGarbageCollect periodically sweeps conns to make sure
// they're still alive. if any are closed, remvoes them.
func (s *Swarm) connGarbageCollect() {
for {
select {
case <-s.closed:
return
case <-time.After(GarbageCollectTimeout):
}
for _, c := range s.Conns() {
if c.smuxConn != nil && c.smuxConn.IsClosed() {
go c.Close()
}
}
}
}
// Notify signs up Notifiee to receive signals when events happen
func (s *Swarm) Notify(n Notifiee) {
s.notifieeLock.Lock()
s.notifiees[n] = struct{}{}
s.notifieeLock.Unlock()
}
// StopNotify unregisters Notifiee fromr receiving signals
func (s *Swarm) StopNotify(n Notifiee) {
s.notifieeLock.Lock()
delete(s.notifiees, n)
s.notifieeLock.Unlock()
}
// notifyAll runs the notification function on all Notifiees
func (s *Swarm) notifyAll(notification func(n Notifiee)) {
s.notifieeLock.Lock()
var wg sync.WaitGroup
for n := range s.notifiees {
// make sure we dont block
// and they dont block each other.
wg.Add(1)
go func(n Notifiee) {
defer wg.Done()
notification(n)
}(n)
}
wg.Wait()
s.notifieeLock.Unlock()
}
// Notifiee is an interface for an object wishing to receive
// notifications from a Swarm. Notifiees should take care not to register other
// notifiees inside of a notification. They should also take care to do as
// little work as possible within their notification, putting any blocking work
// out into a goroutine.
type Notifiee interface {
Connected(*Conn) // called when a connection opened
Disconnected(*Conn) // called when a connection closed
OpenedStream(*Stream) // called when a stream opened
ClosedStream(*Stream) // called when a stream closed
}