Skip to content

Commit

Permalink
refactor: add check enabled flag to error and latency structs
Browse files Browse the repository at this point in the history
  • Loading branch information
polsar88 committed Oct 7, 2024
1 parent 77838f1 commit 1f77dd9
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 40 deletions.
6 changes: 4 additions & 2 deletions internal/checks/error.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ type ErrorCheck struct {
upstreamConfig *config.UpstreamConfig
routingConfig *config.RoutingConfig
errorCircuitBreaker ErrorCircuitBreaker
isCheckEnabled bool
}

type ErrorCircuitBreaker interface {
Expand Down Expand Up @@ -55,6 +56,7 @@ func NewErrorChecker(
metricsContainer: metricsContainer,
logger: logger,
errorCircuitBreaker: NewErrorStats(routingConfig),
isCheckEnabled: routingConfig.IsCheckEnabled,
}
}

Expand Down Expand Up @@ -155,7 +157,7 @@ func (e *ErrorStats) IsOpen() bool {
}

func (c *ErrorCheck) IsPassing([]string) bool {
if !c.routingConfig.IsEnhancedRoutingControlDefined() {
if !c.isCheckEnabled {
return true
}

Expand All @@ -173,7 +175,7 @@ func (c *ErrorCheck) IsPassing([]string) bool {
}

func (c *ErrorCheck) RecordRequest(data *types.RequestData) {
if !c.routingConfig.IsEnhancedRoutingControlDefined() {
if !c.isCheckEnabled {
return
}

Expand Down
6 changes: 4 additions & 2 deletions internal/checks/latency.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ type LatencyCheck struct {
routingConfig *config.RoutingConfig
methodLatencyBreaker map[string]LatencyCircuitBreaker // RPC method -> LatencyCircuitBreaker
lock sync.RWMutex
isCheckEnabled bool
}

type LatencyCircuitBreaker interface {
Expand Down Expand Up @@ -68,6 +69,7 @@ func NewLatencyChecker(
metricsContainer: metricsContainer,
logger: logger,
methodLatencyBreaker: make(map[string]LatencyCircuitBreaker),
isCheckEnabled: routingConfig.IsCheckEnabled,
}
}

Expand Down Expand Up @@ -113,7 +115,7 @@ func (l *LatencyStats) IsOpen() bool {
}

func (c *LatencyCheck) IsPassing(methods []string) bool {
if !c.routingConfig.IsEnhancedRoutingControlDefined() {
if !c.isCheckEnabled {
return true
}

Expand Down Expand Up @@ -147,7 +149,7 @@ func (c *LatencyCheck) IsPassing(methods []string) bool {
}

func (c *LatencyCheck) RecordRequest(data *types.RequestData) {
if !c.routingConfig.IsEnhancedRoutingControlDefined() {
if !c.isCheckEnabled {
return
}

Expand Down
2 changes: 2 additions & 0 deletions internal/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -352,6 +352,7 @@ type RoutingConfig struct {
BanWindow *time.Duration `yaml:"banWindow"`
MaxBlocksBehind int `yaml:"maxBlocksBehind"`
IsInitialized bool
IsCheckEnabled bool
}

// IsEnhancedRoutingControlDefined returns true iff any of the enhanced routing control fields are specified
Expand Down Expand Up @@ -434,6 +435,7 @@ func (r *RoutingConfig) setDefaults(globalConfig *RoutingConfig, force bool) boo
}

r.IsInitialized = true
r.IsCheckEnabled = true

return true
}
Expand Down
87 changes: 51 additions & 36 deletions internal/config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -429,9 +429,10 @@ func TestParseConfig_ValidConfigLatencyRouting_AllFieldsSet(t *testing.T) {
"internal server error",
},
},
Latency: &expectedLatencyConfig,
AlwaysRoute: newBool(true),
IsInitialized: true,
Latency: &expectedLatencyConfig,
AlwaysRoute: newBool(true),
IsInitialized: true,
IsCheckEnabled: true,
}

expectedRoutingChainConfig := expectedRoutingConfig
Expand Down Expand Up @@ -518,9 +519,10 @@ func TestParseConfig_ValidConfigLatencyRouting_ErrorsConfigOverridesAndMerges(t
"freaking out",
},
},
Latency: &LatencyConfig{MethodLatencyThresholds: map[string]time.Duration{}},
AlwaysRoute: newBool(false),
IsInitialized: true,
Latency: &LatencyConfig{MethodLatencyThresholds: map[string]time.Duration{}},
AlwaysRoute: newBool(false),
IsInitialized: true,
IsCheckEnabled: true,
}

expectedRoutingChainConfig := expectedRoutingConfig
Expand Down Expand Up @@ -598,9 +600,10 @@ func TestParseConfig_ValidConfigLatencyRouting_DefaultsForDetectionAndBanWindows
Errors: &ErrorsConfig{
Rate: 0.25,
},
Latency: &expectedLatencyConfig,
AlwaysRoute: newBool(false),
IsInitialized: true,
Latency: &expectedLatencyConfig,
AlwaysRoute: newBool(false),
IsInitialized: true,
IsCheckEnabled: true,
},
},
Chains: getCommonChainsConfig(&RoutingConfig{
Expand All @@ -610,6 +613,7 @@ func TestParseConfig_ValidConfigLatencyRouting_DefaultsForDetectionAndBanWindows
Latency: &expectedLatencyConfig,
AlwaysRoute: newBool(false),
IsInitialized: true,
IsCheckEnabled: true,
}),
}

Expand Down Expand Up @@ -758,9 +762,10 @@ func TestParseConfig_ValidConfigLatencyRouting_MethodLatencies_TopLevelLatencySp
},
},
},
Errors: &ErrorsConfig{Rate: DefaultErrorRate},
AlwaysRoute: newBool(false),
IsInitialized: true,
Errors: &ErrorsConfig{Rate: DefaultErrorRate},
AlwaysRoute: newBool(false),
IsInitialized: true,
IsCheckEnabled: true,
},
},

Expand All @@ -783,9 +788,10 @@ func TestParseConfig_ValidConfigLatencyRouting_MethodLatencies_TopLevelLatencySp
},
},
},
Errors: &ErrorsConfig{Rate: DefaultErrorRate},
AlwaysRoute: newBool(false),
IsInitialized: true,
Errors: &ErrorsConfig{Rate: DefaultErrorRate},
AlwaysRoute: newBool(false),
IsInitialized: true,
IsCheckEnabled: true,
}),
}

Expand Down Expand Up @@ -849,6 +855,7 @@ func TestParseConfig_ValidConfigLatencyRouting_MethodLatencies_TopLevelLatencyNo
Errors: &ErrorsConfig{Rate: DefaultErrorRate},
AlwaysRoute: newBool(false),
IsInitialized: true,
IsCheckEnabled: true,
},
},

Expand All @@ -859,6 +866,7 @@ func TestParseConfig_ValidConfigLatencyRouting_MethodLatencies_TopLevelLatencyNo
Errors: &ErrorsConfig{Rate: DefaultErrorRate},
AlwaysRoute: newBool(false),
IsInitialized: true,
IsCheckEnabled: true,
}),
}

Expand Down Expand Up @@ -924,9 +932,10 @@ func TestParseConfig_ValidConfigLatencyRouting_MethodLatencies_TopLevelLatencySp
},
},
},
Errors: &ErrorsConfig{Rate: DefaultErrorRate},
AlwaysRoute: newBool(false),
IsInitialized: true,
Errors: &ErrorsConfig{Rate: DefaultErrorRate},
AlwaysRoute: newBool(false),
IsInitialized: true,
IsCheckEnabled: true,
},
},

Expand All @@ -948,9 +957,10 @@ func TestParseConfig_ValidConfigLatencyRouting_MethodLatencies_TopLevelLatencySp
},
},
},
Errors: &ErrorsConfig{Rate: DefaultErrorRate},
AlwaysRoute: newBool(false),
IsInitialized: true,
Errors: &ErrorsConfig{Rate: DefaultErrorRate},
AlwaysRoute: newBool(false),
IsInitialized: true,
IsCheckEnabled: true,
}),
}

Expand Down Expand Up @@ -1054,9 +1064,10 @@ func TestParseConfig_ValidConfigLatencyRouting_MethodLatencies_TopLevelLatencyNo
},
Threshold: DefaultMaxLatency,
},
Errors: &expectedErrorsConfig,
AlwaysRoute: newBool(false),
IsInitialized: true,
Errors: &expectedErrorsConfig,
AlwaysRoute: newBool(false),
IsInitialized: true,
IsCheckEnabled: true,
},
},

Expand Down Expand Up @@ -1087,9 +1098,10 @@ func TestParseConfig_ValidConfigLatencyRouting_MethodLatencies_TopLevelLatencyNo
},
},
},
Errors: &expectedErrorsConfig,
AlwaysRoute: newBool(false),
IsInitialized: true,
Errors: &expectedErrorsConfig,
AlwaysRoute: newBool(false),
IsInitialized: true,
IsCheckEnabled: true,
}),
}

Expand Down Expand Up @@ -1158,9 +1170,10 @@ func TestParseConfig_ValidConfigLatencyRouting_NoGlobalRoutingConfig_TwoChains_O
Latency: &LatencyConfig{
MethodLatencyThresholds: map[string]time.Duration{},
},
Errors: &ErrorsConfig{Rate: 0.25},
AlwaysRoute: newBool(false),
IsInitialized: true,
Errors: &ErrorsConfig{Rate: 0.25},
AlwaysRoute: newBool(false),
IsInitialized: true,
IsCheckEnabled: true,
},
},
Chains: append(getCommonChainsConfig(&RoutingConfig{
Expand All @@ -1176,9 +1189,10 @@ func TestParseConfig_ValidConfigLatencyRouting_NoGlobalRoutingConfig_TwoChains_O
},
},
},
Errors: &ErrorsConfig{Rate: 0.25},
AlwaysRoute: newBool(false),
IsInitialized: true,
Errors: &ErrorsConfig{Rate: 0.25},
AlwaysRoute: newBool(false),
IsInitialized: true,
IsCheckEnabled: true,
}), append(getCommonChainsConfig(&RoutingConfig{
DetectionWindow: NewDuration(DefaultDetectionWindow),
BanWindow: NewDuration(DefaultBanWindow),
Expand All @@ -1193,9 +1207,10 @@ func TestParseConfig_ValidConfigLatencyRouting_NoGlobalRoutingConfig_TwoChains_O
},
},
},
Errors: &ErrorsConfig{Rate: 0.25},
AlwaysRoute: newBool(false),
IsInitialized: true,
Errors: &ErrorsConfig{Rate: 0.25},
AlwaysRoute: newBool(false),
IsInitialized: true,
IsCheckEnabled: true,
}), getCommonChainsConfig(&RoutingConfig{})...)...),
}

Expand Down

0 comments on commit 1f77dd9

Please sign in to comment.