-
-
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
feat(command): add connection direction #5457
Changes from 1 commit
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 |
---|---|---|
|
@@ -81,15 +81,14 @@ var swarmPeersCmd = &cmds.Command{ | |
streams, _, _ := req.Option("streams").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(), | ||
Addr: addr.String(), | ||
Peer: pid.Pretty(), | ||
Direction: inet.Direction(-1), | ||
} | ||
|
||
/* | ||
|
@@ -101,6 +100,9 @@ var swarmPeersCmd = &cmds.Command{ | |
*/ | ||
|
||
if verbose || latency { | ||
// set direction | ||
ci.Direction = c.Stat().Direction | ||
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. Instead of overloading the |
||
|
||
lat := n.Peerstore.LatencyEWMA(pid) | ||
if lat == 0 { | ||
ci.Latency = "n/a" | ||
|
@@ -146,6 +148,11 @@ var swarmPeersCmd = &cmds.Command{ | |
if info.Latency != "" { | ||
fmt.Fprintf(buf, " %s", info.Latency) | ||
} | ||
|
||
if info.Direction >= 0 { | ||
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'd just check to see if the user has passed the appropriate flag (either Alternatively, we can just not print anything if the direction is unknown. Either way is fine by me. |
||
fmt.Fprintf(buf, " %s", directionString(info.Direction)) | ||
} | ||
|
||
fmt.Fprintln(buf) | ||
|
||
for _, s := range info.Streams { | ||
|
@@ -168,11 +175,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 { | ||
|
@@ -203,6 +211,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 "unknown" | ||
} | ||
} | ||
|
||
var swarmAddrsCmd = &cmds.Command{ | ||
Helptext: cmdkit.HelpText{ | ||
Tagline: "List known addresses. Useful for debugging.", | ||
|
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.
Let's just let this default to 0 ("unknown"). By using
-1
, we're changing the API a bit: new clients running against old servers will seeDirection: 0
(instead ofDirection: -1
) even if they haven't requested the direction. When they do, they'll print the direction as if the user had requested it. See my comment at https://github.com/ipfs/go-ipfs/pull/5457/files#diff-ab43cdc4b214774869f9dc156ebc0fa0R152