Skip to content

Commit

Permalink
feat: add accelerated-dht flag
Browse files Browse the repository at this point in the history
  • Loading branch information
hacdias committed Apr 4, 2024
1 parent f5a7295 commit b83765f
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 23 deletions.
43 changes: 29 additions & 14 deletions daemon.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,17 +20,23 @@ import (
record "github.com/libp2p/go-libp2p-record"
"github.com/libp2p/go-libp2p/core/host"
"github.com/libp2p/go-libp2p/core/peer"
"github.com/libp2p/go-libp2p/core/routing"
"github.com/libp2p/go-libp2p/p2p/net/connmgr"
"github.com/multiformats/go-multiaddr"
)

type kademlia interface {
routing.Routing
GetClosestPeers(ctx context.Context, key string) ([]peer.ID, error)
}

type daemon struct {
h host.Host
dht *fullrt.FullRT
dht kademlia
dhtMessenger *dhtpb.ProtocolMessenger
}

func newDaemon() (*daemon, error) {
func newDaemon(ctx context.Context, acceleratedDHT bool) (*daemon, error) {
rm, err := NewResourceManager()
if err != nil {
return nil, err
Expand All @@ -50,16 +56,22 @@ func newDaemon() (*daemon, error) {
return nil, err
}

d, err := fullrt.NewFullRT(h, "/ipfs",
fullrt.DHTOption(
dht.BucketSize(20),
dht.Validator(record.NamespacedValidator{
"pk": record.PublicKeyValidator{},
"ipns": ipns.Validator{},
}),
dht.BootstrapPeers(dht.GetDefaultBootstrapPeerAddrInfos()...),
dht.Mode(dht.ModeClient),
))
var d kademlia
if acceleratedDHT {
d, err = fullrt.NewFullRT(h, "/ipfs",
fullrt.DHTOption(
dht.BucketSize(20),
dht.Validator(record.NamespacedValidator{
"pk": record.PublicKeyValidator{},
"ipns": ipns.Validator{},
}),
dht.BootstrapPeers(dht.GetDefaultBootstrapPeerAddrInfos()...),
dht.Mode(dht.ModeClient),
))

} else {
d, err = dht.New(ctx, h, dht.Mode(dht.ModeClient), dht.BootstrapPeers(dht.GetDefaultBootstrapPeerAddrInfos()...))
}

if err != nil {
return nil, err
Expand All @@ -75,9 +87,12 @@ func newDaemon() (*daemon, error) {

func (d *daemon) mustStart() {
// Wait for the DHT to be ready
for !d.dht.Ready() {
time.Sleep(time.Second * 10)
if frt, ok := d.dht.(*fullrt.FullRT); ok {
for !frt.Ready() {
time.Sleep(time.Second * 10)
}
}

}

func (d *daemon) runCheck(writer http.ResponseWriter, uristr string) error {
Expand Down
16 changes: 7 additions & 9 deletions main.go
Original file line number Diff line number Diff line change
@@ -1,22 +1,14 @@
package main

import (
"context"
"log"
"net"
"net/http"
"os"

"github.com/libp2p/go-libp2p/core/peer"
"github.com/libp2p/go-libp2p/core/routing"
"github.com/urfave/cli/v2"
)

type kademlia interface {
routing.Routing
GetClosestPeers(ctx context.Context, key string) ([]peer.ID, error)
}

func main() {
app := cli.NewApp()
app.Name = "ipfs-check"
Expand All @@ -28,9 +20,15 @@ func main() {
Usage: "address to run on",
EnvVars: []string{"IPFS_CHECK_ADDRESS"},
},
&cli.BoolFlag{
Name: "accelerated-dht",
Value: true,
EnvVars: []string{"IPFS_CHECK_ACCELERATED_DHT"},
Usage: "run the accelerated DHT client",
},
}
app.Action = func(ctx *cli.Context) error {
daemon, err := newDaemon()
daemon, err := newDaemon(ctx.Context, ctx.Bool("accelerated-dht"))
if err != nil {
return err
}
Expand Down

0 comments on commit b83765f

Please sign in to comment.