Ensure API can listen on separate interface/port

The HTTP bind address/port config was not actually being used.
pull/2274/head
Jason Wilder 2015-04-13 15:21:56 -06:00
parent d78033bb49
commit b4d5d03a47
2 changed files with 55 additions and 11 deletions

View File

@ -218,8 +218,6 @@ func NewConfig() *Config {
c := &Config{}
c.Port = DefaultClusterPort
c.HTTPAPI.Port = DefaultClusterPort
c.Data.Enabled = DefaultDataEnabled
c.Broker.Enabled = DefaultBrokerEnabled
@ -278,11 +276,18 @@ func NewTestConfig() (*Config, error) {
// APIAddr returns the TCP binding address for the API server.
func (c *Config) APIAddr() string {
// Default to cluster bind address if not overriden
ba := c.BindAddress
if c.HTTPAPI.BindAddress != "" {
ba = c.HTTPAPI.BindAddress
}
return net.JoinHostPort(ba, strconv.Itoa(c.HTTPAPI.Port))
// Default to cluster port if not overridden
bp := c.Port
if c.HTTPAPI.Port != 0 {
bp = c.HTTPAPI.Port
}
return net.JoinHostPort(ba, strconv.Itoa(bp))
}
// APIAddrUDP returns the UDP address for the series listener.

View File

@ -47,6 +47,7 @@ type Node struct {
adminServer *admin.Server
clusterListener net.Listener // The cluster TCP listener
apiListener net.Listener // The API TCP listener
}
func (s *Node) Close() error {
@ -54,6 +55,10 @@ func (s *Node) Close() error {
return err
}
if err := s.closeAPIListener(); err != nil {
return err
}
if err := s.closeAdminServer(); err != nil {
return err
}
@ -92,16 +97,14 @@ func (s *Node) closeAdminServer() error {
return nil
}
func (s *Node) openClusterListener(addr string, h http.Handler) error {
func (s *Node) openListener(desc, addr string, h http.Handler) (net.Listener, error) {
var err error
// We want to make sure we are spun up before we exit this function, so we manually listen and serve
listener, err := net.Listen("tcp", addr)
if err != nil {
return err
return nil, err
}
s.clusterListener = listener
go func() {
err := http.Serve(s.clusterListener, h)
err := http.Serve(listener, h)
// The listener was closed so exit
// See https://github.com/golang/go/issues/4373
@ -109,9 +112,37 @@ func (s *Node) openClusterListener(addr string, h http.Handler) error {
return
}
if err != nil {
log.Fatalf("TCP server failed to server on %s: %s", addr, err)
log.Fatalf("%s server failed to serve on %s: %s", desc, addr, err)
}
}()
return listener, nil
}
func (s *Node) openAPIListener(addr string, h http.Handler) error {
var err error
s.apiListener, err = s.openListener("API", addr, h)
if err != nil {
return err
}
return nil
}
func (s *Node) closeAPIListener() error {
var err error
if s.apiListener != nil {
err = s.apiListener.Close()
s.apiListener = nil
}
return err
}
func (s *Node) openClusterListener(addr string, h http.Handler) error {
var err error
s.clusterListener, err = s.openListener("Cluster", addr, h)
if err != nil {
return err
}
return nil
}
@ -227,9 +258,9 @@ func (cmd *RunCommand) Open(config *Config, join string) *Node {
err := cmd.node.openClusterListener(cmd.config.ClusterAddr(), h)
if err != nil {
log.Fatalf("TCP server failed to listen on %s. %s ", cmd.config.ClusterAddr(), err)
log.Fatalf("Cluster server failed to listen on %s. %s ", cmd.config.ClusterAddr(), err)
}
log.Printf("TCP server listening on %s", cmd.config.ClusterAddr())
log.Printf("Cluster server listening on %s", cmd.config.ClusterAddr())
var s *influxdb.Server
// Open server, initialize or join as necessary.
@ -389,6 +420,14 @@ func (cmd *RunCommand) Open(config *Config, join string) *Node {
}
}
if cmd.config.APIAddr() != cmd.config.ClusterAddr() {
err := cmd.node.openAPIListener(cmd.config.APIAddr(), h)
if err != nil {
log.Fatalf("API server failed to listen on %s. %s ", cmd.config.APIAddr(), err)
}
}
log.Printf("API server listening on %s", cmd.config.APIAddr())
return cmd.node
}