Skip to content

Commit

Permalink
Added a verbose option for swarm peers.
Browse files Browse the repository at this point in the history
This is used like 'ipfs swarm peers -v'. It prints out the normal peers
information, but also the latency of each peer

License: MIT
Signed-off-by: Chris Sasarak <chris.sasarak@gmail.com>
  • Loading branch information
csasarak committed May 17, 2016
1 parent f40127d commit 762aded
Showing 1 changed file with 30 additions and 1 deletion.
31 changes: 30 additions & 1 deletion core/commands/swarm.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"io"
"path"
"sort"
"time"

cmds "github.com/ipfs/go-ipfs/commands"
iaddr "github.com/ipfs/go-ipfs/thirdparty/ipfsaddr"
Expand All @@ -15,6 +16,7 @@ import (

mafilter "gx/ipfs/QmUaRHbB7pUwj5mS9BS4CMvBiW48MpaH2wbGxeWfFhhHxK/multiaddr-filter"
ma "gx/ipfs/QmYzDkkgAEmrcNzFCiYo6L1dTX4EAG1gZkbtdbd9trL4vd/go-multiaddr"
context "gx/ipfs/QmZy2y8t9zQH2a1b8q2ZSLKp17ATuJoCNxxyMFG5qFExpt/go-net/context"
)

type stringList struct {
Expand Down Expand Up @@ -49,8 +51,15 @@ var swarmPeersCmd = &cmds.Command{
ShortDescription: `
'ipfs swarm peers' lists the set of peers this node is connected to.
`,
},
Options: []cmds.Option{
cmds.BoolOption("verbose", "v",
`Also display latency along with peer information in the following form:
<peer address> <latency>
`),
},
Run: func(req cmds.Request, res cmds.Response) {
timeout := time.Duration(5) * time.Second

log.Debug("ipfs swarm peers")
n, err := req.InvocContext().GetNode()
Expand All @@ -64,12 +73,32 @@ var swarmPeersCmd = &cmds.Command{
return
}

verbose, _, _ := req.Option("verbose").Bool()
conns := n.PeerHost.Network().Conns()
addrs := make([]string, len(conns))
pingService := n.Ping

for i, c := range conns {
pid := c.RemotePeer()
addr := c.RemoteMultiaddr()
addrs[i] = fmt.Sprintf("%s/ipfs/%s", addr, pid.Pretty())

if verbose {
pContext, cancelFn := context.WithTimeout(req.Context(), timeout)
defer cancelFn()

ch, _ := pingService.Ping(pContext, pid)
duration := <-ch
var durStr string

if pContext.Err() != nil {
durStr = pContext.Err().Error()
} else {
durStr = duration.String()
}
addrs[i] = fmt.Sprintf("%s/ipfs/%s %s", addr, pid.Pretty(), durStr)
} else {
addrs[i] = fmt.Sprintf("%s/ipfs/%s", addr, pid.Pretty())
}
}

sort.Sort(sort.StringSlice(addrs))
Expand Down

1 comment on commit 762aded

@csasarak
Copy link
Owner Author

Choose a reason for hiding this comment

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

This addresses ipfs#2629. I don't really think it's done but I wanted to make sure that I'm not going off
the rails. I couldn't find anywhere that latency was stored, so I used the ping service to find out. Some questions/comments I had:

  1. Is there a standard timeout stored somewhere? I used five seconds but would rather use something I didn't make up.
  2. I printed the context's error message in the case that the ping fails. Is there some other preferred message? I most often got "context deadline has passed".
  3. One potential shortcoming of this is that it has to contact each peer and either get a response/timeout before printing anything. Is that acceptable, or should I try to do output in a go routine?

Any advice you can provide would be helpful, so thank you in advance!

Please sign in to comment.