Integrate connection manager
License: MIT Signed-off-by: Jeromy <jeromyj@gmail.com>
Jeromy committed
Sep 14, 2017 at 13:43 UTC
62d521783fb9f7692eac8f11aed5b34106c0e0b3
4 files changed
+60
-2
core/core.go
+29
-1
@@ -45,6 +45,7 @@ import (
45
yamux "gx/ipfs/QmNWCEvi7bPRcvqAV8AKLGVNoQdArWi7NJayka2SM4XtRe/go-smux-yamux"
46
cid "gx/ipfs/QmNp85zy9RLrQ5oQD4hPyS39ezrrXpcaa7R4Y9kxdWQLLQ/go-cid"
47
mplex "gx/ipfs/QmP81tTizXSjKTLWhjty1rabPQHe1YPMj6Bq5gR5fWZakn/go-smux-multiplex"
48
+ connmgr "gx/ipfs/QmPErLx83hgF5XKqjeYaLWXJSFon4mH7YPzoftUvfrPgQG/go-libp2p-connmgr"
49
floodsub "gx/ipfs/QmPGT8xqmDnbCbc5HCpjSCzPw7HcsSKuzVAWGjAtF3oftm/floodsub"
50
routing "gx/ipfs/QmPR2JzfKd9poHx9XBhzoFeBBC31ZM3W5iUPKJZWyaoZZm/go-libp2p-routing"
51
pstore "gx/ipfs/QmPgDWmTmuzvP7QE5zwo1TmjbJme9pmZHNujB2453jkCTr/go-libp2p-peerstore"
@@ -64,6 +65,7 @@ import (
65
peer "gx/ipfs/QmXYjuNuxVzXKJCfWasQk1RqkhVLDM9jtUKhqc2WPQmFSB/go-libp2p-peer"
66
smux "gx/ipfs/QmY9JXR3FupnYAYJWK9aMr9bCpqWKcToQ1tz8DVGTrHpHw/go-stream-muxer"
67
dht "gx/ipfs/QmYi2NvTAiv2xTNJNcnuz3iXDDT1ViBwLFXmDb2g7NogAD/go-libp2p-kad-dht"
68
+ ifconnmgr "gx/ipfs/QmYkCrTwivapqdB3JbwvwvxymseahVkcm46ThRMAA24zCr/go-libp2p-interface-connmgr"
69
ic "gx/ipfs/QmaPbCnUMBohSGo3KnxEa2bHqyJVVeEEcwtqJAYxerieBo/go-libp2p-crypto"
70
discovery "gx/ipfs/Qmbgce14YTWE2qhE49JVvTBPaHTyz3FaFmqQPyuZAz6C28/go-libp2p/p2p/discovery"
71
p2pbhost "gx/ipfs/Qmbgce14YTWE2qhE49JVvTBPaHTyz3FaFmqQPyuZAz6C28/go-libp2p/p2p/host/basic"
@@ -219,11 +221,17 @@ func (n *IpfsNode) startOnlineServices(ctx context.Context, routingOption Routin
221
return err
222
}
223
224
+ connmgr, err := constructConnMgr(cfg.Swarm.ConnMgr)
225
+ if err != nil {
226
+ return err
227
+ }
228
+
229
hostopts := &ConstructPeerHostOpts{
230
AddrsFactory: addrsFactory,
231
DisableNatPortMap: cfg.Swarm.DisableNatPortMap,
232
DisableRelay: cfg.Swarm.DisableRelay,
233
EnableRelayHop: cfg.Swarm.EnableRelayHop,
234
+ ConnectionManager: connmgr,
235
}
236
peerhost, err := hostOption(ctx, n.Identity, n.Peerstore, n.Reporter,
237
addrfilter, tpt, protec, hostopts)
@@ -261,6 +269,22 @@ func (n *IpfsNode) startOnlineServices(ctx context.Context, routingOption Routin
269
return n.Bootstrap(DefaultBootstrapConfig)
270
}
271
272
+func constructConnMgr(cfg config.ConnMgr) (ifconnmgr.ConnManager, error) {
273
+ switch cfg.Type {
274
+ case "", "none":
275
+ return nil, nil
276
+ case "basic":
277
+ grace, err := time.ParseDuration(cfg.GracePeriod)
278
+ if err != nil {
279
+ return nil, fmt.Errorf("parsing Swarm.ConnMgr.GracePeriod: %s", err)
280
+ }
281
+
282
+ return connmgr.NewConnManager(cfg.LowWater, cfg.HighWater, grace), nil
283
+ default:
284
+ return nil, fmt.Errorf("unrecognized ConnMgr.Type: %q", cfg.Type)
285
+ }
286
+}
287
+
288
func (n *IpfsNode) startLateOnlineServices(ctx context.Context) error {
289
cfg, err := n.Repo.Config()
290
if err != nil {
@@ -380,7 +404,7 @@ func setupDiscoveryOption(d config.Discovery) DiscoveryOption {
404
if d.MDNS.Interval == 0 {
405
d.MDNS.Interval = 5
406
}
383
- return discovery.NewMdnsService(ctx, h, time.Duration(d.MDNS.Interval)*time.Second)
407
+ return discovery.NewMdnsService(ctx, h, time.Duration(d.MDNS.Interval)*time.Second, discovery.ServiceTag)
408
}
409
}
410
return nil
@@ -789,6 +813,7 @@ type ConstructPeerHostOpts struct {
813
DisableNatPortMap bool
814
DisableRelay bool
815
EnableRelayHop bool
816
+ ConnectionManager ifconnmgr.ConnManager
817
}
818
819
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
839
if !opts.DisableNatPortMap {
840
hostOpts = append(hostOpts, p2pbhost.NATPortMap)
841
}
842
+ if opts.ConnectionManager != nil {
843
+ hostOpts = append(hostOpts, opts.ConnectionManager)
844
+ }
845
846
addrsFactory := opts.AddrsFactory
847
if !opts.DisableRelay {
docs/config.md
+15
-1
@@ -250,7 +250,7 @@ Options for configuring the swarm.
250
251
- `AddrFilters`
252
An array of address filters (multiaddr netmasks) to filter dials to.
253
-See https://github.com/ipfs/go-ipfs/issues/1226#issuecomment-120494604 for more
253
+See [this issue](https://github.com/ipfs/go-ipfs/issues/1226#issuecomment-120494604) for more
254
information.
255
256
- `DisableBandwidthMetrics`
@@ -267,3 +267,17 @@ Disables the p2p-circuit relay transport.
267
- `EnableRelayHop`
268
Enables HOP relay for the node. If this is enabled, the node will act as
269
an intermediate (Hop Relay) node in relay circuits for connected peers.
270
+
271
+### `ConnMgr`
272
+Connection manager configuration.
273
+
274
+- `Type`
275
+Sets the type of connection manager to use, options are: `"none"` and `"basic"`.
276
+
277
+- `LowWater`
278
+LowWater is the minimum number of connections to maintain.
279
+
280
+- `HighWater`
281
+HighWater is the number of connections that, when exceeded, will trigger a connection GC operation.
282
+- `GracePeriod`
283
+GracePeriod is a time duration that new connections are immune from being closed by the connection manager.
package.json
+6
@@ -469,6 +469,12 @@
469
"hash": "QmWRCn8vruNAzHx8i6SAXinuheRitKEGu8c7m26stKvsYx",
470
"name": "go-testutil",
471
"version": "1.1.11"
472
+ },
473
+ {
474
+ "author": "whyrusleeping",
475
+ "hash": "QmPErLx83hgF5XKqjeYaLWXJSFon4mH7YPzoftUvfrPgQG",
476
+ "name": "go-libp2p-connmgr",
477
+ "version": "0.3.2"
478
}
479
],
480
"gxVersion": "0.10.0",
repo/config/swarm.go
+10
@@ -6,4 +6,14 @@ type SwarmConfig struct {
6
DisableNatPortMap bool
7
DisableRelay bool
8
EnableRelayHop bool
9
+
10
+ ConnMgr ConnMgr
11
+}
12
+
13
+// ConnMgr defines configuration options for the libp2p connection manager
14
+type ConnMgr struct {
15
+ Type string
16
+ LowWater int
17
+ HighWater int
18
+ GracePeriod string
19
}