Skip to content

Commit

Permalink
Serialize resolver Start and Stop
Browse files Browse the repository at this point in the history
Signed-off-by: Santhosh Manohar <santhosh@docker.com>
  • Loading branch information
Santhosh Manohar committed Nov 20, 2016
1 parent f36e733 commit c5bb4cd
Showing 1 changed file with 39 additions and 8 deletions.
47 changes: 39 additions & 8 deletions resolver.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ type extDNSEntry struct {

// resolver implements the Resolver interface
type resolver struct {
sync.Mutex
backend DNSBackend
extDNSList [maxExtDNS]extDNSEntry
server *dns.Server
Expand All @@ -86,6 +87,7 @@ type resolver struct {
queryLock sync.Mutex
listenAddress string
proxyDNS bool
startCh chan struct{}
resolverKey string
}

Expand Down Expand Up @@ -140,38 +142,67 @@ func (r *resolver) Start() error {
if r.err != nil {
return r.err
}
// startCh is to seralize resolver Start and Stop
r.Lock()
r.startCh = make(chan struct{})
r.Unlock()

if err := r.setupIPTable(); err != nil {
return fmt.Errorf("setting up IP table rules failed: %v", err)
}

s := &dns.Server{Handler: r, PacketConn: r.conn}
r.server = s
go func() {
s.ActivateAndServe()
}()

tcpServer := &dns.Server{Handler: r, Listener: r.tcpListen}
r.tcpServer = tcpServer
go func() {
tcpServer.ActivateAndServe()
}()

r.Lock()
r.server = s
r.tcpServer = tcpServer
close(r.startCh)
r.startCh = nil
r.Unlock()

return nil
}

func (r *resolver) Stop() {
if r.server != nil {
r.server.Shutdown()
}
if r.tcpServer != nil {
r.tcpServer.Shutdown()
func (r *resolver) waitForStart() {
r.Lock()
startCh := r.startCh
r.Unlock()

if startCh != nil {
<-startCh
}
}

func (r *resolver) Stop() {
r.waitForStart()

r.Lock()
udpServer := r.server
tcpServer := r.tcpServer
r.conn = nil
r.tcpListen = nil
r.server = nil
r.tcpServer = nil
r.err = fmt.Errorf("setup not done yet")
r.tStamp = time.Time{}
r.count = 0
r.queryLock = sync.Mutex{}
r.Unlock()

if udpServer != nil {
udpServer.Shutdown()
}
if tcpServer != nil {
tcpServer.Shutdown()
}
}

func (r *resolver) SetExtServers(dns []string) {
Expand Down

0 comments on commit c5bb4cd

Please sign in to comment.