Fix failing on startup

When we cannot bind to the port, return an error which will make
CoreDNS fail startup. Still a bit hackish, but good enough.
This commit is contained in:
Miek Gieben 2016-03-19 19:38:54 +00:00
parent 9d14d5fcf0
commit 33f00b5a3a

View file

@ -134,36 +134,21 @@ func (s *Server) Serve(ln ListenerFile) error {
// ListenAndServe starts the server with a new listener. It blocks until the server stops. // ListenAndServe starts the server with a new listener. It blocks until the server stops.
func (s *Server) ListenAndServe() error { func (s *Server) ListenAndServe() error {
err := s.setup() err := s.setup()
once := sync.Once{}
if err != nil { if err != nil {
close(s.startChan) close(s.startChan)
return err return err
} }
// TODO(miek): redo to make it more like caddy // TODO(miek): going out on a limb here, let's assume that listening
// - error handling, re-introduce what Caddy did. // on the part for tcp and udp results in the same error. We can only
// return the error from the udp listener, disregarding whatever
// happenend to the tcp one.
go func() { go func() {
if err := dns.ListenAndServe(s.Addr, "tcp", s.mux); err != nil { dns.ListenAndServe(s.Addr, "tcp", s.mux)
log.Printf("[ERROR] %v\n", err)
defer once.Do(func() { close(s.startChan) })
return
}
}() }()
go func() { close(s.startChan) // unblock anyone waiting for this to start listening
if err := dns.ListenAndServe(s.Addr, "udp", s.mux); err != nil { return dns.ListenAndServe(s.Addr, "udp", s.mux)
log.Printf("[ERROR] %v\n", err)
defer once.Do(func() { close(s.startChan) })
return
}
}()
once.Do(func() { close(s.startChan) }) // unblock anyone waiting for this to start listening
// but block here, as this is what caddy expects
for {
select {}
}
return nil
} }
// setup prepares the server s to begin listening; it should be // setup prepares the server s to begin listening; it should be
@ -315,6 +300,7 @@ func (s *Server) ServeDNS(w dns.ResponseWriter, r *dns.Msg) {
// Wildcard match, if we have found nothing try the root zone as a last resort. // Wildcard match, if we have found nothing try the root zone as a last resort.
if h, ok := s.zones["."]; ok { if h, ok := s.zones["."]; ok {
rcode, _ := h.stack.ServeDNS(ctx, w, r) rcode, _ := h.stack.ServeDNS(ctx, w, r)
// TODO(miek): Double check if we have written something.
if rcode > 0 { if rcode > 0 {
DefaultErrorFunc(w, r, rcode) DefaultErrorFunc(w, r, rcode)
} }