Commit 19da02b9 authored by ale's avatar ale

Properly stop all services on error, and improve logging

parent 49d17654
......@@ -203,16 +203,22 @@ func ednsFromRequest(req, m *dns.Msg) {
// Wrapper to make the dns.Server match the genericServer interface.
type dnsServer struct {
*dns.Server
name string
}
func newDNSServer(addr, proto string, h dns.Handler) *dnsServer {
return &dnsServer{&dns.Server{
Addr: addr,
Net: proto,
Handler: h,
}}
func newDNSServer(name, addr, proto string, h dns.Handler) *dnsServer {
return &dnsServer{
Server: &dns.Server{
Addr: addr,
Net: proto,
Handler: h,
},
name: name,
}
}
func (s *dnsServer) Name() string { return s.name }
func (s *dnsServer) Serve() error {
return s.Server.ListenAndServe()
}
......
......@@ -246,19 +246,25 @@ func withLocalhost(h http.Handler) http.Handler {
// Wrapper to make an http.Server match the genericServer interface.
type httpServer struct {
*http.Server
name string
}
func newHTTPServer(addr string, h http.Handler) *httpServer {
return &httpServer{&http.Server{
Addr: addr,
Handler: h,
ReadTimeout: 10 * time.Second,
ReadHeaderTimeout: 3 * time.Second,
WriteTimeout: 10 * time.Second,
IdleTimeout: 30 * time.Second,
}}
func newHTTPServer(name, addr string, h http.Handler) *httpServer {
return &httpServer{
Server: &http.Server{
Addr: addr,
Handler: h,
ReadTimeout: 10 * time.Second,
ReadHeaderTimeout: 3 * time.Second,
WriteTimeout: 10 * time.Second,
IdleTimeout: 30 * time.Second,
},
name: name,
}
}
func (s *httpServer) Name() string { return s.name }
func (s *httpServer) Serve() error {
err := s.Server.ListenAndServe()
if err == http.ErrServerClosed {
......
......@@ -2,6 +2,7 @@ package node
import (
"fmt"
"log"
"net"
"strconv"
"sync"
......@@ -9,6 +10,7 @@ import (
// A genericServer is just something that can be started and stopped.
type genericServer interface {
Name() string
Serve() error
Stop()
}
......@@ -33,10 +35,14 @@ func buildServer(servers ...genericServer) *Server {
ms.wg.Add(1)
go func(s genericServer) {
defer ms.wg.Done()
log.Printf("starting network service: %s", s.Name())
err := s.Serve()
if err != nil {
werr := fmt.Errorf("%s: %v", s.Name(), err)
select {
case ms.errCh <- err:
case ms.errCh <- werr:
// First error, stop all other services.
ms.Stop()
default:
}
}
......@@ -78,13 +84,13 @@ func NewServer(n *Node, domain string, nameservers []string, publicAddrs []net.I
servers := []genericServer{
newStatusServer(mkaddr(peerAddr, gossipPort), n.statusMgr),
newHTTPServer(fmt.Sprintf(":%d", metricsPort), newMetricsHandler()),
newHTTPServer("metrics", fmt.Sprintf(":%d", metricsPort), newMetricsHandler()),
}
for _, ip := range publicAddrs {
servers = append(servers,
newHTTPServer(mkaddr(ip, httpPort), httpHandler),
newDNSServer(mkaddr(ip, dnsPort), "udp", dnsHandler),
newDNSServer(mkaddr(ip, dnsPort), "tcp", dnsHandler),
newHTTPServer("main", mkaddr(ip, httpPort), httpHandler),
newDNSServer("dns(udp)", mkaddr(ip, dnsPort), "udp", dnsHandler),
newDNSServer("dns(tcp)", mkaddr(ip, dnsPort), "tcp", dnsHandler),
)
}
return buildServer(servers...)
......
......@@ -185,6 +185,8 @@ func newStatusServer(addr string, statusMgr *statusManager) *statusServer {
}
}
func (s *statusServer) Name() string { return "status" }
func (s *statusServer) Serve() error {
l, err := net.Listen("tcp", s.addr)
if err != nil {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment