ping: Match go-ipfs in cmd output
This commit is contained in:
parent
701d0a111e
commit
945b93590d
135
cli/net.go
135
cli/net.go
@ -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)
|
||||
continue
|
||||
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("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))
|
||||
}
|
||||
fmt.Printf("ping %d, %v\n", i, RTT)
|
||||
}
|
||||
return nil
|
||||
},
|
||||
@ -235,45 +271,9 @@ var NetConnect = &cli.Command{
|
||||
defer closer()
|
||||
ctx := ReqContext(cctx)
|
||||
|
||||
pis, err := addrutil.ParseAddresses(ctx, cctx.Args().Slice())
|
||||
pis, err := addrInfoFromArg(ctx, cctx)
|
||||
if err != nil {
|
||||
a, perr := address.NewFromString(cctx.Args().First())
|
||||
if perr != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
na, fc, err := GetFullNodeAPI(cctx)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer fc()
|
||||
|
||||
mi, err := na.StateMinerInfo(ctx, a, types.EmptyTSK)
|
||||
if err != nil {
|
||||
return xerrors.Errorf("getting miner info: %w", err)
|
||||
}
|
||||
|
||||
if mi.PeerId == nil {
|
||||
return xerrors.Errorf("no PeerID for miner")
|
||||
}
|
||||
multiaddrs := make([]multiaddr.Multiaddr, 0, len(mi.Multiaddrs))
|
||||
for i, a := range mi.Multiaddrs {
|
||||
maddr, err := multiaddr.NewMultiaddrBytes(a)
|
||||
if err != nil {
|
||||
log.Warnf("parsing multiaddr %d (%x): %s", i, a, err)
|
||||
continue
|
||||
}
|
||||
multiaddrs = append(multiaddrs, maddr)
|
||||
}
|
||||
|
||||
pi := peer.AddrInfo{
|
||||
ID: *mi.PeerId,
|
||||
Addrs: multiaddrs,
|
||||
}
|
||||
|
||||
fmt.Printf("%s -> %s\n", a, pi)
|
||||
|
||||
pis = append(pis, pi)
|
||||
return err
|
||||
}
|
||||
|
||||
for _, pi := range pis {
|
||||
@ -290,6 +290,51 @@ var NetConnect = &cli.Command{
|
||||
},
|
||||
}
|
||||
|
||||
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 nil, err
|
||||
}
|
||||
|
||||
na, fc, err := GetFullNodeAPI(cctx)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer fc()
|
||||
|
||||
mi, err := na.StateMinerInfo(ctx, a, types.EmptyTSK)
|
||||
if err != nil {
|
||||
return nil, xerrors.Errorf("getting miner info: %w", err)
|
||||
}
|
||||
|
||||
if mi.PeerId == nil {
|
||||
return nil, xerrors.Errorf("no PeerID for miner")
|
||||
}
|
||||
multiaddrs := make([]multiaddr.Multiaddr, 0, len(mi.Multiaddrs))
|
||||
for i, a := range mi.Multiaddrs {
|
||||
maddr, err := multiaddr.NewMultiaddrBytes(a)
|
||||
if err != nil {
|
||||
log.Warnf("parsing multiaddr %d (%x): %s", i, a, err)
|
||||
continue
|
||||
}
|
||||
multiaddrs = append(multiaddrs, maddr)
|
||||
}
|
||||
|
||||
pi := peer.AddrInfo{
|
||||
ID: *mi.PeerId,
|
||||
Addrs: multiaddrs,
|
||||
}
|
||||
|
||||
fmt.Printf("%s -> %s\n", a, pi)
|
||||
|
||||
pis = append(pis, pi)
|
||||
}
|
||||
|
||||
return pis, err
|
||||
}
|
||||
|
||||
var NetId = &cli.Command{
|
||||
Name: "id",
|
||||
Usage: "Get node identity",
|
||||
|
@ -1245,8 +1245,9 @@ USAGE:
|
||||
lotus-miner net ping [command options] [arguments...]
|
||||
|
||||
OPTIONS:
|
||||
--count value, -c value specify the number of times it should ping (default: 10)
|
||||
--help, -h show help (default: false)
|
||||
--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)
|
||||
|
||||
```
|
||||
|
||||
|
@ -2647,8 +2647,9 @@ USAGE:
|
||||
lotus net ping [command options] [arguments...]
|
||||
|
||||
OPTIONS:
|
||||
--count value, -c value specify the number of times it should ping (default: 10)
|
||||
--help, -h show help (default: false)
|
||||
--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)
|
||||
|
||||
```
|
||||
|
||||
|
@ -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
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user