From 132bdd815ca09077fa13b833581eb3beebb8baaa Mon Sep 17 00:00:00 2001 From: Roman Zavodskikh Date: Tue, 7 May 2024 13:25:11 +0200 Subject: [PATCH] Make min-drop-probability of PHC optional The default value (0.0) makes some sense and is equivalent to how PHC was working before. Signed-off-by: Roman Zavodskikh --- proxy/proxy.go | 30 +++++++++++++++++------------- proxy/proxy_test.go | 16 ++++++++++++++++ 2 files changed, 33 insertions(+), 13 deletions(-) diff --git a/proxy/proxy.go b/proxy/proxy.go index 2de0161d01..97143f6575 100644 --- a/proxy/proxy.go +++ b/proxy/proxy.go @@ -169,10 +169,11 @@ func InitPassiveHealthChecker(o map[string]string) (bool, *PassiveHealthCheck, e } result := &PassiveHealthCheck{} - keysInitialized := make(map[string]struct{}) + requiredKeysInitialized := make(map[string]struct{}) for key, value := range o { switch key { + /* required parameters */ case "period": period, err := time.ParseDuration(value) if err != nil { @@ -182,6 +183,7 @@ func InitPassiveHealthChecker(o map[string]string) (bool, *PassiveHealthCheck, e return false, nil, fmt.Errorf("passive health check: invalid period value: %s", value) } result.Period = period + requiredKeysInitialized[key] = struct{}{} case "min-requests": minRequests, err := strconv.Atoi(value) if err != nil { @@ -191,15 +193,7 @@ func InitPassiveHealthChecker(o map[string]string) (bool, *PassiveHealthCheck, e return false, nil, fmt.Errorf("passive health check: invalid minRequests value: %s", value) } result.MinRequests = int64(minRequests) - case "min-drop-probability": - minDropProbability, err := strconv.ParseFloat(value, 64) - if err != nil { - return false, nil, fmt.Errorf("passive health check: invalid minDropProbability value: %s", value) - } - if minDropProbability < 0 || minDropProbability > 1 { - return false, nil, fmt.Errorf("passive health check: invalid minDropProbability value: %s", value) - } - result.MinDropProbability = minDropProbability + requiredKeysInitialized[key] = struct{}{} case "max-drop-probability": maxDropProbability, err := strconv.ParseFloat(value, 64) if err != nil { @@ -209,14 +203,24 @@ func InitPassiveHealthChecker(o map[string]string) (bool, *PassiveHealthCheck, e return false, nil, fmt.Errorf("passive health check: invalid maxDropProbability value: %s", value) } result.MaxDropProbability = maxDropProbability + requiredKeysInitialized[key] = struct{}{} + + /* optional parameters */ + case "min-drop-probability": + minDropProbability, err := strconv.ParseFloat(value, 64) + if err != nil { + return false, nil, fmt.Errorf("passive health check: invalid minDropProbability value: %s", value) + } + if minDropProbability < 0 || minDropProbability > 1 { + return false, nil, fmt.Errorf("passive health check: invalid minDropProbability value: %s", value) + } + result.MinDropProbability = minDropProbability default: return false, nil, fmt.Errorf("passive health check: invalid parameter: key=%s,value=%s", key, value) } - - keysInitialized[key] = struct{}{} } - if len(keysInitialized) != 4 { + if len(requiredKeysInitialized) != 3 { return false, nil, fmt.Errorf("passive health check: missing required parameters") } if result.MinDropProbability >= result.MaxDropProbability { diff --git a/proxy/proxy_test.go b/proxy/proxy_test.go index ea3cc79463..f4bbf6ad52 100644 --- a/proxy/proxy_test.go +++ b/proxy/proxy_test.go @@ -2454,6 +2454,22 @@ func TestInitPassiveHealthChecker(t *testing.T) { expectedParams: nil, expectedError: fmt.Errorf("passive health check: missing required parameters"), }, + { + inputArg: map[string]string{ + "period": "1m", + "min-requests": "10", + "max-drop-probability": "0.9", + /* using default max-drop-probability */ + }, + expectedEnabled: true, + expectedParams: &PassiveHealthCheck{ + Period: 1 * time.Minute, + MinRequests: 10, + MaxDropProbability: 0.9, + MinDropProbability: 0.0, + }, + expectedError: nil, + }, } { t.Run(fmt.Sprintf("case_%d", i), func(t *testing.T) { enabled, params, err := InitPassiveHealthChecker(ti.inputArg)