Skip to content

Commit

Permalink
add a subcommand for the connmgr
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 25, 2017
1 parent c0d6224 commit 67e0293
Show file tree
Hide file tree
Showing 4 changed files with 132 additions and 5 deletions.
88 changes: 86 additions & 2 deletions core/commands/swarm.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,20 @@ import (
"path"
"sort"
"strings"
"time"

cmds "github.com/ipfs/go-ipfs/commands"
repo "github.com/ipfs/go-ipfs/repo"
config "github.com/ipfs/go-ipfs/repo/config"
"github.com/ipfs/go-ipfs/repo/fsrepo"
iaddr "github.com/ipfs/go-ipfs/thirdparty/ipfsaddr"
pstore "gx/ipfs/QmPgDWmTmuzvP7QE5zwo1TmjbJme9pmZHNujB2453jkCTr/go-libp2p-peerstore"
swarm "gx/ipfs/QmdQFrFnPrKRQtpeHKjZ3cVNwxmGKKS2TvhJTuN9C9yduh/go-libp2p-swarm"

pstore "gx/ipfs/QmPgDWmTmuzvP7QE5zwo1TmjbJme9pmZHNujB2453jkCTr/go-libp2p-peerstore"
mafilter "gx/ipfs/QmSMZwvs3n4GBikZ7hKzT17c3bk65FmyZo2JqtJ16swqCv/multiaddr-filter"
connmgr "gx/ipfs/QmUbUNq1Q6eE2LXqKzKW8BW1SUMqscAj9A3ot9j5suuaRb/go-libp2p-connmgr"
ma "gx/ipfs/QmXY77cVe7rVRQXZZQRioukUM7aRW3BTcAgJe12MCtb3Ji/go-multiaddr"
ifconnmgr "gx/ipfs/QmYkCrTwivapqdB3JbwvwvxymseahVkcm46ThRMAA24zCr/go-libp2p-interface-connmgr"
swarm "gx/ipfs/QmdQFrFnPrKRQtpeHKjZ3cVNwxmGKKS2TvhJTuN9C9yduh/go-libp2p-swarm"
)

type stringList struct {
Expand All @@ -44,6 +47,7 @@ ipfs peers in the internet.
"disconnect": swarmDisconnectCmd,
"filters": swarmFiltersCmd,
"peers": swarmPeersCmd,
"connmgr": swarmConnMgrCmd,
},
}

Expand Down Expand Up @@ -812,3 +816,83 @@ func filtersRemove(r repo.Repo, cfg *config.Config, toRemoveFilters []string) ([

return removed, nil
}

var swarmConnMgrCmd = &cmds.Command{
Helptext: cmds.HelpText{
Tagline: "Interact with the Connection Manager.",
ShortDescription: `
Display information about the current state of the connection manager.
`,
},
Run: func(req cmds.Request, res cmds.Response) {
n, err := req.InvocContext().GetNode()
if err != nil {
res.SetError(err, cmds.ErrNormal)
return
}

if n.PeerHost == nil {
res.SetError(errNotOnline, cmds.ErrClient)
return
}

infos := make(map[string]interface{})
cmgr := n.PeerHost.ConnManager()
switch cmgr := cmgr.(type) {
case nil:
infos["type"] = "<nil>"
case *ifconnmgr.NullConnMgr:
infos["type"] = "disabled"
case *connmgr.BasicConnMgr:
infos["type"] = "basic"

inf := cmgr.GetInfo()
infos["lowWater"] = inf.LowWater
infos["highWater"] = inf.HighWater
infos["lastTrim"] = inf.LastTrim
infos["gracePeriod"] = inf.GracePeriod.String()
infos["connCount"] = inf.ConnCount
default:
infos["type"] = "unknown"
}

res.SetOutput(infos)
},
Marshalers: cmds.MarshalerMap{
cmds.Text: func(res cmds.Response) (io.Reader, error) {
infosp, ok := res.Output().(*map[string]interface{})
if !ok {
return nil, fmt.Errorf("expected output type to be a map: %#v", res.Output())
}
infos := *infosp

buf := new(bytes.Buffer)

switch infos["type"] {
case "basic":
tstr := "<n/a>"
t, err := time.Parse(time.RFC3339Nano, infos["lastTrim"].(string))
if err != nil {
tstr = "<err>"
} else if !t.IsZero() {
tstr = t.String()
}

fmt.Fprintf(buf, "Type: basic\n")
fmt.Fprintf(buf, "Low Water: %d\n", int(infos["lowWater"].(float64)))
fmt.Fprintf(buf, "High Water: %d\n", int(infos["highWater"].(float64)))
fmt.Fprintf(buf, "Grace Period: %s\n", infos["gracePeriod"])
fmt.Fprintf(buf, "Connection Count: %d\n", int(infos["connCount"].(float64)))
fmt.Fprintf(buf, "Last Trim: %s\n", tstr)
case "none":
fmt.Println("Connection Manager Disabled")
default:
log.Errorf("unknown connection manager type: %s", infos["type"])
fmt.Println("Unknown Connection Manager Settings")
}

return buf, nil
},
},
Type: make(map[string]interface{}),
}
2 changes: 1 addition & 1 deletion core/core.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@ 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"
routing "gx/ipfs/QmPR2JzfKd9poHx9XBhzoFeBBC31ZM3W5iUPKJZWyaoZZm/go-libp2p-routing"
pstore "gx/ipfs/QmPgDWmTmuzvP7QE5zwo1TmjbJme9pmZHNujB2453jkCTr/go-libp2p-peerstore"
mafilter "gx/ipfs/QmQBB2dQLmQHJgs2gqZ3iqL2XiuCtUCvXzWt5kMXDf5Zcr/go-maddr-filter"
Expand All @@ -57,6 +56,7 @@ import (
logging "gx/ipfs/QmSpJByNKFX1sCsHBEp3R73FL4NF6FnQTEGyNAXHm2GS52/go-log"
b58 "gx/ipfs/QmT8rehPR3F6bmwL6zjUN8XpiDBFFpMP2myPdC6ApsWfJf/go-base58"
floodsub "gx/ipfs/QmUUSLfvihARhCxxgnjW4hmycJpPvzNu12Aaz6JWVdfnLg/go-libp2p-floodsub"
connmgr "gx/ipfs/QmUbUNq1Q6eE2LXqKzKW8BW1SUMqscAj9A3ot9j5suuaRb/go-libp2p-connmgr"
addrutil "gx/ipfs/QmVJGsPeK3vwtEyyTxpCs47yjBYMmYsAhEouPDF3Gb2eK3/go-addr-util"
ds "gx/ipfs/QmVSase1JP7cq9QkPT46oNwdp9pT6kBkG3oqS14y3QcZjG/go-datastore"
mssmux "gx/ipfs/QmVniQJkdzLZaZwzwMdd3dJTvWiJ1DQEkreVy6hs6h7Vk5/go-smux-multistream"
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -472,9 +472,9 @@
},
{
"author": "whyrusleeping",
"hash": "QmPErLx83hgF5XKqjeYaLWXJSFon4mH7YPzoftUvfrPgQG",
"hash": "QmUbUNq1Q6eE2LXqKzKW8BW1SUMqscAj9A3ot9j5suuaRb",
"name": "go-libp2p-connmgr",
"version": "0.3.2"
"version": "0.3.3"
},
{
"author": "why",
Expand Down
43 changes: 43 additions & 0 deletions test/sharness/t0140-swarm.sh
Original file line number Diff line number Diff line change
Expand Up @@ -97,4 +97,47 @@ test_expect_success "Addresses.NoAnnounce with /ipcidr affects addresses" '

test_kill_ipfs_daemon

test_launch_ipfs_daemon

test_expect_success "ipfs swarm connmgr shows default settings" '
ipfs swarm connmgr > connmgr_out &&
grep "Low Water: 600" connmgr_out &&
grep "High Water: 900" connmgr_out &&
grep "Type: basic" connmgr_out
'

test_kill_ipfs_daemon

test_expect_success "clear out connmgr config" '
ipfs config --json Swarm.ConnMgr.HighWater 1500
ipfs config Swarm.ConnMgr.GracePeriod "3s"
'

test_launch_ipfs_daemon

test_expect_success "ipfs swarm connmgr shows modified settings" '
ipfs swarm connmgr > connmgr_out &&
grep "Low Water: 600" connmgr_out &&
grep "Grace Period: 3s" connmgr_out &&
grep "High Water: 1500" connmgr_out &&
grep "Type: basic" connmgr_out
'

test_kill_ipfs_daemon

test_expect_success "clear out connmgr config" '
ipfs config --json Swarm.ConnMgr "{}"
'

test_launch_ipfs_daemon

test_expect_success "ipfs swarm connmgr shows default settings" '
ipfs swarm connmgr > connmgr_out &&
grep "Low Water: 600" connmgr_out &&
grep "High Water: 900" connmgr_out &&
grep "Type: basic" connmgr_out
'

test_kill_ipfs_daemon

test_done

0 comments on commit 67e0293

Please sign in to comment.