Skip to content

Commit

Permalink
feat(dht): switch to a single RefreshRoutingTable function returning …
Browse files Browse the repository at this point in the history
…a channel
  • Loading branch information
Stebalien committed Dec 10, 2019
1 parent 0be0cbc commit 09f6660
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 20 deletions.
28 changes: 9 additions & 19 deletions dht_bootstrap.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ func (dht *IpfsDHT) startRefreshing() error {
return
}

// Batch multiple refresh requests if they're all waiting at the same time.
collectWaiting:
for {
select {
Expand All @@ -83,9 +84,11 @@ func (dht *IpfsDHT) startRefreshing() error {
break collectWaiting
}
}

err := dht.doRefresh(ctx)
for _, w := range waiting {
w <- err
close(w)
}
if err != nil {
logger.Warning(err)
Expand Down Expand Up @@ -181,27 +184,14 @@ func (dht *IpfsDHT) Bootstrap(_ context.Context) error {
}

// RefreshRoutingTable tells the DHT to refresh it's routing tables.
func (dht *IpfsDHT) RefreshRoutingTable() {
select {
case dht.triggerRtRefresh <- nil:
default:
}
}

// RefreshRoutingTableWait tells the DHT to refresh it's routing tables and
// waits for it to finish.
func (dht *IpfsDHT) RefreshRoutingTableWait(ctx context.Context) error {
//
// The returned channel will block until the refresh finishes, then yield the
// error and close. The channel is buffered and safe to ignore.
func (dht *IpfsDHT) RefreshRoutingTable() <-chan error {
res := make(chan error, 1)
select {
case dht.triggerRtRefresh <- res:
case <-ctx.Done():
return ctx.Err()
}

select {
case err := <-res:
return err
case <-ctx.Done():
return ctx.Err()
default:
}
return res
}
5 changes: 4 additions & 1 deletion dht_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,10 @@ func bootstrap(t *testing.T, ctx context.Context, dhts []*IpfsDHT) {
start := rand.Intn(len(dhts)) // randomize to decrease bias.
for i := range dhts {
dht := dhts[(start+i)%len(dhts)]
dht.RefreshRoutingTableWait(ctx)
select {
case <-dht.RefreshRoutingTable():
case <-ctx.Done():
}
}
}

Expand Down

0 comments on commit 09f6660

Please sign in to comment.