@cryptotaxi247 / kubo / commits / 566a86f5d

Address PR comments and add in more user feedback

Jeromy committed Jan 9, 2015 at 19:04 UTC 566a86f5d4a3e7e64d98dcada09f44c4e8eaf9e6
5 files changed +47 -24
core/commands/ping.go
+34 -16
@@ -2,7 +2,6 @@ package commands
2
3 import (
4 "bytes"
5 - "errors"
5 "fmt"
6 "io"
7 "time"
@@ -19,6 +18,7 @@ const kPingTimeout = 10 * time.Second
18 type PingResult struct {
19 Success bool
20 Time time.Duration
21 + Text string
22 }
23
24 var PingCmd = &cmds.Command{
@@ -33,7 +33,10 @@ send pings, wait for pongs, and print out round-trip latency information.
33 `,
34 },
35 Arguments: []cmds.Argument{
36 - cmds.StringArg("count", false, true, "Number of pings to perform"),
36 + cmds.StringArg("peer ID", true, true, "ID of peer to be pinged"),
37 + },
38 + Options: []cmds.Option{
39 + cmds.IntOption("count", "n"),
40 },
41 Marshalers: cmds.MarshalerMap{
42 cmds.Text: func(res cmds.Response) (io.Reader, error) {
@@ -49,7 +52,9 @@ send pings, wait for pongs, and print out round-trip latency information.
52 }
53
54 buf := new(bytes.Buffer)
52 - if obj.Success {
55 + if len(obj.Text) > 0 {
56 + buf = bytes.NewBufferString(obj.Text + "\n")
57 + } else if obj.Success {
58 fmt.Fprintf(buf, "Pong took %.2fms\n", obj.Time.Seconds()*1000)
59 } else {
60 fmt.Fprintf(buf, "Pong failed\n")
@@ -75,9 +80,11 @@ send pings, wait for pongs, and print out round-trip latency information.
80 }
81
82 if len(req.Arguments()) == 0 {
78 - return nil, errors.New("no peer specified!")
83 + return nil, cmds.ClientError("no peer specified!")
84 }
85
86 + outChan := make(chan interface{}, 5)
87 +
88 // Set up number of pings
89 numPings := 10
90 val, found, err := req.Option("count").Int()
@@ -94,33 +101,44 @@ send pings, wait for pongs, and print out round-trip latency information.
101 return nil, err
102 }
103
97 - // Make sure we can find the node in question
98 - ctx, _ := context.WithTimeout(context.Background(), kPingTimeout)
99 - p, err := n.Routing.FindPeer(ctx, peerID)
100 - if err != nil {
101 - return nil, err
102 - }
103 -
104 - outChan := make(chan interface{})
105 -
104 go func() {
105 defer close(outChan)
106 +
107 + // Make sure we can find the node in question
108 + outChan <- &PingResult{
109 + Text: fmt.Sprintf("Looking up peer %s", peerID.Pretty()),
110 + }
111 + ctx, _ := context.WithTimeout(context.Background(), kPingTimeout)
112 + p, err := n.Routing.FindPeer(ctx, peerID)
113 + if err != nil {
114 + outChan <- &PingResult{Text: "Peer lookup error!"}
115 + outChan <- &PingResult{Text: err.Error()}
116 + return
117 + }
118 + outChan <- &PingResult{
119 + Text: fmt.Sprintf("Peer found, starting pings."),
120 + }
121 +
122 + var total time.Duration
123 for i := 0; i < numPings; i++ {
124 ctx, _ = context.WithTimeout(context.Background(), kPingTimeout)
110 - before := time.Now()
111 - err := n.Routing.Ping(ctx, p.ID)
125 + took, err := n.Routing.Ping(ctx, p.ID)
126 if err != nil {
127 log.Errorf("Ping error: %s", err)
128 outChan <- &PingResult{}
129 break
130 }
117 - took := time.Now().Sub(before)
131 outChan <- &PingResult{
132 Success: true,
133 Time: took,
134 }
135 + total += took
136 time.Sleep(time.Second)
137 }
138 + averagems := total.Seconds() * 1000 / float64(numPings)
139 + outChan <- &PingResult{
140 + Text: fmt.Sprintf("Average latency: %.2fms", averagems),
141 + }
142 }()
143
144 return outChan, nil
routing/dht/dht.go
+3 -3
@@ -103,8 +103,8 @@ func (dht *IpfsDHT) Connect(ctx context.Context, npeer peer.ID) error {
103
104 // Ping new peer to register in their routing table
105 // NOTE: this should be done better...
106 - if err := dht.Ping(ctx, npeer); err != nil {
107 - return fmt.Errorf("failed to ping newly connected peer: %s\n", err)
106 + if _, err := dht.Ping(ctx, npeer); err != nil {
107 + return fmt.Errorf("failed to ping newly connected peer: %s", err)
108 }
109 log.Event(ctx, "connect", dht.self, npeer)
110 dht.Update(ctx, npeer)
@@ -329,7 +329,7 @@ func (dht *IpfsDHT) PingRoutine(t time.Duration) {
329 peers := dht.routingTable.NearestPeers(kb.ConvertKey(u.Key(id)), 5)
330 for _, p := range peers {
331 ctx, _ := context.WithTimeout(dht.Context(), time.Second*5)
332 - err := dht.Ping(ctx, p)
332 + _, err := dht.Ping(ctx, p)
333 if err != nil {
334 log.Errorf("Ping error: %s", err)
335 }
routing/dht/routing.go
+5 -2
@@ -3,6 +3,7 @@ package dht
3 import (
4 "math"
5 "sync"
6 + "time"
7
8 context "github.com/jbenet/go-ipfs/Godeps/_workspace/src/code.google.com/p/go.net/context"
9
@@ -434,12 +435,14 @@ func (dht *IpfsDHT) FindPeersConnectedToPeer(ctx context.Context, id peer.ID) (<
435 }
436
437 // Ping a peer, log the time it took
437 -func (dht *IpfsDHT) Ping(ctx context.Context, p peer.ID) error {
438 +func (dht *IpfsDHT) Ping(ctx context.Context, p peer.ID) (time.Duration, error) {
439 // Thoughts: maybe this should accept an ID and do a peer lookup?
440 log.Debugf("ping %s start", p)
441 + before := time.Now()
442
443 pmes := pb.NewMessage(pb.Message_PING, "", 0)
444 _, err := dht.sendRequest(ctx, p, pmes)
445 log.Debugf("ping %s end (err = %s)", p, err)
444 - return err
446 +
447 + return time.Now().Sub(before), err
448 }
routing/mock/centralized_client.go
+3 -2
@@ -2,6 +2,7 @@ package mockrouting
2
3 import (
4 "errors"
5 + "time"
6
7 context "github.com/jbenet/go-ipfs/Godeps/_workspace/src/code.google.com/p/go.net/context"
8 ds "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-datastore"
@@ -79,8 +80,8 @@ func (c *client) Provide(_ context.Context, key u.Key) error {
80 return c.server.Announce(info, key)
81 }
82
82 -func (c *client) Ping(ctx context.Context, p peer.ID) error {
83 - return nil
83 +func (c *client) Ping(ctx context.Context, p peer.ID) (time.Duration, error) {
84 + return 0, nil
85 }
86
87 var _ routing.IpfsRouting = &client{}
routing/routing.go
+2 -1
@@ -3,6 +3,7 @@ package routing
3
4 import (
5 "errors"
6 + "time"
7
8 context "github.com/jbenet/go-ipfs/Godeps/_workspace/src/code.google.com/p/go.net/context"
9
@@ -38,5 +39,5 @@ type IpfsRouting interface {
39 FindPeer(context.Context, peer.ID) (peer.PeerInfo, error)
40
41 // Ping a peer, log the time it took
41 - Ping(context.Context, peer.ID) error
42 + Ping(context.Context, peer.ID) (time.Duration, error)
43 }