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 13beb2e
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 5 deletions.
77 changes: 75 additions & 2 deletions core/commands/swarm.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,13 @@ import (
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 Down Expand Up @@ -812,3 +814,74 @@ 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
infos["connCount"] = inf.ConnCount
default:
infos["type"] = "unknown"
}

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

buf := new(bytes.Buffer)

switch infos["type"] {
case "basic":
fmt.Fprintf(buf, "Type: basic\n")
fmt.Fprintf(buf, "Low Water: %d\n", infos["lowWater"])
fmt.Fprintf(buf, "High Water: %d\n", infos["highWater"])
fmt.Fprintf(buf, "Grace Period: %d\n", infos["gracePeriod"])
fmt.Fprintf(buf, "Connection Count: %d\n", infos["connCount"])
fmt.Fprintf(buf, "Last Trim: %s\n", infos["lastTrim"])
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

0 comments on commit 13beb2e

Please sign in to comment.