Unverified Commit ddd5604b authored by vyzo's avatar vyzo Committed by GitHub
Browse files

Merge pull request #458 from libp2p/feat/ping

Add ping service in basic host and libp2p constructor option (enabled by default)
parents 4dc78663 48e0effd
...@@ -56,6 +56,9 @@ type Config struct { ...@@ -56,6 +56,9 @@ type Config struct {
NATManager NATManagerC NATManager NATManagerC
Peerstore pstore.Peerstore Peerstore pstore.Peerstore
Reporter metrics.Reporter Reporter metrics.Reporter
PingCustom bool
Ping bool
} }
// NewNode constructs a new libp2p Host from the Config. // NewNode constructs a new libp2p Host from the Config.
...@@ -102,6 +105,7 @@ func (cfg *Config) NewNode(ctx context.Context) (host.Host, error) { ...@@ -102,6 +105,7 @@ func (cfg *Config) NewNode(ctx context.Context) (host.Host, error) {
ConnManager: cfg.ConnManager, ConnManager: cfg.ConnManager,
AddrsFactory: cfg.AddrsFactory, AddrsFactory: cfg.AddrsFactory,
NATManager: cfg.NATManager, NATManager: cfg.NATManager,
EnablePing: cfg.Ping,
}) })
if err != nil { if err != nil {
swrm.Close() swrm.Close()
......
...@@ -75,6 +75,11 @@ var DefaultEnableRelay = func(cfg *Config) error { ...@@ -75,6 +75,11 @@ var DefaultEnableRelay = func(cfg *Config) error {
return cfg.Apply(EnableRelay()) return cfg.Apply(EnableRelay())
} }
// DefaultEnablePing enables the ping service by default
var DefaultEnablePing = func(cfg *Config) error {
return cfg.Apply(Ping(true))
}
// Complete list of default options and when to fallback on them. // Complete list of default options and when to fallback on them.
// //
// Please *DON'T* specify default options any other way. Putting this all here // Please *DON'T* specify default options any other way. Putting this all here
...@@ -111,6 +116,10 @@ var defaults = []struct { ...@@ -111,6 +116,10 @@ var defaults = []struct {
fallback: func(cfg *Config) bool { return !cfg.RelayCustom }, fallback: func(cfg *Config) bool { return !cfg.RelayCustom },
opt: DefaultEnableRelay, opt: DefaultEnableRelay,
}, },
{
fallback: func(cfg *Config) bool { return !cfg.PingCustom },
opt: DefaultEnablePing,
},
} }
// Defaults configures libp2p to use the default options. Can be combined with // Defaults configures libp2p to use the default options. Can be combined with
......
...@@ -252,6 +252,15 @@ func NATManager(nm config.NATManagerC) Option { ...@@ -252,6 +252,15 @@ func NATManager(nm config.NATManagerC) Option {
} }
} }
// Ping will configure libp2p to support the ping service; enable by default.
func Ping(enable bool) Option {
return func(cfg *Config) error {
cfg.PingCustom = true
cfg.Ping = enable
return nil
}
}
// NoListenAddrs will configure libp2p to not listen by default. // NoListenAddrs will configure libp2p to not listen by default.
// //
// This will both clear any configured listen addrs and prevent libp2p from // This will both clear any configured listen addrs and prevent libp2p from
......
...@@ -14,6 +14,7 @@ import ( ...@@ -14,6 +14,7 @@ import (
pstore "github.com/libp2p/go-libp2p-peerstore" pstore "github.com/libp2p/go-libp2p-peerstore"
protocol "github.com/libp2p/go-libp2p-protocol" protocol "github.com/libp2p/go-libp2p-protocol"
identify "github.com/libp2p/go-libp2p/p2p/protocol/identify" identify "github.com/libp2p/go-libp2p/p2p/protocol/identify"
ping "github.com/libp2p/go-libp2p/p2p/protocol/ping"
ma "github.com/multiformats/go-multiaddr" ma "github.com/multiformats/go-multiaddr"
madns "github.com/multiformats/go-multiaddr-dns" madns "github.com/multiformats/go-multiaddr-dns"
msmux "github.com/multiformats/go-multistream" msmux "github.com/multiformats/go-multistream"
...@@ -56,6 +57,7 @@ type BasicHost struct { ...@@ -56,6 +57,7 @@ type BasicHost struct {
network inet.Network network inet.Network
mux *msmux.MultistreamMuxer mux *msmux.MultistreamMuxer
ids *identify.IDService ids *identify.IDService
pings *ping.PingService
natmgr NATManager natmgr NATManager
addrs AddrsFactory addrs AddrsFactory
maResolver *madns.Resolver maResolver *madns.Resolver
...@@ -96,6 +98,9 @@ type HostOpts struct { ...@@ -96,6 +98,9 @@ type HostOpts struct {
// ConnManager is a libp2p connection manager // ConnManager is a libp2p connection manager
ConnManager ifconnmgr.ConnManager ConnManager ifconnmgr.ConnManager
// EnablePing indicates whether to instantiate the ping service
EnablePing bool
} }
// NewHost constructs a new *BasicHost and activates it by attaching its stream and connection handlers to the given inet.Network. // NewHost constructs a new *BasicHost and activates it by attaching its stream and connection handlers to the given inet.Network.
...@@ -149,6 +154,10 @@ func NewHost(ctx context.Context, net inet.Network, opts *HostOpts) (*BasicHost, ...@@ -149,6 +154,10 @@ func NewHost(ctx context.Context, net inet.Network, opts *HostOpts) (*BasicHost,
net.Notify(h.cmgr.Notifee()) net.Notify(h.cmgr.Notifee())
} }
if opts.EnablePing {
h.pings = ping.NewPingService(h)
}
net.SetConnHandler(h.newConnHandler) net.SetConnHandler(h.newConnHandler)
net.SetStreamHandler(h.newStreamHandler) net.SetStreamHandler(h.newStreamHandler)
return h, nil return h, nil
......
...@@ -72,7 +72,11 @@ func (p *PingService) PingHandler(s inet.Stream) { ...@@ -72,7 +72,11 @@ func (p *PingService) PingHandler(s inet.Stream) {
} }
func (ps *PingService) Ping(ctx context.Context, p peer.ID) (<-chan time.Duration, error) { func (ps *PingService) Ping(ctx context.Context, p peer.ID) (<-chan time.Duration, error) {
s, err := ps.Host.NewStream(ctx, p, ID) return Ping(ctx, ps.Host, p)
}
func Ping(ctx context.Context, h host.Host, p peer.ID) (<-chan time.Duration, error) {
s, err := h.NewStream(ctx, p, ID)
if err != nil { if err != nil {
return nil, err return nil, err
} }
...@@ -92,7 +96,7 @@ func (ps *PingService) Ping(ctx context.Context, p peer.ID) (<-chan time.Duratio ...@@ -92,7 +96,7 @@ func (ps *PingService) Ping(ctx context.Context, p peer.ID) (<-chan time.Duratio
return return
} }
ps.Host.Peerstore().RecordLatency(p, t) h.Peerstore().RecordLatency(p, t)
select { select {
case out <- t: case out <- t:
case <-ctx.Done(): case <-ctx.Done():
......
package ping package ping_test
import ( import (
"context" "context"
...@@ -9,6 +9,7 @@ import ( ...@@ -9,6 +9,7 @@ import (
pstore "github.com/libp2p/go-libp2p-peerstore" pstore "github.com/libp2p/go-libp2p-peerstore"
swarmt "github.com/libp2p/go-libp2p-swarm/testing" swarmt "github.com/libp2p/go-libp2p-swarm/testing"
bhost "github.com/libp2p/go-libp2p/p2p/host/basic" bhost "github.com/libp2p/go-libp2p/p2p/host/basic"
ping "github.com/libp2p/go-libp2p/p2p/protocol/ping"
) )
func TestPing(t *testing.T) { func TestPing(t *testing.T) {
...@@ -26,14 +27,14 @@ func TestPing(t *testing.T) { ...@@ -26,14 +27,14 @@ func TestPing(t *testing.T) {
t.Fatal(err) t.Fatal(err)
} }
ps1 := NewPingService(h1) ps1 := ping.NewPingService(h1)
ps2 := NewPingService(h2) ps2 := ping.NewPingService(h2)
testPing(t, ps1, h2.ID()) testPing(t, ps1, h2.ID())
testPing(t, ps2, h1.ID()) testPing(t, ps2, h1.ID())
} }
func testPing(t *testing.T, ps *PingService, p peer.ID) { func testPing(t *testing.T, ps *ping.PingService, p peer.ID) {
pctx, cancel := context.WithCancel(context.Background()) pctx, cancel := context.WithCancel(context.Background())
defer cancel() defer cancel()
ts, err := ps.Ping(pctx, p) ts, err := ps.Ping(pctx, p)
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment