Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add only connect #52

Merged
merged 1 commit into from
Feb 3, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 46 additions & 0 deletions lib/only_connect.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package vole

import (
"context"
"fmt"
"time"

"github.com/libp2p/go-libp2p/core/peer"
"github.com/libp2p/go-libp2p/core/peerstore"
"github.com/libp2p/go-libp2p/p2p/protocol/ping"
)

func OnlyConnect(ctx context.Context, p *peer.AddrInfo) error {
h, err := libp2pHost()
if err != nil {
return err
}
defer h.Close()

start := time.Now()
h.Peerstore().AddAddrs(p.ID, p.Addrs, peerstore.TempAddrTTL)
_, err = h.Network().DialPeer(ctx, p.ID)
if err != nil {
return err
}
connectTime := time.Since(start)
fmt.Println("Connect took:", connectTime)

pingService := ping.NewPingService(h)
ctx, cancel := context.WithCancel(ctx)
defer cancel()
resCh := pingService.Ping(ctx, p.ID)
var avg time.Duration
trials := 3
for i := 0; i < trials; i++ {
res := <-resCh
if res.Error != nil {
return res.Error
}
avg += res.RTT
}
avg /= time.Duration(trials)
fmt.Println("Average RTT:", avg)
fmt.Println("Number of roundtrips to Connect:", float64(connectTime)/float64(avg))
return nil
}
17 changes: 17 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -366,6 +366,23 @@ Note: may not work with some transports such as p2p-circuit (not applicable) and
}
return vole.Ping(c.Context, c.Bool("force-relay"), ai)
},
}, {
Name: "connect",
ArgsUsage: "<multiaddr>",
Flags: []cli.Flag{},
Usage: "connect to a peer",
Description: "connects to the target address and pings",
Action: func(c *cli.Context) error {
if c.NArg() != 1 {
return fmt.Errorf("invalid number of arguments")
}
maStr := c.Args().First()
ai, err := peer.AddrInfoFromString(maStr)
if err != nil {
return err
}
return vole.OnlyConnect(c.Context, ai)
},
},
},
},
Expand Down
Loading