Skip to content

Commit

Permalink
since ip change events are fired by 5sec schedulers , no need to wait.
Browse files Browse the repository at this point in the history
  • Loading branch information
MOHANKUMAR-IT committed Dec 10, 2024
1 parent e73024f commit b1fdc89
Showing 1 changed file with 10 additions and 17 deletions.
27 changes: 10 additions & 17 deletions p2p/discovery/mdns/mdns.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,12 @@ import (
"context"
"errors"
"github.com/libp2p/go-libp2p/core/event"
"github.com/libp2p/go-libp2p/core/host"
"github.com/libp2p/go-libp2p/core/peer"
"io"
"math/rand"
"strings"
"sync"
"time"

"github.com/libp2p/go-libp2p/core/host"
"github.com/libp2p/go-libp2p/core/peer"

"github.com/libp2p/zeroconf/v2"

Expand Down Expand Up @@ -85,25 +83,20 @@ func (s *mdnsService) Start() error {
log.Errorf("failed to close ipEvt: %s", err)
}
}()
var addrUpdateDebounce sync.Once

for {
select {
case <-s.ctx.Done():
return
case evt := <-ipEvt.Out():
if _, ok := evt.(event.EvtLocalAddressesUpdated); ok {
addrUpdateDebounce.Do(func() {
time.AfterFunc(2*time.Second, func() {
addrUpdateDebounce = sync.Once{}
if s.server != nil {
s.server.Shutdown()
s.server = nil
}
if err = s.startServer(); err != nil {
log.Errorf("failed to restart mdns server: %s", err)
}
})
})
if s.server != nil {
s.server.Shutdown()
s.server = nil
}
if err = s.startServer(); err != nil {
log.Errorf("failed to restart mdns server: %s", err)
}
}
}
}
Expand Down

0 comments on commit b1fdc89

Please sign in to comment.