From 67e029318d8ebe811878ae8a68064392c308b7b8 Mon Sep 17 00:00:00 2001 From: Jeromy Date: Mon, 16 Oct 2017 09:22:02 -0700 Subject: [PATCH] add a subcommand for the connmgr License: MIT Signed-off-by: Jeromy --- core/commands/swarm.go | 88 +++++++++++++++++++++++++++++++++++- core/core.go | 2 +- package.json | 4 +- test/sharness/t0140-swarm.sh | 43 ++++++++++++++++++ 4 files changed, 132 insertions(+), 5 deletions(-) diff --git a/core/commands/swarm.go b/core/commands/swarm.go index 915695bd8dd1..1b6f8e21d80d 100644 --- a/core/commands/swarm.go +++ b/core/commands/swarm.go @@ -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 { @@ -44,6 +47,7 @@ ipfs peers in the internet. "disconnect": swarmDisconnectCmd, "filters": swarmFiltersCmd, "peers": swarmPeersCmd, + "connmgr": swarmConnMgrCmd, }, } @@ -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"] = "" + 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 := "" + t, err := time.Parse(time.RFC3339Nano, infos["lastTrim"].(string)) + if err != nil { + tstr = "" + } 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{}), +} diff --git a/core/core.go b/core/core.go index 4f27c447e978..35f46a8e6da1 100644 --- a/core/core.go +++ b/core/core.go @@ -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" @@ -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" diff --git a/package.json b/package.json index 335f1944a901..48e16394fde0 100644 --- a/package.json +++ b/package.json @@ -472,9 +472,9 @@ }, { "author": "whyrusleeping", - "hash": "QmPErLx83hgF5XKqjeYaLWXJSFon4mH7YPzoftUvfrPgQG", + "hash": "QmUbUNq1Q6eE2LXqKzKW8BW1SUMqscAj9A3ot9j5suuaRb", "name": "go-libp2p-connmgr", - "version": "0.3.2" + "version": "0.3.3" }, { "author": "why", diff --git a/test/sharness/t0140-swarm.sh b/test/sharness/t0140-swarm.sh index d4d5a96a5c46..2ffc2d5e16d8 100755 --- a/test/sharness/t0140-swarm.sh +++ b/test/sharness/t0140-swarm.sh @@ -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