ping: Match go-ipfs in cmd output

This commit is contained in:
Łukasz Magiera 2022-03-21 13:56:06 +01:00
parent 701d0a111e
commit 945b93590d
4 changed files with 101 additions and 52 deletions

View File

@ -1,12 +1,14 @@
package cli
import (
"context"
"encoding/json"
"fmt"
"os"
"sort"
"strings"
"text/tabwriter"
"time"
"github.com/dustin/go-humanize"
"github.com/urfave/cli/v2"
@ -128,6 +130,12 @@ var NetPing = &cli.Command{
Aliases: []string{"c"},
Usage: "specify the number of times it should ping",
},
&cli.DurationFlag{
Name: "interval",
Value: time.Second,
Aliases: []string{"i"},
Usage: "minimum time between pings",
},
},
Action: func(cctx *cli.Context) error {
if cctx.Args().Len() != 1 {
@ -142,19 +150,47 @@ var NetPing = &cli.Command{
ctx := ReqContext(cctx)
peerID, err := peer.Decode(cctx.Args().First())
pis, err := addrInfoFromArg(ctx, cctx)
if err != nil {
return xerrors.Errorf("failed to parse peerID: %v", err)
return err
}
count := cctx.Int("count")
for i := 0; i < count; i++ {
RTT, err := api.NetPing(ctx, peerID)
interval := cctx.Duration("interval")
for _, pi := range pis {
err := api.NetConnect(ctx, pi)
if err != nil {
log.Errorf("ping failed: %v", err)
return xerrors.Errorf("connect: %w", err)
}
fmt.Printf("PING %s\n", pi.ID)
var avg time.Duration
var successful int
for i := 0; i < count && ctx.Err() == nil; i++ {
start := time.Now()
rtt, err := api.NetPing(ctx, pi.ID)
if err != nil {
if ctx.Err() != nil {
break
}
log.Errorf("Ping failed: error=%v", err)
continue
}
fmt.Printf("ping %d, %v\n", i, RTT)
fmt.Printf("Pong received: time=%v\n", rtt)
avg = avg + rtt
successful++
wctx, cancel := context.WithTimeout(ctx, time.Until(start.Add(interval)))
<-wctx.Done()
cancel()
}
if successful > 0 {
fmt.Printf("Average latency: %v\n", avg/time.Duration(successful))
}
}
return nil
},
@ -235,26 +271,46 @@ var NetConnect = &cli.Command{
defer closer()
ctx := ReqContext(cctx)
pis, err := addrInfoFromArg(ctx, cctx)
if err != nil {
return err
}
for _, pi := range pis {
fmt.Printf("connect %s: ", pi.ID.Pretty())
err := api.NetConnect(ctx, pi)
if err != nil {
fmt.Println("failure")
return err
}
fmt.Println("success")
}
return nil
},
}
func addrInfoFromArg(ctx context.Context, cctx *cli.Context) ([]peer.AddrInfo, error) {
pis, err := addrutil.ParseAddresses(ctx, cctx.Args().Slice())
if err != nil {
a, perr := address.NewFromString(cctx.Args().First())
if perr != nil {
return err
return nil, err
}
na, fc, err := GetFullNodeAPI(cctx)
if err != nil {
return err
return nil, err
}
defer fc()
mi, err := na.StateMinerInfo(ctx, a, types.EmptyTSK)
if err != nil {
return xerrors.Errorf("getting miner info: %w", err)
return nil, xerrors.Errorf("getting miner info: %w", err)
}
if mi.PeerId == nil {
return xerrors.Errorf("no PeerID for miner")
return nil, xerrors.Errorf("no PeerID for miner")
}
multiaddrs := make([]multiaddr.Multiaddr, 0, len(mi.Multiaddrs))
for i, a := range mi.Multiaddrs {
@ -276,18 +332,7 @@ var NetConnect = &cli.Command{
pis = append(pis, pi)
}
for _, pi := range pis {
fmt.Printf("connect %s: ", pi.ID.Pretty())
err := api.NetConnect(ctx, pi)
if err != nil {
fmt.Println("failure")
return err
}
fmt.Println("success")
}
return nil
},
return pis, err
}
var NetId = &cli.Command{

View File

@ -1246,6 +1246,7 @@ USAGE:
OPTIONS:
--count value, -c value specify the number of times it should ping (default: 10)
--interval value, -i value minimum time between pings (default: 1s)
--help, -h show help (default: false)
```

View File

@ -2648,6 +2648,7 @@ USAGE:
OPTIONS:
--count value, -c value specify the number of times it should ping (default: 10)
--interval value, -i value minimum time between pings (default: 1s)
--help, -h show help (default: false)
```

View File

@ -7,6 +7,7 @@ import (
"time"
"go.uber.org/fx"
"golang.org/x/xerrors"
"github.com/libp2p/go-libp2p-core/host"
"github.com/libp2p/go-libp2p-core/metrics"
@ -180,9 +181,10 @@ func (a *NetAPI) NetBandwidthStatsByPeer(ctx context.Context) (map[string]metric
}
func (a *NetAPI) NetPing(ctx context.Context, p peer.ID) (time.Duration, error) {
pingService := ping.NewPingService(a.Host)
result := <-pingService.Ping(ctx, p)
result, ok := <-ping.Ping(ctx, a.Host, p)
if !ok {
return 0, xerrors.Errorf("didn't get ping result: %w", ctx.Err())
}
return result.RTT, result.Error
}