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

Revert "Remove signal bootstrapping" #227

Merged
merged 1 commit into from
Jan 23, 2019
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
21 changes: 21 additions & 0 deletions dht_bootstrap.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (

u "github.com/ipfs/go-ipfs-util"
goprocess "github.com/jbenet/goprocess"
periodicproc "github.com/jbenet/goprocess/periodic"
peer "github.com/libp2p/go-libp2p-peer"
routing "github.com/libp2p/go-libp2p-routing"
)
Expand Down Expand Up @@ -89,6 +90,26 @@ func (dht *IpfsDHT) BootstrapWithConfig(cfg BootstrapConfig) (goprocess.Process,
return proc, nil
}

// SignalBootstrap ensures the dht routing table remains healthy as peers come and go.
// it builds up a list of peers by requesting random peer IDs. The Bootstrap
// process will run a number of queries each time, and run every time signal fires.
// These parameters are configurable.
//
// SignalBootstrap returns a process, so the user can stop it.
func (dht *IpfsDHT) BootstrapOnSignal(cfg BootstrapConfig, signal <-chan time.Time) (goprocess.Process, error) {
if cfg.Queries <= 0 {
return nil, fmt.Errorf("invalid number of queries: %d", cfg.Queries)
}

if signal == nil {
return nil, fmt.Errorf("invalid signal: %v", signal)
}

proc := periodicproc.Ticker(signal, dht.bootstrapWorker(cfg))

return proc, nil
}

func (dht *IpfsDHT) bootstrapWorker(cfg BootstrapConfig) func(worker goprocess.Process) {
return func(worker goprocess.Process) {
// it would be useful to be able to send out signals of when we bootstrap, too...
Expand Down
21 changes: 16 additions & 5 deletions dht_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -679,10 +679,23 @@ func TestPeriodicBootstrap(t *testing.T) {
}
}()

signals := []chan time.Time{}

var cfg BootstrapConfig
cfg = DefaultBootstrapConfig
cfg.Queries = 5

// kick off periodic bootstrappers with instrumented signals.
for _, dht := range dhts {
s := make(chan time.Time)
signals = append(signals, s)
proc, err := dht.BootstrapOnSignal(cfg, s)
if err != nil {
t.Fatal(err)
}
defer proc.Close()
}

t.Logf("dhts are not connected. %d", nDHTs)
for _, dht := range dhts {
rtlen := dht.routingTable.Size()
Expand All @@ -708,11 +721,9 @@ func TestPeriodicBootstrap(t *testing.T) {
}

t.Logf("bootstrapping them so they find each other. %d", nDHTs)
for _, dht := range dhts {
_, err := dht.BootstrapWithConfig(cfg)
if err != nil {
t.Fatalf("error bootstrapping a dht: %s", err)
}
now := time.Now()
for _, signal := range signals {
go func(s chan time.Time) { s <- now }(signal)
}

// this is async, and we dont know when it's finished with one cycle, so keep checking
Expand Down