Skip to content
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

feat(command): add connection direction #5457

Merged
merged 2 commits into from
Sep 14, 2018
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 30 additions & 7 deletions core/commands/swarm.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ var swarmPeersCmd = &cmds.Command{
cmdkit.BoolOption("verbose", "v", "display all extra information"),
cmdkit.BoolOption("streams", "Also list information about open streams for each peer"),
cmdkit.BoolOption("latency", "Also list information about latency to each peer"),
cmdkit.BoolOption("direction", "Also list information about the direction of connection"),
},
Run: func(req cmds.Request, res cmds.Response) {

Expand All @@ -79,14 +80,13 @@ var swarmPeersCmd = &cmds.Command{
verbose, _, _ := req.Option("verbose").Bool()
latency, _, _ := req.Option("latency").Bool()
streams, _, _ := req.Option("streams").Bool()
direction, _, _ := req.Option("direction").Bool()

conns := n.PeerHost.Network().Conns()

var out connInfos
for _, c := range conns {
pid := c.RemotePeer()
addr := c.RemoteMultiaddr()

ci := connInfo{
Addr: addr.String(),
Peer: pid.Pretty(),
Expand All @@ -100,6 +100,11 @@ var swarmPeersCmd = &cmds.Command{
}
*/

if verbose || direction {
// set direction
ci.Direction = c.Stat().Direction
}

if verbose || latency {
lat := n.Peerstore.LatencyEWMA(pid)
if lat == 0 {
Expand Down Expand Up @@ -146,6 +151,11 @@ var swarmPeersCmd = &cmds.Command{
if info.Latency != "" {
fmt.Fprintf(buf, " %s", info.Latency)
}

if info.Direction != inet.DirUnknown {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit (feel free to ignore): can we check for the direction or verbose flag here? I prefer not to rely on default (unset) values.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See: #5457 (comment). We've explicitly defined the zero value of Direction to be DirUnknown so this should always work.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

SGTY or would you actually like to print "unknown" if --direction is passed?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

SGTY or would you actually like to print "unknown" if --direction is passed?

I'm fine either way ("unknown" or any non-empty string would make the output easier to parse in case we add more information fields in the future), my original comment was more about the fact that we are overloading the DirUnknown value to mean either unknown and/or unset, but's just a nit, I'm fine with the patch as is.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, I see your point. The original patch used -1 but I that would change the API (go tends to assume missing == 0 so old go-ipfs daemons would return 0 instead of -1).

I'm just going to go with this for now as I think it's the simplest solution. If it continues to bug you, we can do a separate PR.

(note: users should be parsing ipfs --enc=json swarm peers if they know what's good for them...).

fmt.Fprintf(buf, " %s", directionString(info.Direction))
}

fmt.Fprintln(buf)

for _, s := range info.Streams {
Expand All @@ -168,11 +178,12 @@ type streamInfo struct {
}

type connInfo struct {
Addr string
Peer string
Latency string
Muxer string
Streams []streamInfo
Addr string
Peer string
Latency string
Muxer string
Direction inet.Direction
Streams []streamInfo
}

func (ci *connInfo) Less(i, j int) bool {
Expand Down Expand Up @@ -203,6 +214,18 @@ func (ci connInfos) Swap(i, j int) {
ci.Peers[i], ci.Peers[j] = ci.Peers[j], ci.Peers[i]
}

// directionString transfers to string
func directionString(d inet.Direction) string {
switch d {
case inet.DirInbound:
return "inbound"
case inet.DirOutbound:
return "outbound"
default:
return ""
}
}

var swarmAddrsCmd = &cmds.Command{
Helptext: cmdkit.HelpText{
Tagline: "List known addresses. Useful for debugging.",
Expand Down