From 61cf4c302476e6d095d878491fa0964bed76074e Mon Sep 17 00:00:00 2001 From: Marcin Wolny Date: Tue, 20 Feb 2024 11:17:49 +0100 Subject: [PATCH] Inconsistent config practices --- internal/proxy/config.go | 7 ++++--- internal/proxy/proxy.go | 4 ++-- internal/proxy/proxy_test.go | 13 ++++++++----- internal/rpcgateway/rpcgateway.go | 8 ++++---- 4 files changed, 18 insertions(+), 14 deletions(-) diff --git a/internal/proxy/config.go b/internal/proxy/config.go index 699767b..f4c21f3 100644 --- a/internal/proxy/config.go +++ b/internal/proxy/config.go @@ -18,7 +18,8 @@ type ProxyConfig struct { // nolint:revive // This struct is temporary. It's about to keep the input interface clean and simple. type Config struct { - Proxy ProxyConfig - Targets []NodeProviderConfig - HealthChecks HealthCheckConfig + Proxy ProxyConfig + Targets []NodeProviderConfig + HealthChecks HealthCheckConfig + HealthcheckManager *HealthCheckManager } diff --git a/internal/proxy/proxy.go b/internal/proxy/proxy.go index c466b6c..9d3908b 100644 --- a/internal/proxy/proxy.go +++ b/internal/proxy/proxy.go @@ -21,9 +21,9 @@ type Proxy struct { metricResponseStatus *prometheus.CounterVec } -func NewProxy(config Config, hcm *HealthCheckManager) *Proxy { +func NewProxy(config Config) *Proxy { proxy := &Proxy{ - hcm: hcm, + hcm: config.HealthcheckManager, timeout: config.Proxy.UpstreamTimeout, metricRequestDuration: promauto.NewHistogramVec( prometheus.HistogramOpts{ diff --git a/internal/proxy/proxy_test.go b/internal/proxy/proxy_test.go index cc4647e..db72ea2 100644 --- a/internal/proxy/proxy_test.go +++ b/internal/proxy/proxy_test.go @@ -71,11 +71,12 @@ func TestHttpFailoverProxyRerouteRequests(t *testing.T) { Config: rpcGatewayConfig.HealthChecks, Logger: slog.New(slog.NewTextHandler(os.Stderr, nil)), }) + rpcGatewayConfig.HealthcheckManager = healthcheckManager // Setup HttpFailoverProxy but not starting the HealthCheckManager // so the no target will be tainted or marked as unhealthy by the HealthCheckManager // the failoverProxy should automatically reroute the request to the second RPC Server by itself - httpFailoverProxy := NewProxy(rpcGatewayConfig, healthcheckManager) + httpFailoverProxy := NewProxy(rpcGatewayConfig) requestBody := bytes.NewBufferString(`{"this_is": "body"}`) req, err := http.NewRequest(http.MethodPost, "/", requestBody) @@ -123,9 +124,10 @@ func TestHttpFailoverProxyDecompressRequest(t *testing.T) { Config: rpcGatewayConfig.HealthChecks, Logger: slog.New(slog.NewTextHandler(os.Stderr, nil)), }) + rpcGatewayConfig.HealthcheckManager = healthcheckManager // Setup HttpFailoverProxy but not starting the HealthCheckManager // so the no target will be tainted or marked as unhealthy by the HealthCheckManager - httpFailoverProxy := NewProxy(rpcGatewayConfig, healthcheckManager) + httpFailoverProxy := NewProxy(rpcGatewayConfig) var buf bytes.Buffer g := gzip.NewWriter(&buf) @@ -178,9 +180,10 @@ func TestHttpFailoverProxyWithCompressionSupportedTarget(t *testing.T) { Config: rpcGatewayConfig.HealthChecks, Logger: slog.New(slog.NewTextHandler(os.Stderr, nil)), }) + rpcGatewayConfig.HealthcheckManager = healthcheckManager // Setup HttpFailoverProxy but not starting the HealthCheckManager // so the no target will be tainted or marked as unhealthy by the HealthCheckManager - httpFailoverProxy := NewProxy(rpcGatewayConfig, healthcheckManager) + httpFailoverProxy := NewProxy(rpcGatewayConfig) var buf bytes.Buffer g := gzip.NewWriter(&buf) @@ -243,13 +246,13 @@ func TestHTTPFailoverProxyWhenCannotConnectToPrimaryProvider(t *testing.T) { Config: rpcGatewayConfig.HealthChecks, Logger: slog.New(slog.NewTextHandler(os.Stderr, nil)), }) - + rpcGatewayConfig.HealthcheckManager = healthcheckManager // Setup HttpFailoverProxy but not starting the HealthCheckManager so the // no target will be tainted or marked as unhealthy by the // HealthCheckManager the failoverProxy should automatically reroute the // request to the second RPC Server by itself - httpFailoverProxy := NewProxy(rpcGatewayConfig, healthcheckManager) + httpFailoverProxy := NewProxy(rpcGatewayConfig) requestBody := bytes.NewBufferString(`{"this_is": "body"}`) req, err := http.NewRequest(http.MethodPost, "/", requestBody) diff --git a/internal/rpcgateway/rpcgateway.go b/internal/rpcgateway/rpcgateway.go index 32fba0e..9a45399 100644 --- a/internal/rpcgateway/rpcgateway.go +++ b/internal/rpcgateway/rpcgateway.go @@ -80,11 +80,11 @@ func NewRPCGateway(config RPCGatewayConfig) *RPCGateway { }) proxy := proxy.NewProxy( proxy.Config{ - Proxy: config.Proxy, - Targets: config.Targets, - HealthChecks: config.HealthChecks, + Proxy: config.Proxy, + Targets: config.Targets, + HealthChecks: config.HealthChecks, + HealthcheckManager: hcm, }, - hcm, ) r := chi.NewRouter()