Skip to content

[baseserver] Dedicated debug server #9742

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

Merged
merged 2 commits into from
May 13, 2022
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
22 changes: 15 additions & 7 deletions components/common-go/baseserver/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ type config struct {

// hostname is the hostname on which our servers will listen.
hostname string
// debugPort is the port we listen on for metrics, pprof, readiness and livenss checks
debugPort int
// grpcPort is the port we listen on for gRPC traffic
grpcPort int
// httpPort is the port we listen on for HTTP traffic
Expand All @@ -40,8 +42,9 @@ func defaultConfig() *config {
return &config{
logger: log.New(),
hostname: "localhost",
httpPort: 9000,
grpcPort: 9001,
httpPort: -1, // disabled by default
grpcPort: -1, // disabled by default
debugPort: 9500,
closeTimeout: 5 * time.Second,
healthHandler: healthcheck.NewHandler(),
metricsRegistry: prometheus.NewRegistry(),
Expand All @@ -58,24 +61,29 @@ func WithHostname(hostname string) Option {
}
}

// WithHTTPPort sets the port to use for an HTTP server. Setting WithHTTPPort also enables an HTTP server on the baseserver.
func WithHTTPPort(port int) Option {
return func(cfg *config) error {
if port < 0 {
return fmt.Errorf("http must not be negative, got: %d", port)
}

cfg.httpPort = port
return nil
}
}

// WithGRPCPort sets the port to use for an HTTP server. Setting WithGRPCPort also enables a gRPC server on the baseserver.
func WithGRPCPort(port int) Option {
return func(cfg *config) error {
cfg.grpcPort = port
return nil
}
}

func WithDebugPort(port int) Option {
return func(cfg *config) error {
if port < 0 {
return fmt.Errorf("grpc port must not be negative, got: %d", port)
}

cfg.grpcPort = port
cfg.debugPort = port
return nil
}
}
Expand Down
64 changes: 47 additions & 17 deletions components/common-go/baseserver/options_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ func TestOptions(t *testing.T) {
logger := log.New()
httpPort := 8080
grpcPort := 8081
debugPort := 8082
timeout := 10 * time.Second
hostname := "another_hostname"
registry := prometheus.NewRegistry()
Expand All @@ -26,6 +27,7 @@ func TestOptions(t *testing.T) {

var opts = []Option{
WithHostname(hostname),
WithDebugPort(debugPort),
WithHTTPPort(httpPort),
WithGRPCPort(grpcPort),
WithLogger(logger),
Expand All @@ -42,35 +44,63 @@ func TestOptions(t *testing.T) {
hostname: hostname,
grpcPort: grpcPort,
httpPort: httpPort,
debugPort: debugPort,
closeTimeout: timeout,
metricsRegistry: registry,
healthHandler: health,
grpcHealthCheck: grpcHealthService,
}, cfg)
}

func TestWithTTPPort(t *testing.T) {
t.Run("negative", func(t *testing.T) {
_, err := evaluateOptions(defaultConfig(), WithHTTPPort(-1))
require.Error(t, err)
})

t.Run("zero", func(t *testing.T) {
_, err := evaluateOptions(defaultConfig(), WithHTTPPort(0))
func TestWithHTTPPort(t *testing.T) {
for _, scenario := range []struct {
Port int
Expected int
}{
{Port: -1, Expected: -1},
{Port: 0, Expected: 0},
{Port: 9000, Expected: 9000},
} {
cfg, err := evaluateOptions(defaultConfig(), WithHTTPPort(scenario.Port))
require.NoError(t, err)
})
require.Equal(t, scenario.Expected, cfg.httpPort)
}
}

func TestWithGRPCPort(t *testing.T) {
t.Run("negative", func(t *testing.T) {
_, err := evaluateOptions(defaultConfig(), WithGRPCPort(-1))
require.Error(t, err)
})

t.Run("zero", func(t *testing.T) {
_, err := evaluateOptions(defaultConfig(), WithGRPCPort(0))
for _, scenario := range []struct {
Port int
Expected int
}{
{Port: -1, Expected: -1},
{Port: 0, Expected: 0},
{Port: 9000, Expected: 9000},
} {
cfg, err := evaluateOptions(defaultConfig(), WithGRPCPort(scenario.Port))
require.NoError(t, err)
})
require.Equal(t, scenario.Expected, cfg.grpcPort)
}
}

func TestWithDebugPort(t *testing.T) {
for _, scenario := range []struct {
Port int

Errors bool
Expected int
}{
{Port: -1, Errors: true},
{Port: 0, Expected: 0},
{Port: 9000, Expected: 9000},
} {
cfg, err := evaluateOptions(defaultConfig(), WithDebugPort(scenario.Port))
if scenario.Errors {
require.Error(t, err)
continue
}

require.Equal(t, scenario.Expected, cfg.debugPort)
}
}

func TestLogger_ErrorsWithNilLogger(t *testing.T) {
Expand Down
Loading