servers/servers.go
func (s *Servers) Serve() (err error) {
	lns := make([]net.Listener, len(s.servers))
	conns := make([]*net.UDPConn, len(s.servers))
	for i, srv := range s.servers {
		if _, yes := srv.isTCP(); yes {
			ln, err := net.Listen("tcp", srv.address)
			if err != nil {
				for _, l := range lns {
					if l == nil {
						continue
					}
					if err := l.Close(); err != nil {
						s.logger.Errorf("%s tcp listener %q close: %v", srv.label(), srv.address, err)
					}
				}
				return fmt.Errorf("%s tcp listener %q: %v", srv.label(), srv.address, err)
			}
			lns[i] = ln
		}
		if _, yes := srv.isUDP(); yes {
			addr, err := net.ResolveUDPAddr("udp", srv.address)
			if err != nil {
				return fmt.Errorf("%s resolve udp address %q: %v", srv.label(), srv.address, err)
			}
			conn, err := net.ListenUDP("udp", addr)
			if err != nil {
				return fmt.Errorf("%s udp listener %q: %v", srv.label(), srv.address, err)
			}
			conns[i] = conn
		}
	}
	for i, srv := range s.servers {
		if tcpSrv, yes := srv.isTCP(); yes {
			go func(srv *server, ln net.Listener) {
				defer s.recover()

				s.mu.Lock()
				srv.tcpAddr = ln.Addr().(*net.TCPAddr)
				s.mu.Unlock()

				s.logger.Infof("%s listening on %q", srv.label(), srv.tcpAddr.String())
				if err := tcpSrv.ServeTCP(ln); err != nil {
					s.logger.Errorf("%s serve %q: %v", srv.label(), srv.tcpAddr.String(), err)
				}
			}(srv, lns[i])
		}
		if udpSrv, yes := srv.isUDP(); yes {
			go func(srv *server, conn *net.UDPConn) {
				defer s.recover()

				s.mu.Lock()
				srv.udpAddr = conn.LocalAddr().(*net.UDPAddr)
				s.mu.Unlock()

				s.logger.Infof("%s listening on %q", srv.label(), srv.tcpAddr.String())
				if err := udpSrv.ServeUDP(conn); err != nil {
					s.logger.Errorf("%s serve %q: %v", srv.label(), srv.tcpAddr.String(), err)
				}
			}(srv, conns[i])
		}
	}
	return nil
}
func (s *Servers) Serve() (err error) {
	lns := make([]net.Listener, len(s.servers))
	conns := make([]*net.UDPConn, len(s.servers))
	for i, srv := range s.servers {
		if _, yes := srv.isTCP(); yes {
			ln, err := net.Listen("tcp", srv.address)
			if err != nil {
				for _, l := range lns {
					if l == nil {
						continue
					}
					if err := l.Close(); err != nil {
						s.logger.Errorf("%s tcp listener %q close: %v", srv.label(), srv.address, err)
					}
				}
				return fmt.Errorf("%s tcp listener %q: %v", srv.label(), srv.address, err)
			}
			lns[i] = ln
		}
		if _, yes := srv.isUDP(); yes {
			addr, err := net.ResolveUDPAddr("udp", srv.address)
			if err != nil {
				return fmt.Errorf("%s resolve udp address %q: %v", srv.label(), srv.address, err)
			}
			conn, err := net.ListenUDP("udp", addr)
			if err != nil {
				return fmt.Errorf("%s udp listener %q: %v", srv.label(), srv.address, err)
			}
			conns[i] = conn
		}
	}
	for i, srv := range s.servers {
		if tcpSrv, yes := srv.isTCP(); yes {
			go func(srv *server, ln net.Listener) {
				defer s.recover()

				s.mu.Lock()
				srv.tcpAddr = ln.Addr().(*net.TCPAddr)
				s.mu.Unlock()

				s.logger.Infof("%s listening on %q", srv.label(), srv.tcpAddr.String())
				if err := tcpSrv.ServeTCP(ln); err != nil {
					s.logger.Errorf("%s serve %q: %v", srv.label(), srv.tcpAddr.String(), err)
				}
			}(srv, lns[i])
		}
		if udpSrv, yes := srv.isUDP(); yes {
			go func(srv *server, conn *net.UDPConn) {
				defer s.recover()

				s.mu.Lock()
				srv.udpAddr = conn.LocalAddr().(*net.UDPAddr)
				s.mu.Unlock()

				s.logger.Infof("%s listening on %q", srv.label(), srv.tcpAddr.String())
				if err := udpSrv.ServeUDP(conn); err != nil {
					s.logger.Errorf("%s serve %q: %v", srv.label(), srv.tcpAddr.String(), err)
				}
			}(srv, conns[i])
		}
	}
	return nil
}
// Serve starts all added servers.
// New new servers must be added after this methid is called.