-
-
Notifications
You must be signed in to change notification settings - Fork 3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
add a subcommand for the connmgr #4308
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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" | ||
pstore "gx/ipfs/QmPgDWmTmuzvP7QE5zwo1TmjbJme9pmZHNujB2453jkCTr/go-libp2p-peerstore" | ||
swarm "gx/ipfs/QmdQFrFnPrKRQtpeHKjZ3cVNwxmGKKS2TvhJTuN9C9yduh/go-libp2p-swarm" | ||
iaddr "gx/ipfs/QmeS8cCKawUwejVrsBtmC1toTXmwVWZGiRJqzgTURVWeF9/go-ipfs-addr" | ||
|
||
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" | ||
iaddr "gx/ipfs/QmeS8cCKawUwejVrsBtmC1toTXmwVWZGiRJqzgTURVWeF9/go-ipfs-addr" | ||
) | ||
|
||
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"] = "<nil>" | ||
case *ifconnmgr.NullConnMgr: | ||
infos["type"] = "disabled" | ||
case *connmgr.BasicConnMgr: | ||
infos["type"] = "basic" | ||
|
||
inf := cmgr.GetInfo() | ||
infos["lowWater"] = inf.LowWater | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I assume these are supposed to be human readable keys? If so, we should probably just say things like "Low Watermark" instead. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Those will be the result of JSON endpoint. We can create mappings in TextMarshaller. |
||
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{}), | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is distinguishing between these two cases really useful? Users aren't going to know what
<nil>
means.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
They shouldnt ever see "nil", Everything currently will either return a NullConnMgr or a BasicConnMgr. So maybe I should remove it?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Probably. I'd fail with an error if this isn't supposed to happen.