-
Notifications
You must be signed in to change notification settings - Fork 351
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Roman Zavodskikh <roman.zavodskikh@zalando.de>
- Loading branch information
Roman Zavodskikh
committed
Feb 1, 2024
1 parent
ef11aa0
commit 56b9fee
Showing
10 changed files
with
332 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package proxy | ||
|
||
import "github.com/zalando/skipper/routing" | ||
|
||
type healthyEndpoints struct { | ||
enabled bool | ||
timeoutedRequestsRatioThreshold float64 | ||
totalRequestsThreshold float64 | ||
} | ||
|
||
func (h *healthyEndpoints) healthyEndpoint(endpoint routing.LBEndpoint) bool { | ||
timeoutedRequests := float64(endpoint.Metrics.TimeoutedRequests()) | ||
totalRequests := float64(endpoint.Metrics.TotalRequests()) | ||
failedRequestsRatio := timeoutedRequests / totalRequests | ||
|
||
return failedRequestsRatio < h.timeoutedRequestsRatioThreshold || totalRequests < h.totalRequestsThreshold | ||
} | ||
|
||
func (h *healthyEndpoints) filterHealthyEndpoints(endpoints []routing.LBEndpoint, rt *routing.Route) []routing.LBEndpoint { | ||
if !h.enabled { | ||
return endpoints | ||
} | ||
|
||
filtered := make([]routing.LBEndpoint, 0, len(endpoints)) | ||
for _, e := range endpoints { | ||
if h.healthyEndpoint(e) { | ||
filtered = append(filtered, e) | ||
} | ||
} | ||
|
||
if len(filtered) == 0 { | ||
return endpoints | ||
} | ||
return filtered | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
package proxy | ||
|
||
import ( | ||
"fmt" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
"github.com/zalando/skipper/eskip" | ||
"github.com/zalando/skipper/loadbalancer" | ||
"github.com/zalando/skipper/routing" | ||
) | ||
|
||
func initiliaze(hosts []string) (*routing.EndpointRegistry, *routing.Route) { | ||
endpointRegistry := routing.NewEndpointRegistry(routing.RegistryOptions{}) | ||
eskipRoute := eskip.Route{BackendType: eskip.LBBackend, LBAlgorithm: loadbalancer.PowerOfRandomNChoices.String()} | ||
for i := range hosts { | ||
eskipRoute.LBEndpoints = append(eskipRoute.LBEndpoints, fmt.Sprintf("http://%s", hosts[i])) | ||
} | ||
|
||
route := &routing.Route{ | ||
Route: eskipRoute, | ||
LBEndpoints: []routing.LBEndpoint{}, | ||
} | ||
rt := loadbalancer.NewAlgorithmProvider().Do([]*routing.Route{route}) | ||
route = rt[0] | ||
endpointRegistry.Do([]*routing.Route{route}) | ||
|
||
return endpointRegistry, route | ||
} | ||
|
||
func TestFilterHealthyEndpointsReturnsAllEndpointsWhenAllAreHealthy(t *testing.T) { | ||
hosts := []string{"host1:80", "host2:80", "host3:80"} | ||
endpointRegistry, route := initiliaze(hosts) | ||
|
||
for i := range hosts { | ||
metrics := endpointRegistry.GetMetrics(hosts[i]) | ||
for j := 0; j < 50; j++ { | ||
metrics.IncTotalRequests() | ||
} | ||
} | ||
|
||
proxy := &Proxy{ | ||
registry: endpointRegistry, | ||
heathlyEndpoints: &healthyEndpoints{enabled: true, timeoutedRequestsRatioThreshold: 0.1, totalRequestsThreshold: 10}, | ||
} | ||
endpoints := proxy.heathlyEndpoints.filterHealthyEndpoints(route.LBEndpoints, route) | ||
|
||
assert.Equal(t, 3, len(endpoints)) | ||
assert.Equal(t, hosts[0], endpoints[0].Host) | ||
assert.Equal(t, hosts[1], endpoints[1].Host) | ||
assert.Equal(t, hosts[2], endpoints[2].Host) | ||
} | ||
|
||
func TestFilterHealthyEndpointsReturnsAllEndpointsWhenAllAreUnhealthy(t *testing.T) { | ||
hosts := []string{"host1:80", "host2:80", "host3:80"} | ||
endpointRegistry, route := initiliaze(hosts) | ||
|
||
for i := range hosts { | ||
metrics := endpointRegistry.GetMetrics(hosts[i]) | ||
for j := 0; j < 50; j++ { | ||
metrics.IncTotalRequests() | ||
} | ||
for j := 0; j < 30; j++ { | ||
metrics.IncTimeoutedRequests() | ||
} | ||
} | ||
|
||
proxy := &Proxy{ | ||
registry: endpointRegistry, | ||
heathlyEndpoints: &healthyEndpoints{enabled: true, timeoutedRequestsRatioThreshold: 0.1, totalRequestsThreshold: 10}, | ||
} | ||
endpoints := proxy.heathlyEndpoints.filterHealthyEndpoints(route.LBEndpoints, route) | ||
|
||
assert.Equal(t, 3, len(endpoints)) | ||
assert.Equal(t, hosts[0], endpoints[0].Host) | ||
assert.Equal(t, hosts[1], endpoints[1].Host) | ||
assert.Equal(t, hosts[2], endpoints[2].Host) | ||
} | ||
|
||
func TestFilterHealthyEndpointsFiltersUnhealthyEndpoints(t *testing.T) { | ||
hosts := []string{"host1:80", "host2:80", "host3:80"} | ||
endpointRegistry, route := initiliaze(hosts) | ||
|
||
for i := range hosts { | ||
metrics := endpointRegistry.GetMetrics(hosts[i]) | ||
for j := 0; j < 50; j++ { | ||
metrics.IncTotalRequests() | ||
} | ||
} | ||
for i := 0; i < 30; i++ { | ||
metrics := endpointRegistry.GetMetrics(hosts[0]) | ||
metrics.IncTimeoutedRequests() | ||
} | ||
|
||
proxy := &Proxy{ | ||
registry: endpointRegistry, | ||
heathlyEndpoints: &healthyEndpoints{enabled: true, timeoutedRequestsRatioThreshold: 0.1, totalRequestsThreshold: 10}, | ||
} | ||
endpoints := proxy.heathlyEndpoints.filterHealthyEndpoints(route.LBEndpoints, route) | ||
|
||
assert.Equal(t, 2, len(endpoints)) | ||
assert.Equal(t, hosts[1], endpoints[0].Host) | ||
assert.Equal(t, hosts[2], endpoints[1].Host) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.