Skip to content

Commit

Permalink
Integrate connection manager
Browse files Browse the repository at this point in the history
License: MIT
Signed-off-by: Jeromy <jeromyj@gmail.com>
  • Loading branch information
whyrusleeping committed Oct 9, 2017
1 parent 48654d3 commit a5367a3
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 2 deletions.
30 changes: 29 additions & 1 deletion core/core.go
Expand Up @@ -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"
Expand All @@ -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"
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand All @@ -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 {
Expand Down
17 changes: 16 additions & 1 deletion docs/config.md
Expand Up @@ -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`
Expand All @@ -267,3 +267,18 @@ 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 the length of time that new connections are immune from being closed by the connection manager.

6 changes: 6 additions & 0 deletions package.json
Expand Up @@ -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",
Expand Down
10 changes: 10 additions & 0 deletions repo/config/swarm.go
Expand Up @@ -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
}

0 comments on commit a5367a3

Please sign in to comment.