swarm.go 8.42 KB
Newer Older
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
1
2
3
4
5
// package swarm implements a connection muxer with a pair of channels
// to synchronize all network communication.
package swarm

import (
6
	"fmt"
7
	"sync"
8
	"time"
9

Jeromy's avatar
Jeromy committed
10
	metrics "github.com/ipfs/go-ipfs/metrics"
11
	inet "github.com/ipfs/go-ipfs/p2p/net"
12
	filter "github.com/ipfs/go-ipfs/p2p/net/filter"
13
14
15
16
17
18
	addrutil "github.com/ipfs/go-ipfs/p2p/net/swarm/addr"
	peer "github.com/ipfs/go-ipfs/p2p/peer"
	eventlog "github.com/ipfs/go-ipfs/thirdparty/eventlog"

	ma "github.com/ipfs/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-multiaddr"
	ps "github.com/ipfs/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-peerstream"
19
	pst "github.com/ipfs/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-peerstream/transport"
20
	psy "github.com/ipfs/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-peerstream/transport/yamux"
21
22
	"github.com/ipfs/go-ipfs/Godeps/_workspace/src/github.com/jbenet/goprocess"
	goprocessctx "github.com/ipfs/go-ipfs/Godeps/_workspace/src/github.com/jbenet/goprocess/context"
23
	prom "github.com/ipfs/go-ipfs/Godeps/_workspace/src/github.com/prometheus/client_golang/prometheus"
24
	mafilter "github.com/ipfs/go-ipfs/Godeps/_workspace/src/github.com/whyrusleeping/multiaddr-filter"
25
	context "github.com/ipfs/go-ipfs/Godeps/_workspace/src/golang.org/x/net/context"
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
26
27
28
29
)

var log = eventlog.Logger("swarm2")

30
31
var PSTransport pst.Transport

32
33
34
35
36
37
38
var peersTotal = prom.NewGaugeVec(prom.GaugeOpts{
	Namespace: "ipfs",
	Subsystem: "p2p",
	Name:      "peers_total",
	Help:      "Number of connected peers",
}, []string{"peer_id"})

39
40
41
42
43
func init() {
	tpt := *psy.DefaultTransport
	tpt.MaxStreamWindowSize = 512 * 1024
	PSTransport = &tpt
}
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
44
45
46
47
48
49
50
51
52
53
54
55

// Swarm is a connection muxer, allowing connections to other peers to
// be opened and closed, while still using the same Chan for all
// communication. The Chan sends/receives Messages, which note the
// destination or source Peer.
//
// Uses peerstream.Swarm
type Swarm struct {
	swarm *ps.Swarm
	local peer.ID
	peers peer.Peerstore
	connh ConnHandler
56

57
	dsync dialsync
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
58
	backf dialbackoff
59
	dialT time.Duration // mainly for tests
60

61
62
63
	notifmu sync.RWMutex
	notifs  map[inet.Notifiee]ps.Notifiee

Jeromy's avatar
Jeromy committed
64
	// filters for addresses that shouldnt be dialed
65
	Filters *filter.Filters
Jeromy's avatar
Jeromy committed
66

67
	proc goprocess.Process
68
	ctx  context.Context
69
	bwc  metrics.Reporter
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
70
71
72
73
}

// NewSwarm constructs a Swarm, with a Chan.
func NewSwarm(ctx context.Context, listenAddrs []ma.Multiaddr,
Jeromy's avatar
Jeromy committed
74
	local peer.ID, peers peer.Peerstore, bwc metrics.Reporter) (*Swarm, error) {
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
75

76
77
78
	listenAddrs, err := filterAddrs(listenAddrs)
	if err != nil {
		return nil, err
79
80
	}

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
81
	s := &Swarm{
Jeromy's avatar
Jeromy committed
82
83
84
		swarm:   ps.NewSwarm(PSTransport),
		local:   local,
		peers:   peers,
85
		proc:    goprocessctx.WithContext(ctx),
86
		ctx:     ctx,
Jeromy's avatar
Jeromy committed
87
88
89
		dialT:   DialTimeout,
		notifs:  make(map[inet.Notifiee]ps.Notifiee),
		bwc:     bwc,
90
		Filters: filter.NewFilters(),
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
91
92
93
	}

	// configure Swarm
94
	s.proc.SetTeardown(s.teardown)
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
95
96
	s.SetConnHandler(nil) // make sure to setup our own conn handler.

97
98
99
100
	// setup swarm metrics
	prom.MustRegisterOrGet(peersTotal)
	s.Notify((*metricsNotifiee)(s))

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
101
102
103
104
105
106
107
	return s, s.listen(listenAddrs)
}

func (s *Swarm) teardown() error {
	return s.swarm.Close()
}

108
109
110
111
112
113
114
115
116
func (s *Swarm) AddAddrFilter(f string) error {
	m, err := mafilter.NewMask(f)
	if err != nil {
		return err
	}

	s.Filters.AddDialFilter(m)
	return nil
}
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
func filterAddrs(listenAddrs []ma.Multiaddr) ([]ma.Multiaddr, error) {
	if len(listenAddrs) > 0 {
		filtered := addrutil.FilterUsableAddrs(listenAddrs)
		if len(filtered) < 1 {
			return nil, fmt.Errorf("swarm cannot use any addr in: %s", listenAddrs)
		}
		listenAddrs = filtered
	}
	return listenAddrs, nil
}

func (s *Swarm) Listen(addrs ...ma.Multiaddr) error {
	addrs, err := filterAddrs(addrs)
	if err != nil {
		return err
	}

	return s.listen(addrs)
}

137
138
139
// Process returns the Process of the swarm
func (s *Swarm) Process() goprocess.Process {
	return s.proc
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
140
141
}

142
143
144
145
146
// Context returns the context of the swarm
func (s *Swarm) Context() context.Context {
	return s.ctx
}

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
147
148
// Close stops the Swarm.
func (s *Swarm) Close() error {
149
	return s.proc.Close()
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
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
}

// StreamSwarm returns the underlying peerstream.Swarm
func (s *Swarm) StreamSwarm() *ps.Swarm {
	return s.swarm
}

// SetConnHandler assigns the handler for new connections.
// See peerstream. You will rarely use this. See SetStreamHandler
func (s *Swarm) SetConnHandler(handler ConnHandler) {

	// handler is nil if user wants to clear the old handler.
	if handler == nil {
		s.swarm.SetConnHandler(func(psconn *ps.Conn) {
			s.connHandler(psconn)
		})
		return
	}

	s.swarm.SetConnHandler(func(psconn *ps.Conn) {
		// sc is nil if closed in our handler.
		if sc := s.connHandler(psconn); sc != nil {
			// call the user's handler. in a goroutine for sync safety.
			go handler(sc)
		}
	})
}

// SetStreamHandler assigns the handler for new streams.
// See peerstream.
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
180
func (s *Swarm) SetStreamHandler(handler inet.StreamHandler) {
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
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
	s.swarm.SetStreamHandler(func(s *ps.Stream) {
		handler(wrapStream(s))
	})
}

// NewStreamWithPeer creates a new stream on any available connection to p
func (s *Swarm) NewStreamWithPeer(p peer.ID) (*Stream, error) {
	// if we have no connections, try connecting.
	if len(s.ConnectionsToPeer(p)) == 0 {
		log.Debug("Swarm: NewStreamWithPeer no connections. Attempting to connect...")
		if _, err := s.Dial(context.Background(), p); err != nil {
			return nil, err
		}
	}
	log.Debug("Swarm: NewStreamWithPeer...")

	st, err := s.swarm.NewStreamWithGroup(p)
	return wrapStream(st), err
}

// StreamsWithPeer returns all the live Streams to p
func (s *Swarm) StreamsWithPeer(p peer.ID) []*Stream {
	return wrapStreams(ps.StreamsWithGroup(p, s.swarm.Streams()))
}

// ConnectionsToPeer returns all the live connections to p
func (s *Swarm) ConnectionsToPeer(p peer.ID) []*Conn {
	return wrapConns(ps.ConnsWithGroup(p, s.swarm.Conns()))
}

// Connections returns a slice of all connections.
func (s *Swarm) Connections() []*Conn {
	return wrapConns(s.swarm.Conns())
}

// CloseConnection removes a given peer from swarm + closes the connection
func (s *Swarm) CloseConnection(p peer.ID) error {
	conns := s.swarm.ConnsWithGroup(p) // boom.
	for _, c := range conns {
		c.Close()
	}
	return nil
}

// Peers returns a copy of the set of peers swarm is connected to.
func (s *Swarm) Peers() []peer.ID {
	conns := s.Connections()

	seen := make(map[peer.ID]struct{})
	peers := make([]peer.ID, 0, len(conns))
	for _, c := range conns {
		p := c.RemotePeer()
		if _, found := seen[p]; found {
			continue
		}

237
		seen[p] = struct{}{}
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
238
239
240
241
242
243
244
245
246
		peers = append(peers, p)
	}
	return peers
}

// LocalPeer returns the local peer swarm is associated to.
func (s *Swarm) LocalPeer() peer.ID {
	return s.local
}
247

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
248
249
250
251
252
253
254
255
256
// notifyAll sends a signal to all Notifiees
func (s *Swarm) notifyAll(notify func(inet.Notifiee)) {
	s.notifmu.RLock()
	for f := range s.notifs {
		go notify(f)
	}
	s.notifmu.RUnlock()
}

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
// Notify signs up Notifiee to receive signals when events happen
func (s *Swarm) Notify(f inet.Notifiee) {
	// wrap with our notifiee, to translate function calls
	n := &ps2netNotifee{net: (*Network)(s), not: f}

	s.notifmu.Lock()
	s.notifs[f] = n
	s.notifmu.Unlock()

	// register for notifications in the peer swarm.
	s.swarm.Notify(n)
}

// StopNotify unregisters Notifiee fromr receiving signals
func (s *Swarm) StopNotify(f inet.Notifiee) {
	s.notifmu.Lock()
	n, found := s.notifs[f]
	if found {
		delete(s.notifs, f)
	}
	s.notifmu.Unlock()

	if found {
		s.swarm.StopNotify(n)
	}
}

type ps2netNotifee struct {
	net *Network
	not inet.Notifiee
}

func (n *ps2netNotifee) Connected(c *ps.Conn) {
	n.not.Connected(n.net, inet.Conn((*Conn)(c)))
}

func (n *ps2netNotifee) Disconnected(c *ps.Conn) {
	n.not.Disconnected(n.net, inet.Conn((*Conn)(c)))
}

func (n *ps2netNotifee) OpenedStream(s *ps.Stream) {
	n.not.OpenedStream(n.net, inet.Stream((*Stream)(s)))
}

func (n *ps2netNotifee) ClosedStream(s *ps.Stream) {
	n.not.ClosedStream(n.net, inet.Stream((*Stream)(s)))
}
304
305
306
307

type metricsNotifiee Swarm

func (nn *metricsNotifiee) Connected(n inet.Network, v inet.Conn) {
Lars Gierth's avatar
Lars Gierth committed
308
	peersTotalGauge(n.LocalPeer()).Set(float64(len(n.Conns())))
309
310
311
}

func (nn *metricsNotifiee) Disconnected(n inet.Network, v inet.Conn) {
Lars Gierth's avatar
Lars Gierth committed
312
	peersTotalGauge(n.LocalPeer()).Set(float64(len(n.Conns())))
313
314
315
316
317
318
319
320
321
322
}

func (nn *metricsNotifiee) OpenedStream(n inet.Network, v inet.Stream) {}
func (nn *metricsNotifiee) ClosedStream(n inet.Network, v inet.Stream) {}
func (nn *metricsNotifiee) Listen(n inet.Network, a ma.Multiaddr)      {}
func (nn *metricsNotifiee) ListenClose(n inet.Network, a ma.Multiaddr) {}

func peersTotalGauge(id peer.ID) prom.Gauge {
	return peersTotal.With(prom.Labels{"peer_id": id.Pretty()})
}