Browse Source

dht: connect in goroutine first to avoid blocking startup

Ettore Di Giacinto 4 years ago
parent
commit
c68f3c2c54
1 changed files with 12 additions and 9 deletions
  1. 12 9
      pkg/discovery/dht.go

+ 12 - 9
pkg/discovery/dht.go

@@ -84,11 +84,20 @@ func (d *DHT) Run(c *zap.Logger, ctx context.Context, host host.Host) error {
 		return err
 	}
 
-	d.bootstrapPeers(c, ctx, host)
-	d.announceAndConnect(ctx, kademliaDHT, host, d.Rendezvous())
+	connect := func() {
+		d.bootstrapPeers(c, ctx, host)
+		if d.latestRendezvous != "" {
+			d.announceAndConnect(ctx, kademliaDHT, host, d.latestRendezvous)
+		}
+
+		rv := d.Rendezvous()
+		d.announceAndConnect(ctx, kademliaDHT, host, rv)
+	}
 
 	go func() {
 
+		connect()
+
 		t := jitterbug.New(
 			time.Second*time.Duration(d.RefreshDiscoveryTime),
 			&jitterbug.Norm{Stdev: time.Second * 10},
@@ -97,13 +106,7 @@ func (d *DHT) Run(c *zap.Logger, ctx context.Context, host host.Host) error {
 		for {
 			select {
 			case <-t.C:
-				d.bootstrapPeers(c, ctx, host)
-				if d.latestRendezvous != "" {
-					d.announceAndConnect(ctx, kademliaDHT, host, d.latestRendezvous)
-				}
-
-				rv := d.Rendezvous()
-				d.announceAndConnect(ctx, kademliaDHT, host, rv)
+				connect()
 
 			case <-ctx.Done():
 				return