Browse Source

:gear: Switch to new mdns, but keep legacy around

Ettore Di Giacinto 3 years ago
parent
commit
cdb87678b6
1 changed files with 16 additions and 12 deletions
  1. 16 12
      pkg/discovery/mdns.go

+ 16 - 12
pkg/discovery/mdns.go

@@ -17,6 +17,7 @@ package discovery
 
 import (
 	"context"
+	"os"
 	"time"
 
 	"github.com/ipfs/go-log"
@@ -24,7 +25,8 @@ import (
 
 	"github.com/libp2p/go-libp2p-core/host"
 	"github.com/libp2p/go-libp2p-core/peer"
-	mdns "github.com/libp2p/go-libp2p/p2p/discovery/mdns_legacy"
+	"github.com/libp2p/go-libp2p/p2p/discovery/mdns"
+	mdnsLegacy "github.com/libp2p/go-libp2p/p2p/discovery/mdns_legacy"
 )
 
 type MDNS struct {
@@ -54,19 +56,21 @@ func (d *MDNS) Option(ctx context.Context) func(c *libp2p.Config) error {
 
 func (d *MDNS) Run(l log.StandardLogger, ctx context.Context, host host.Host) error {
 	// setup mDNS discovery to find local peers
-	// XXX: Valid for new mdns
-	// disc := mdns.NewMdnsService(host, d.DiscoveryServiceTag, &discoveryNotifee{h: host, c: l})
-	// return disc.Start()
-	// We stick to legacy atm as mdns 0.15 is kinda of broken
-	// see: https://github.com/libp2p/go-libp2p/pull/1192
-	disc, err := mdns.NewMdnsService(ctx, host, time.Hour, d.DiscoveryServiceTag)
-	if err != nil {
-		return err
-	}
+	if os.Getenv("MDNS_LEGACY") == "true" {
+		// We stick to legacy atm as mdns 0.15 is kinda of broken
+		// see: https://github.com/libp2p/go-libp2p/pull/1192
+		disc, err := mdnsLegacy.NewMdnsService(ctx, host, time.Hour, d.DiscoveryServiceTag)
+		if err != nil {
+			return err
+		}
 
-	n := &discoveryNotifee{h: host, c: l}
+		n := &discoveryNotifee{h: host, c: l}
 
-	disc.RegisterNotifee(n)
+		disc.RegisterNotifee(n)
+	} else {
+		disc := mdns.NewMdnsService(host, d.DiscoveryServiceTag, &discoveryNotifee{h: host, c: l})
+		return disc.Start()
+	}
 
 	return nil
 }