mirror of
https://github.com/fluencelabs/go-libp2p-kad-dht
synced 2025-04-24 22:32:13 +00:00
clean up unused dht methods
License: MIT Signed-off-by: Jeromy <jeromyj@gmail.com>
This commit is contained in:
parent
0e59a511f2
commit
7887e04c8a
57
dht.go
57
dht.go
@ -4,7 +4,6 @@ package dht
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"crypto/rand"
|
||||
"errors"
|
||||
"fmt"
|
||||
"sync"
|
||||
@ -33,8 +32,6 @@ var log = eventlog.Logger("dht")
|
||||
|
||||
var ProtocolDHT protocol.ID = "/ipfs/dht"
|
||||
|
||||
const doPinging = false
|
||||
|
||||
// NumBootstrapQueries defines the number of random dht queries to do to
|
||||
// collect members of the routing table.
|
||||
const NumBootstrapQueries = 5
|
||||
@ -92,11 +89,6 @@ func NewDHT(ctx context.Context, h host.Host, dstore ds.ThreadSafeDatastore) *Ip
|
||||
dht.Validator = make(record.Validator)
|
||||
dht.Validator["pk"] = record.PublicKeyValidator
|
||||
|
||||
if doPinging {
|
||||
dht.proc.Go(func(p goprocess.Process) {
|
||||
dht.PingRoutine(time.Second * 10)
|
||||
})
|
||||
}
|
||||
return dht
|
||||
}
|
||||
|
||||
@ -110,23 +102,6 @@ func (dht *IpfsDHT) log() eventlog.EventLogger {
|
||||
return log // TODO rm
|
||||
}
|
||||
|
||||
// Connect to a new peer at the given address, ping and add to the routing table
|
||||
func (dht *IpfsDHT) Connect(ctx context.Context, npeer peer.ID) error {
|
||||
// TODO: change interface to accept a PeerInfo as well.
|
||||
if err := dht.host.Connect(ctx, peer.PeerInfo{ID: npeer}); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// Ping new peer to register in their routing table
|
||||
// NOTE: this should be done better...
|
||||
if _, err := dht.Ping(ctx, npeer); err != nil {
|
||||
return fmt.Errorf("failed to ping newly connected peer: %s", err)
|
||||
}
|
||||
log.Event(ctx, "connect", dht.self, npeer)
|
||||
dht.Update(ctx, npeer)
|
||||
return nil
|
||||
}
|
||||
|
||||
// putValueToPeer stores the given key/value pair at the peer 'p'
|
||||
func (dht *IpfsDHT) putValueToPeer(ctx context.Context, p peer.ID,
|
||||
key key.Key, rec *pb.Record) error {
|
||||
@ -343,38 +318,6 @@ func (dht *IpfsDHT) betterPeersToQuery(pmes *pb.Message, p peer.ID, count int) [
|
||||
return filtered
|
||||
}
|
||||
|
||||
func (dht *IpfsDHT) ensureConnectedToPeer(ctx context.Context, p peer.ID) error {
|
||||
if p == dht.self {
|
||||
return errors.New("attempting to ensure connection to self")
|
||||
}
|
||||
|
||||
// dial connection
|
||||
return dht.host.Connect(ctx, peer.PeerInfo{ID: p})
|
||||
}
|
||||
|
||||
// PingRoutine periodically pings nearest neighbors.
|
||||
func (dht *IpfsDHT) PingRoutine(t time.Duration) {
|
||||
tick := time.Tick(t)
|
||||
for {
|
||||
select {
|
||||
case <-tick:
|
||||
id := make([]byte, 16)
|
||||
rand.Read(id)
|
||||
peers := dht.routingTable.NearestPeers(kb.ConvertKey(key.Key(id)), 5)
|
||||
for _, p := range peers {
|
||||
ctx, cancel := context.WithTimeout(dht.Context(), time.Second*5)
|
||||
_, err := dht.Ping(ctx, p)
|
||||
if err != nil {
|
||||
log.Debugf("Ping error: %s", err)
|
||||
}
|
||||
cancel()
|
||||
}
|
||||
case <-dht.proc.Closing():
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Context return dht's context
|
||||
func (dht *IpfsDHT) Context() context.Context {
|
||||
return dht.ctx
|
||||
|
@ -74,7 +74,8 @@ func connect(t *testing.T, ctx context.Context, a, b *IpfsDHT) {
|
||||
}
|
||||
|
||||
a.peerstore.AddAddrs(idB, addrB, peer.TempAddrTTL)
|
||||
if err := a.Connect(ctx, idB); err != nil {
|
||||
pi := peer.PeerInfo{ID: idB}
|
||||
if err := a.host.Connect(ctx, pi); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
@ -789,12 +790,14 @@ func TestConnectCollision(t *testing.T) {
|
||||
errs := make(chan error)
|
||||
go func() {
|
||||
dhtA.peerstore.AddAddr(peerB, addrB, peer.TempAddrTTL)
|
||||
err := dhtA.Connect(ctx, peerB)
|
||||
pi := peer.PeerInfo{ID: peerB}
|
||||
err := dhtA.host.Connect(ctx, pi)
|
||||
errs <- err
|
||||
}()
|
||||
go func() {
|
||||
dhtB.peerstore.AddAddr(peerA, addrA, peer.TempAddrTTL)
|
||||
err := dhtB.Connect(ctx, peerA)
|
||||
pi := peer.PeerInfo{ID: peerA}
|
||||
err := dhtB.host.Connect(ctx, pi)
|
||||
errs <- err
|
||||
}()
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user