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
|
package cli
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"context"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
"os"
|
"os"
|
||||||
"sort"
|
"sort"
|
||||||
"strings"
|
"strings"
|
||||||
"text/tabwriter"
|
"text/tabwriter"
|
||||||
|
"time"
|
||||||
|
|
||||||
"github.com/dustin/go-humanize"
|
"github.com/dustin/go-humanize"
|
||||||
"github.com/urfave/cli/v2"
|
"github.com/urfave/cli/v2"
|
||||||
@ -128,6 +130,12 @@ var NetPing = &cli.Command{
|
|||||||
Aliases: []string{"c"},
|
Aliases: []string{"c"},
|
||||||
Usage: "specify the number of times it should ping",
|
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 {
|
Action: func(cctx *cli.Context) error {
|
||||||
if cctx.Args().Len() != 1 {
|
if cctx.Args().Len() != 1 {
|
||||||
@ -142,19 +150,47 @@ var NetPing = &cli.Command{
|
|||||||
|
|
||||||
ctx := ReqContext(cctx)
|
ctx := ReqContext(cctx)
|
||||||
|
|
||||||
peerID, err := peer.Decode(cctx.Args().First())
|
pis, err := addrInfoFromArg(ctx, cctx)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return xerrors.Errorf("failed to parse peerID: %v", err)
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
count := cctx.Int("count")
|
count := cctx.Int("count")
|
||||||
for i := 0; i < count; i++ {
|
interval := cctx.Duration("interval")
|
||||||
RTT, err := api.NetPing(ctx, peerID)
|
|
||||||
|
for _, pi := range pis {
|
||||||
|
err := api.NetConnect(ctx, pi)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Errorf("ping failed: %v", err)
|
return xerrors.Errorf("connect: %w", err)
|
||||||
continue
|
}
|
||||||
|
|
||||||
|
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
|
return nil
|
||||||
},
|
},
|
||||||
@ -235,45 +271,9 @@ var NetConnect = &cli.Command{
|
|||||||
defer closer()
|
defer closer()
|
||||||
ctx := ReqContext(cctx)
|
ctx := ReqContext(cctx)
|
||||||
|
|
||||||
pis, err := addrutil.ParseAddresses(ctx, cctx.Args().Slice())
|
pis, err := addrInfoFromArg(ctx, cctx)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
a, perr := address.NewFromString(cctx.Args().First())
|
return err
|
||||||
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)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, pi := range pis {
|
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{
|
var NetId = &cli.Command{
|
||||||
Name: "id",
|
Name: "id",
|
||||||
Usage: "Get node identity",
|
Usage: "Get node identity",
|
||||||
|
@ -1245,8 +1245,9 @@ USAGE:
|
|||||||
lotus-miner net ping [command options] [arguments...]
|
lotus-miner net ping [command options] [arguments...]
|
||||||
|
|
||||||
OPTIONS:
|
OPTIONS:
|
||||||
--count value, -c value specify the number of times it should ping (default: 10)
|
--count value, -c value specify the number of times it should ping (default: 10)
|
||||||
--help, -h show help (default: false)
|
--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...]
|
lotus net ping [command options] [arguments...]
|
||||||
|
|
||||||
OPTIONS:
|
OPTIONS:
|
||||||
--count value, -c value specify the number of times it should ping (default: 10)
|
--count value, -c value specify the number of times it should ping (default: 10)
|
||||||
--help, -h show help (default: false)
|
--interval value, -i value minimum time between pings (default: 1s)
|
||||||
|
--help, -h show help (default: false)
|
||||||
|
|
||||||
```
|
```
|
||||||
|
|
||||||
|
@ -7,6 +7,7 @@ import (
|
|||||||
"time"
|
"time"
|
||||||
|
|
||||||
"go.uber.org/fx"
|
"go.uber.org/fx"
|
||||||
|
"golang.org/x/xerrors"
|
||||||
|
|
||||||
"github.com/libp2p/go-libp2p-core/host"
|
"github.com/libp2p/go-libp2p-core/host"
|
||||||
"github.com/libp2p/go-libp2p-core/metrics"
|
"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) {
|
func (a *NetAPI) NetPing(ctx context.Context, p peer.ID) (time.Duration, error) {
|
||||||
pingService := ping.NewPingService(a.Host)
|
result, ok := <-ping.Ping(ctx, a.Host, p)
|
||||||
|
if !ok {
|
||||||
result := <-pingService.Ping(ctx, p)
|
return 0, xerrors.Errorf("didn't get ping result: %w", ctx.Err())
|
||||||
|
}
|
||||||
return result.RTT, result.Error
|
return result.RTT, result.Error
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user