Skip to content

Commit bc378ed

Browse files
committed
[baseserver] Dedicated debug server
1 parent 0ab2a8d commit bc378ed

File tree

8 files changed

+265
-92
lines changed

8 files changed

+265
-92
lines changed

components/common-go/baseserver/options.go

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

1919
// hostname is the hostname on which our servers will listen.
2020
hostname string
21+
// debugPort is the port we listen on for metrics, pprof, readiness and livenss checks
22+
debugPort int
2123
// grpcPort is the port we listen on for gRPC traffic
2224
grpcPort int
2325
// httpPort is the port we listen on for HTTP traffic
@@ -36,8 +38,9 @@ func defaultConfig() *config {
3638
return &config{
3739
logger: log.New(),
3840
hostname: "localhost",
39-
httpPort: 9000,
40-
grpcPort: 9001,
41+
httpPort: -1, // disabled by default
42+
grpcPort: -1, // disabled by default
43+
debugPort: 9500,
4144
closeTimeout: 5 * time.Second,
4245
healthHandler: healthcheck.NewHandler(),
4346
}
@@ -52,24 +55,29 @@ func WithHostname(hostname string) Option {
5255
}
5356
}
5457

58+
// WithHTTPPort sets the port to use for an HTTP server. Setting WithHTTPPort also enables an HTTP server on the baseserver.
5559
func WithHTTPPort(port int) Option {
5660
return func(cfg *config) error {
57-
if port < 0 {
58-
return fmt.Errorf("http must not be negative, got: %d", port)
59-
}
60-
6161
cfg.httpPort = port
6262
return nil
6363
}
6464
}
6565

66+
// WithGRPCPort sets the port to use for an HTTP server. Setting WithGRPCPort also enables a gRPC server on the baseserver.
6667
func WithGRPCPort(port int) Option {
68+
return func(cfg *config) error {
69+
cfg.grpcPort = port
70+
return nil
71+
}
72+
}
73+
74+
func WithDebugPort(port int) Option {
6775
return func(cfg *config) error {
6876
if port < 0 {
6977
return fmt.Errorf("grpc port must not be negative, got: %d", port)
7078
}
7179

72-
cfg.grpcPort = port
80+
cfg.debugPort = port
7381
return nil
7482
}
7583
}

components/common-go/baseserver/options_test.go

Lines changed: 47 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,15 @@ func TestOptions(t *testing.T) {
1717
logger := log.New()
1818
httpPort := 8080
1919
grpcPort := 8081
20+
debugPort := 8082
2021
timeout := 10 * time.Second
2122
hostname := "another_hostname"
2223
registry := prometheus.NewRegistry()
2324
health := healthcheck.NewHandler()
2425

2526
var opts = []Option{
2627
WithHostname(hostname),
28+
WithDebugPort(debugPort),
2729
WithHTTPPort(httpPort),
2830
WithGRPCPort(grpcPort),
2931
WithLogger(logger),
@@ -39,34 +41,62 @@ func TestOptions(t *testing.T) {
3941
hostname: hostname,
4042
grpcPort: grpcPort,
4143
httpPort: httpPort,
44+
debugPort: debugPort,
4245
closeTimeout: timeout,
4346
metricsRegistry: registry,
4447
healthHandler: health,
4548
}, cfg)
4649
}
4750

48-
func TestWithTTPPort(t *testing.T) {
49-
t.Run("negative", func(t *testing.T) {
50-
_, err := evaluateOptions(defaultConfig(), WithHTTPPort(-1))
51-
require.Error(t, err)
52-
})
53-
54-
t.Run("zero", func(t *testing.T) {
55-
_, err := evaluateOptions(defaultConfig(), WithHTTPPort(0))
51+
func TestWithHTTPPort(t *testing.T) {
52+
for _, scenario := range []struct {
53+
Port int
54+
Expected int
55+
}{
56+
{Port: -1, Expected: -1},
57+
{Port: 0, Expected: 0},
58+
{Port: 9000, Expected: 9000},
59+
} {
60+
cfg, err := evaluateOptions(defaultConfig(), WithHTTPPort(scenario.Port))
5661
require.NoError(t, err)
57-
})
62+
require.Equal(t, scenario.Expected, cfg.httpPort)
63+
}
5864
}
5965

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

72102
func TestLogger_ErrorsWithNilLogger(t *testing.T) {

0 commit comments

Comments
 (0)