Skip to content

Commit ff55c8f

Browse files
committed
add a subcommand for the connmgr
License: MIT Signed-off-by: Jeromy <[email protected]>
1 parent 8923fdd commit ff55c8f

File tree

4 files changed

+133
-6
lines changed

4 files changed

+133
-6
lines changed

core/commands/swarm.go

+87-3
Original file line numberDiff line numberDiff line change
@@ -8,17 +8,20 @@ import (
88
"path"
99
"sort"
1010
"strings"
11+
"time"
1112

1213
cmds "github.com/ipfs/go-ipfs/commands"
1314
repo "github.com/ipfs/go-ipfs/repo"
1415
config "github.com/ipfs/go-ipfs/repo/config"
1516
"github.com/ipfs/go-ipfs/repo/fsrepo"
16-
pstore "gx/ipfs/QmPgDWmTmuzvP7QE5zwo1TmjbJme9pmZHNujB2453jkCTr/go-libp2p-peerstore"
17-
swarm "gx/ipfs/QmdQFrFnPrKRQtpeHKjZ3cVNwxmGKKS2TvhJTuN9C9yduh/go-libp2p-swarm"
18-
iaddr "gx/ipfs/QmeS8cCKawUwejVrsBtmC1toTXmwVWZGiRJqzgTURVWeF9/go-ipfs-addr"
1917

18+
pstore "gx/ipfs/QmPgDWmTmuzvP7QE5zwo1TmjbJme9pmZHNujB2453jkCTr/go-libp2p-peerstore"
2019
mafilter "gx/ipfs/QmSMZwvs3n4GBikZ7hKzT17c3bk65FmyZo2JqtJ16swqCv/multiaddr-filter"
20+
connmgr "gx/ipfs/QmUbUNq1Q6eE2LXqKzKW8BW1SUMqscAj9A3ot9j5suuaRb/go-libp2p-connmgr"
2121
ma "gx/ipfs/QmXY77cVe7rVRQXZZQRioukUM7aRW3BTcAgJe12MCtb3Ji/go-multiaddr"
22+
ifconnmgr "gx/ipfs/QmYkCrTwivapqdB3JbwvwvxymseahVkcm46ThRMAA24zCr/go-libp2p-interface-connmgr"
23+
swarm "gx/ipfs/QmdQFrFnPrKRQtpeHKjZ3cVNwxmGKKS2TvhJTuN9C9yduh/go-libp2p-swarm"
24+
iaddr "gx/ipfs/QmeS8cCKawUwejVrsBtmC1toTXmwVWZGiRJqzgTURVWeF9/go-ipfs-addr"
2225
)
2326

2427
type stringList struct {
@@ -44,6 +47,7 @@ ipfs peers in the internet.
4447
"disconnect": swarmDisconnectCmd,
4548
"filters": swarmFiltersCmd,
4649
"peers": swarmPeersCmd,
50+
"connmgr": swarmConnMgrCmd,
4751
},
4852
}
4953

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

813817
return removed, nil
814818
}
819+
820+
var swarmConnMgrCmd = &cmds.Command{
821+
Helptext: cmds.HelpText{
822+
Tagline: "Interact with the Connection Manager.",
823+
ShortDescription: `
824+
Display information about the current state of the connection manager.
825+
`,
826+
},
827+
Run: func(req cmds.Request, res cmds.Response) {
828+
n, err := req.InvocContext().GetNode()
829+
if err != nil {
830+
res.SetError(err, cmds.ErrNormal)
831+
return
832+
}
833+
834+
if n.PeerHost == nil {
835+
res.SetError(errNotOnline, cmds.ErrClient)
836+
return
837+
}
838+
839+
infos := make(map[string]interface{})
840+
cmgr := n.PeerHost.ConnManager()
841+
switch cmgr := cmgr.(type) {
842+
case nil:
843+
infos["type"] = "<nil>"
844+
case *ifconnmgr.NullConnMgr:
845+
infos["type"] = "disabled"
846+
case *connmgr.BasicConnMgr:
847+
infos["type"] = "basic"
848+
849+
inf := cmgr.GetInfo()
850+
infos["lowWater"] = inf.LowWater
851+
infos["highWater"] = inf.HighWater
852+
infos["lastTrim"] = inf.LastTrim
853+
infos["gracePeriod"] = inf.GracePeriod.String()
854+
infos["connCount"] = inf.ConnCount
855+
default:
856+
infos["type"] = "unknown"
857+
}
858+
859+
res.SetOutput(infos)
860+
},
861+
Marshalers: cmds.MarshalerMap{
862+
cmds.Text: func(res cmds.Response) (io.Reader, error) {
863+
infosp, ok := res.Output().(*map[string]interface{})
864+
if !ok {
865+
return nil, fmt.Errorf("expected output type to be a map: %#v", res.Output())
866+
}
867+
infos := *infosp
868+
869+
buf := new(bytes.Buffer)
870+
871+
switch infos["type"] {
872+
case "basic":
873+
tstr := "<n/a>"
874+
t, err := time.Parse(time.RFC3339Nano, infos["lastTrim"].(string))
875+
if err != nil {
876+
tstr = "<err>"
877+
} else if !t.IsZero() {
878+
tstr = t.String()
879+
}
880+
881+
fmt.Fprintf(buf, "Type: basic\n")
882+
fmt.Fprintf(buf, "Low Water: %d\n", int(infos["lowWater"].(float64)))
883+
fmt.Fprintf(buf, "High Water: %d\n", int(infos["highWater"].(float64)))
884+
fmt.Fprintf(buf, "Grace Period: %s\n", infos["gracePeriod"])
885+
fmt.Fprintf(buf, "Connection Count: %d\n", int(infos["connCount"].(float64)))
886+
fmt.Fprintf(buf, "Last Trim: %s\n", tstr)
887+
case "none":
888+
fmt.Println("Connection Manager Disabled")
889+
default:
890+
log.Errorf("unknown connection manager type: %s", infos["type"])
891+
fmt.Println("Unknown Connection Manager Settings")
892+
}
893+
894+
return buf, nil
895+
},
896+
},
897+
Type: make(map[string]interface{}),
898+
}

core/core.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,6 @@ import (
4545
yamux "gx/ipfs/QmNWCEvi7bPRcvqAV8AKLGVNoQdArWi7NJayka2SM4XtRe/go-smux-yamux"
4646
cid "gx/ipfs/QmNp85zy9RLrQ5oQD4hPyS39ezrrXpcaa7R4Y9kxdWQLLQ/go-cid"
4747
mplex "gx/ipfs/QmP81tTizXSjKTLWhjty1rabPQHe1YPMj6Bq5gR5fWZakn/go-smux-multiplex"
48-
connmgr "gx/ipfs/QmPErLx83hgF5XKqjeYaLWXJSFon4mH7YPzoftUvfrPgQG/go-libp2p-connmgr"
4948
routing "gx/ipfs/QmPR2JzfKd9poHx9XBhzoFeBBC31ZM3W5iUPKJZWyaoZZm/go-libp2p-routing"
5049
pstore "gx/ipfs/QmPgDWmTmuzvP7QE5zwo1TmjbJme9pmZHNujB2453jkCTr/go-libp2p-peerstore"
5150
mafilter "gx/ipfs/QmQBB2dQLmQHJgs2gqZ3iqL2XiuCtUCvXzWt5kMXDf5Zcr/go-maddr-filter"
@@ -57,6 +56,7 @@ import (
5756
logging "gx/ipfs/QmSpJByNKFX1sCsHBEp3R73FL4NF6FnQTEGyNAXHm2GS52/go-log"
5857
b58 "gx/ipfs/QmT8rehPR3F6bmwL6zjUN8XpiDBFFpMP2myPdC6ApsWfJf/go-base58"
5958
floodsub "gx/ipfs/QmUUSLfvihARhCxxgnjW4hmycJpPvzNu12Aaz6JWVdfnLg/go-libp2p-floodsub"
59+
connmgr "gx/ipfs/QmUbUNq1Q6eE2LXqKzKW8BW1SUMqscAj9A3ot9j5suuaRb/go-libp2p-connmgr"
6060
addrutil "gx/ipfs/QmVJGsPeK3vwtEyyTxpCs47yjBYMmYsAhEouPDF3Gb2eK3/go-addr-util"
6161
ds "gx/ipfs/QmVSase1JP7cq9QkPT46oNwdp9pT6kBkG3oqS14y3QcZjG/go-datastore"
6262
mssmux "gx/ipfs/QmVniQJkdzLZaZwzwMdd3dJTvWiJ1DQEkreVy6hs6h7Vk5/go-smux-multistream"

package.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -472,9 +472,9 @@
472472
},
473473
{
474474
"author": "whyrusleeping",
475-
"hash": "QmPErLx83hgF5XKqjeYaLWXJSFon4mH7YPzoftUvfrPgQG",
475+
"hash": "QmUbUNq1Q6eE2LXqKzKW8BW1SUMqscAj9A3ot9j5suuaRb",
476476
"name": "go-libp2p-connmgr",
477-
"version": "0.3.2"
477+
"version": "0.3.3"
478478
},
479479
{
480480
"author": "why",

test/sharness/t0140-swarm.sh

+43
Original file line numberDiff line numberDiff line change
@@ -97,4 +97,47 @@ test_expect_success "Addresses.NoAnnounce with /ipcidr affects addresses" '
9797

9898
test_kill_ipfs_daemon
9999

100+
test_launch_ipfs_daemon
101+
102+
test_expect_success "ipfs swarm connmgr shows default settings" '
103+
ipfs swarm connmgr > connmgr_out &&
104+
grep "Low Water: 600" connmgr_out &&
105+
grep "High Water: 900" connmgr_out &&
106+
grep "Type: basic" connmgr_out
107+
'
108+
109+
test_kill_ipfs_daemon
110+
111+
test_expect_success "clear out connmgr config" '
112+
ipfs config --json Swarm.ConnMgr.HighWater 1500
113+
ipfs config Swarm.ConnMgr.GracePeriod "3s"
114+
'
115+
116+
test_launch_ipfs_daemon
117+
118+
test_expect_success "ipfs swarm connmgr shows modified settings" '
119+
ipfs swarm connmgr > connmgr_out &&
120+
grep "Low Water: 600" connmgr_out &&
121+
grep "Grace Period: 3s" connmgr_out &&
122+
grep "High Water: 1500" connmgr_out &&
123+
grep "Type: basic" connmgr_out
124+
'
125+
126+
test_kill_ipfs_daemon
127+
128+
test_expect_success "clear out connmgr config" '
129+
ipfs config --json Swarm.ConnMgr "{}"
130+
'
131+
132+
test_launch_ipfs_daemon
133+
134+
test_expect_success "ipfs swarm connmgr shows default settings" '
135+
ipfs swarm connmgr > connmgr_out &&
136+
grep "Low Water: 600" connmgr_out &&
137+
grep "High Water: 900" connmgr_out &&
138+
grep "Type: basic" connmgr_out
139+
'
140+
141+
test_kill_ipfs_daemon
142+
100143
test_done

0 commit comments

Comments
 (0)