Skip to content

Commit 71b2b02

Browse files
committed
[baseserver] Dedicated debug server
1 parent efe297d commit 71b2b02

File tree

8 files changed

+264
-90
lines changed

8 files changed

+264
-90
lines changed

components/common-go/baseserver/options.go

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ type config struct {
2020

2121
// hostname is the hostname on which our servers will listen.
2222
hostname string
23+
// debugPort is the port we listen on for metrics, pprof, readiness and livenss checks
24+
debugPort int
2325
// grpcPort is the port we listen on for gRPC traffic
2426
grpcPort int
2527
// httpPort is the port we listen on for HTTP traffic
@@ -40,8 +42,9 @@ func defaultConfig() *config {
4042
return &config{
4143
logger: log.New(),
4244
hostname: "localhost",
43-
httpPort: 9000,
44-
grpcPort: 9001,
45+
httpPort: -1, // disabled by default
46+
grpcPort: -1, // disabled by default
47+
debugPort: 9500,
4548
closeTimeout: 5 * time.Second,
4649
healthHandler: healthcheck.NewHandler(),
4750
metricsRegistry: prometheus.NewRegistry(),
@@ -58,24 +61,29 @@ func WithHostname(hostname string) Option {
5861
}
5962
}
6063

64+
// WithHTTPPort sets the port to use for an HTTP server. Setting WithHTTPPort also enables an HTTP server on the baseserver.
6165
func WithHTTPPort(port int) Option {
6266
return func(cfg *config) error {
63-
if port < 0 {
64-
return fmt.Errorf("http must not be negative, got: %d", port)
65-
}
66-
6767
cfg.httpPort = port
6868
return nil
6969
}
7070
}
7171

72+
// WithGRPCPort sets the port to use for an HTTP server. Setting WithGRPCPort also enables a gRPC server on the baseserver.
7273
func WithGRPCPort(port int) Option {
74+
return func(cfg *config) error {
75+
cfg.grpcPort = port
76+
return nil
77+
}
78+
}
79+
80+
func WithDebugPort(port int) Option {
7381
return func(cfg *config) error {
7482
if port < 0 {
7583
return fmt.Errorf("grpc port must not be negative, got: %d", port)
7684
}
7785

78-
cfg.grpcPort = port
86+
cfg.debugPort = port
7987
return nil
8088
}
8189
}

components/common-go/baseserver/options_test.go

Lines changed: 47 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ func TestOptions(t *testing.T) {
1818
logger := log.New()
1919
httpPort := 8080
2020
grpcPort := 8081
21+
debugPort := 8082
2122
timeout := 10 * time.Second
2223
hostname := "another_hostname"
2324
registry := prometheus.NewRegistry()
@@ -26,6 +27,7 @@ func TestOptions(t *testing.T) {
2627

2728
var opts = []Option{
2829
WithHostname(hostname),
30+
WithDebugPort(debugPort),
2931
WithHTTPPort(httpPort),
3032
WithGRPCPort(grpcPort),
3133
WithLogger(logger),
@@ -42,35 +44,63 @@ func TestOptions(t *testing.T) {
4244
hostname: hostname,
4345
grpcPort: grpcPort,
4446
httpPort: httpPort,
47+
debugPort: debugPort,
4548
closeTimeout: timeout,
4649
metricsRegistry: registry,
4750
healthHandler: health,
4851
grpcHealthCheck: grpcHealthService,
4952
}, cfg)
5053
}
5154

52-
func TestWithTTPPort(t *testing.T) {
53-
t.Run("negative", func(t *testing.T) {
54-
_, err := evaluateOptions(defaultConfig(), WithHTTPPort(-1))
55-
require.Error(t, err)
56-
})
57-
58-
t.Run("zero", func(t *testing.T) {
59-
_, err := evaluateOptions(defaultConfig(), WithHTTPPort(0))
55+
func TestWithHTTPPort(t *testing.T) {
56+
for _, scenario := range []struct {
57+
Port int
58+
Expected int
59+
}{
60+
{Port: -1, Expected: -1},
61+
{Port: 0, Expected: 0},
62+
{Port: 9000, Expected: 9000},
63+
} {
64+
cfg, err := evaluateOptions(defaultConfig(), WithHTTPPort(scenario.Port))
6065
require.NoError(t, err)
61-
})
66+
require.Equal(t, scenario.Expected, cfg.httpPort)
67+
}
6268
}
6369

6470
func TestWithGRPCPort(t *testing.T) {
65-
t.Run("negative", func(t *testing.T) {
66-
_, err := evaluateOptions(defaultConfig(), WithGRPCPort(-1))
67-
require.Error(t, err)
68-
})
69-
70-
t.Run("zero", func(t *testing.T) {
71-
_, err := evaluateOptions(defaultConfig(), WithGRPCPort(0))
71+
for _, scenario := range []struct {
72+
Port int
73+
Expected int
74+
}{
75+
{Port: -1, Expected: -1},
76+
{Port: 0, Expected: 0},
77+
{Port: 9000, Expected: 9000},
78+
} {
79+
cfg, err := evaluateOptions(defaultConfig(), WithGRPCPort(scenario.Port))
7280
require.NoError(t, err)
73-
})
81+
require.Equal(t, scenario.Expected, cfg.grpcPort)
82+
}
83+
}
84+
85+
func TestWithDebugPort(t *testing.T) {
86+
for _, scenario := range []struct {
87+
Port int
88+
89+
Errors bool
90+
Expected int
91+
}{
92+
{Port: -1, Errors: true},
93+
{Port: 0, Expected: 0},
94+
{Port: 9000, Expected: 9000},
95+
} {
96+
cfg, err := evaluateOptions(defaultConfig(), WithDebugPort(scenario.Port))
97+
if scenario.Errors {
98+
require.Error(t, err)
99+
continue
100+
}
101+
102+
require.Equal(t, scenario.Expected, cfg.debugPort)
103+
}
74104
}
75105

76106
func TestLogger_ErrorsWithNilLogger(t *testing.T) {

0 commit comments

Comments
 (0)