Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow user to specify HTTP and gRPC bind addresses (not just ports.) (Rebase of #153) #167

Merged
merged 3 commits into from
Nov 3, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 9 additions & 5 deletions server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,11 @@ import (

// Config for a Server
type Config struct {
MetricsNamespace string `yaml:"-"`
HTTPListenPort int `yaml:"http_listen_port"`
GRPCListenPort int `yaml:"grpc_listen_port"`
MetricsNamespace string `yaml:"-"`
HTTPListenAddress string `yaml:"http_listen_address"`
HTTPListenPort int `yaml:"http_listen_port"`
GRPCListenAddress string `yaml:"grpc_listen_address"`
GRPCListenPort int `yaml:"grpc_listen_port"`

RegisterInstrumentation bool `yaml:"register_instrumentation"`
ExcludeRequestInLog bool `yaml:"-"`
Expand All @@ -56,7 +58,9 @@ type Config struct {

// RegisterFlags adds the flags required to config this to the given FlagSet
func (cfg *Config) RegisterFlags(f *flag.FlagSet) {
f.StringVar(&cfg.HTTPListenAddress, "server.http-listen-address", "", "HTTP server listen address.")
f.IntVar(&cfg.HTTPListenPort, "server.http-listen-port", 80, "HTTP server listen port.")
f.StringVar(&cfg.GRPCListenAddress, "server.grpc-listen-address", "", "gRPC server listen address.")
f.IntVar(&cfg.GRPCListenPort, "server.grpc-listen-port", 9095, "gRPC server listen port.")
f.BoolVar(&cfg.RegisterInstrumentation, "server.register-instrumentation", true, "Register the intrumentation handlers (/metrics etc).")
f.DurationVar(&cfg.ServerGracefulShutdownTimeout, "server.graceful-shutdown-timeout", 30*time.Second, "Timeout for graceful shutdowns")
Expand Down Expand Up @@ -88,12 +92,12 @@ type Server struct {
// New makes a new Server
func New(cfg Config) (*Server, error) {
// Setup listeners first, so we can fail early if the port is in use.
httpListener, err := net.Listen("tcp", fmt.Sprintf(":%d", cfg.HTTPListenPort))
httpListener, err := net.Listen("tcp", fmt.Sprintf("%s:%d", cfg.HTTPListenAddress, cfg.HTTPListenPort))
if err != nil {
return nil, err
}

grpcListener, err := net.Listen("tcp", fmt.Sprintf(":%d", cfg.GRPCListenPort))
grpcListener, err := net.Listen("tcp", fmt.Sprintf("%s:%d", cfg.GRPCListenAddress, cfg.GRPCListenPort))
if err != nil {
return nil, err
}
Expand Down
17 changes: 11 additions & 6 deletions server/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ func (f FakeServer) Succeed(ctx context.Context, req *google_protobuf.Empty) (*g
func TestErrorInstrumentationMiddleware(t *testing.T) {
var cfg Config
cfg.RegisterFlags(flag.NewFlagSet("", flag.ExitOnError))
cfg.GRPCListenAddress = "localhost"
cfg.GRPCListenPort = 1234
server, err := New(cfg)
require.NoError(t, err)
Expand Down Expand Up @@ -102,8 +103,10 @@ func TestErrorInstrumentationMiddleware(t *testing.T) {

func TestRunReturnsError(t *testing.T) {
cfg := Config{
HTTPListenPort: 9190,
GRPCListenPort: 9191,
HTTPListenAddress: "localhost",
HTTPListenPort: 9190,
GRPCListenAddress: "localhost",
GRPCListenPort: 9191,
}
t.Run("http", func(t *testing.T) {
cfg.MetricsNamespace = "testing_http"
Expand Down Expand Up @@ -142,10 +145,12 @@ func TestMiddlewareLogging(t *testing.T) {
var level logging.Level
level.Set("info")
cfg := Config{
HTTPListenPort: 9192,
HTTPMiddleware: []middleware.Interface{middleware.Logging},
MetricsNamespace: "testing_logging",
LogLevel: level,
HTTPListenAddress: "localhost",
HTTPListenPort: 9192,
GRPCListenAddress: "localhost",
HTTPMiddleware: []middleware.Interface{middleware.Logging},
MetricsNamespace: "testing_logging",
LogLevel: level,
}
server, err := New(cfg)
require.NoError(t, err)
Expand Down