From 3b8eed8a52289dfc87219b5507442e410494c0d8 Mon Sep 17 00:00:00 2001 From: Jeromy Date: Thu, 14 Sep 2017 13:43:37 -0700 Subject: [PATCH] Integrate connection manager License: MIT Signed-off-by: Jeromy --- core/core.go | 30 +++++++++++++++++++++++++++++- docs/config.md | 16 +++++++++++++++- package.json | 6 ++++++ repo/config/swarm.go | 10 ++++++++++ 4 files changed, 60 insertions(+), 2 deletions(-) diff --git a/core/core.go b/core/core.go index 24a34d285ba..760ed7bfb17 100644 --- a/core/core.go +++ b/core/core.go @@ -45,6 +45,7 @@ import ( yamux "gx/ipfs/QmNWCEvi7bPRcvqAV8AKLGVNoQdArWi7NJayka2SM4XtRe/go-smux-yamux" cid "gx/ipfs/QmNp85zy9RLrQ5oQD4hPyS39ezrrXpcaa7R4Y9kxdWQLLQ/go-cid" mplex "gx/ipfs/QmP81tTizXSjKTLWhjty1rabPQHe1YPMj6Bq5gR5fWZakn/go-smux-multiplex" + connmgr "gx/ipfs/QmPErLx83hgF5XKqjeYaLWXJSFon4mH7YPzoftUvfrPgQG/go-libp2p-connmgr" floodsub "gx/ipfs/QmPGT8xqmDnbCbc5HCpjSCzPw7HcsSKuzVAWGjAtF3oftm/floodsub" routing "gx/ipfs/QmPR2JzfKd9poHx9XBhzoFeBBC31ZM3W5iUPKJZWyaoZZm/go-libp2p-routing" pstore "gx/ipfs/QmPgDWmTmuzvP7QE5zwo1TmjbJme9pmZHNujB2453jkCTr/go-libp2p-peerstore" @@ -64,6 +65,7 @@ import ( peer "gx/ipfs/QmXYjuNuxVzXKJCfWasQk1RqkhVLDM9jtUKhqc2WPQmFSB/go-libp2p-peer" smux "gx/ipfs/QmY9JXR3FupnYAYJWK9aMr9bCpqWKcToQ1tz8DVGTrHpHw/go-stream-muxer" dht "gx/ipfs/QmYi2NvTAiv2xTNJNcnuz3iXDDT1ViBwLFXmDb2g7NogAD/go-libp2p-kad-dht" + ifconnmgr "gx/ipfs/QmYkCrTwivapqdB3JbwvwvxymseahVkcm46ThRMAA24zCr/go-libp2p-interface-connmgr" ic "gx/ipfs/QmaPbCnUMBohSGo3KnxEa2bHqyJVVeEEcwtqJAYxerieBo/go-libp2p-crypto" discovery "gx/ipfs/Qmbgce14YTWE2qhE49JVvTBPaHTyz3FaFmqQPyuZAz6C28/go-libp2p/p2p/discovery" p2pbhost "gx/ipfs/Qmbgce14YTWE2qhE49JVvTBPaHTyz3FaFmqQPyuZAz6C28/go-libp2p/p2p/host/basic" @@ -219,11 +221,17 @@ func (n *IpfsNode) startOnlineServices(ctx context.Context, routingOption Routin return err } + connmgr, err := constructConnMgr(cfg.Swarm.ConnMgr) + if err != nil { + return err + } + hostopts := &ConstructPeerHostOpts{ AddrsFactory: addrsFactory, DisableNatPortMap: cfg.Swarm.DisableNatPortMap, DisableRelay: cfg.Swarm.DisableRelay, EnableRelayHop: cfg.Swarm.EnableRelayHop, + ConnectionManager: connmgr, } peerhost, err := hostOption(ctx, n.Identity, n.Peerstore, n.Reporter, addrfilter, tpt, protec, hostopts) @@ -261,6 +269,22 @@ func (n *IpfsNode) startOnlineServices(ctx context.Context, routingOption Routin return n.Bootstrap(DefaultBootstrapConfig) } +func constructConnMgr(cfg config.ConnMgr) (ifconnmgr.ConnManager, error) { + switch cfg.Type { + case "", "none": + return nil, nil + case "basic": + grace, err := time.ParseDuration(cfg.GracePeriod) + if err != nil { + return nil, fmt.Errorf("parsing Swarm.ConnMgr.GracePeriod: %s", err) + } + + return connmgr.NewConnManager(cfg.LowWater, cfg.HighWater, grace), nil + default: + return nil, fmt.Errorf("unrecognized ConnMgr.Type: %q", cfg.Type) + } +} + func (n *IpfsNode) startLateOnlineServices(ctx context.Context) error { cfg, err := n.Repo.Config() if err != nil { @@ -380,7 +404,7 @@ func setupDiscoveryOption(d config.Discovery) DiscoveryOption { if d.MDNS.Interval == 0 { d.MDNS.Interval = 5 } - return discovery.NewMdnsService(ctx, h, time.Duration(d.MDNS.Interval)*time.Second) + return discovery.NewMdnsService(ctx, h, time.Duration(d.MDNS.Interval)*time.Second, discovery.ServiceTag) } } return nil @@ -789,6 +813,7 @@ type ConstructPeerHostOpts struct { DisableNatPortMap bool DisableRelay bool EnableRelayHop bool + ConnectionManager ifconnmgr.ConnManager } type HostOption func(ctx context.Context, id peer.ID, ps pstore.Peerstore, bwr metrics.Reporter, fs []*net.IPNet, tpt smux.Transport, protc ipnet.Protector, opts *ConstructPeerHostOpts) (p2phost.Host, error) @@ -814,6 +839,9 @@ func constructPeerHost(ctx context.Context, id peer.ID, ps pstore.Peerstore, bwr if !opts.DisableNatPortMap { hostOpts = append(hostOpts, p2pbhost.NATPortMap) } + if opts.ConnectionManager != nil { + hostOpts = append(hostOpts, opts.ConnectionManager) + } addrsFactory := opts.AddrsFactory if !opts.DisableRelay { diff --git a/docs/config.md b/docs/config.md index cd9e7a0168b..fc5c3d66db4 100644 --- a/docs/config.md +++ b/docs/config.md @@ -250,7 +250,7 @@ Options for configuring the swarm. - `AddrFilters` An array of address filters (multiaddr netmasks) to filter dials to. -See https://github.com/ipfs/go-ipfs/issues/1226#issuecomment-120494604 for more +See [this issue](https://github.com/ipfs/go-ipfs/issues/1226#issuecomment-120494604) for more information. - `DisableBandwidthMetrics` @@ -267,3 +267,17 @@ Disables the p2p-circuit relay transport. - `EnableRelayHop` Enables HOP relay for the node. If this is enabled, the node will act as an intermediate (Hop Relay) node in relay circuits for connected peers. + +### `ConnMgr` +Connection manager configuration. + +- `Type` +Sets the type of connection manager to use, options are: `"none"` and `"basic"`. + +- `LowWater` +LowWater is the minimum number of connections to maintain. + +- `HighWater` +HighWater is the number of connections that, when exceeded, will trigger a connection GC operation. +- `GracePeriod` +GracePeriod is a time duration that new connections are immune from being closed by the connection manager. diff --git a/package.json b/package.json index 31dd4392ef1..49ae9f890d6 100644 --- a/package.json +++ b/package.json @@ -469,6 +469,12 @@ "hash": "QmWRCn8vruNAzHx8i6SAXinuheRitKEGu8c7m26stKvsYx", "name": "go-testutil", "version": "1.1.11" + }, + { + "author": "whyrusleeping", + "hash": "QmPErLx83hgF5XKqjeYaLWXJSFon4mH7YPzoftUvfrPgQG", + "name": "go-libp2p-connmgr", + "version": "0.3.2" } ], "gxVersion": "0.10.0", diff --git a/repo/config/swarm.go b/repo/config/swarm.go index 25c35d58521..5b128f38aab 100644 --- a/repo/config/swarm.go +++ b/repo/config/swarm.go @@ -6,4 +6,14 @@ type SwarmConfig struct { DisableNatPortMap bool DisableRelay bool EnableRelayHop bool + + ConnMgr ConnMgr +} + +// ConnMgr defines configuration options for the libp2p connection manager +type ConnMgr struct { + Type string + LowWater int + HighWater int + GracePeriod string }