Skip to content

Commit

Permalink
Add start stop mutex
Browse files Browse the repository at this point in the history
  • Loading branch information
qdm12 committed Jan 2, 2023
1 parent bbc03be commit b093f4e
Showing 1 changed file with 12 additions and 3 deletions.
15 changes: 12 additions & 3 deletions internal/mdns/mdns.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"errors"
"fmt"
"net"
"sync"
"time"

"github.com/libp2p/go-libp2p-core/peer"
Expand Down Expand Up @@ -36,9 +37,11 @@ type Service struct {
server *mdns.Server

// Internal service management fields.
started bool
stop chan struct{}
done chan struct{}
// startStopMutex is to prevent concurrent calls to Start and Stop.
startStopMutex sync.Mutex
started bool
stop chan struct{}
done chan struct{}
}

// NewService creates and returns a new mDNS service.
Expand All @@ -59,6 +62,9 @@ func NewService(p2pHost IDNetworker, serviceTag string,

// Start starts the mDNS service.
func (s *Service) Start() (err error) {
s.startStopMutex.Lock()
defer s.startStopMutex.Unlock()

if s.started {
return nil
}
Expand Down Expand Up @@ -96,6 +102,9 @@ func (s *Service) Start() (err error) {

// Stop stops the mDNS service and server.
func (s *Service) Stop() (err error) {
s.startStopMutex.Lock()
defer s.startStopMutex.Unlock()

if !s.started {
return nil
}
Expand Down

0 comments on commit b093f4e

Please sign in to comment.