Files

38 lines
962 B
Go

package cluster
import (
"context"
"fmt"
"time"
"git.clan.lol/clan/data-mesher/pkg/config"
dht "github.com/libp2p/go-libp2p-kad-dht"
"github.com/libp2p/go-libp2p/core/host"
)
const (
handlerPrefix = "/clan/data-mesher"
dthRoutingTableRefreshPeriod = 30 * time.Second
)
func NewDHT(ctx context.Context, h host.Host, cfg *config.Cluster) (*dht.IpfsDHT, error) {
opts := []dht.Option{
dht.Mode(dht.ModeServer),
dht.ProtocolPrefix(handlerPrefix),
// reduce the refresh period from the default of 10 minutes to speed up detecting bad peers in the routing table
// 10 minutes is for large IPFS style clusters, for tens of nodes this is fine
// todo make configurable eventually
dht.RoutingTableRefreshPeriod(dthRoutingTableRefreshPeriod),
dht.BootstrapPeers(cfg.BootstrapPeers...),
}
kdht, err := dht.New(ctx, h, opts...)
if err != nil {
return nil, fmt.Errorf("failed to create DHT: %w", err)
}
return kdht, nil
}