perf: reuse reverse proxy candidate slices

This commit is contained in:
wjqserver 2026-04-10 06:18:52 +08:00
parent 71a344a3de
commit 017bb13295
3 changed files with 86 additions and 8 deletions

View file

@ -110,10 +110,10 @@ func BenchmarkReverseProxyCopyResponse(b *testing.B) {
func BenchmarkReverseProxyAvailableUpstreams(b *testing.B) {
proxy := &reverseProxyHandler{
upstreams: []*reverseProxyUpstream{
{key: "a"},
{key: "b"},
{key: "c"},
{key: "d"},
{key: "a", index: 0},
{key: "b", index: 1},
{key: "c", index: 2},
{key: "d", index: 3},
},
config: ReverseProxyConfig{
PassiveHealth: ReverseProxyPassiveHealthConfig{
@ -135,6 +135,38 @@ func BenchmarkReverseProxyAvailableUpstreams(b *testing.B) {
}
}
func BenchmarkReverseProxySelectUpstream(b *testing.B) {
proxy := &reverseProxyHandler{
upstreams: []*reverseProxyUpstream{
{key: "a", index: 0},
{key: "b", index: 1},
{key: "c", index: 2},
{key: "d", index: 3},
},
config: ReverseProxyConfig{
LoadBalancing: ReverseProxyLoadBalancingConfig{Policy: LBRoundRobin()},
PassiveHealth: ReverseProxyPassiveHealthConfig{
FailDuration: time.Minute,
MaxFails: 3,
},
},
}
proxy.upstreams[0].failures = []time.Time{time.Now().Add(-30 * time.Second)}
c, _ := CreateTestContext(nil)
b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ {
selected, err := proxy.selectUpstream(c, nil)
if err != nil {
b.Fatalf("selectUpstream failed: %v", err)
}
benchmarkReverseProxySink = selected.index
}
}
func TestReverseProxyCopyResponseWithoutBufferPool(t *testing.T) {
proxy := newReverseProxyHandler(ReverseProxyConfig{})
dst := newBenchmarkResponseWriter()
@ -203,4 +235,29 @@ func TestReverseProxyCopyResponseRespectsCustomBufferLength(t *testing.T) {
}
}
func TestReverseProxyAvailableUpstreamsFiltersExcludedAndUnhealthy(t *testing.T) {
now := time.Now()
proxy := &reverseProxyHandler{
upstreams: []*reverseProxyUpstream{
{key: "a"},
{key: "b", failures: []time.Time{now.Add(-20 * time.Second), now.Add(-10 * time.Second)}},
{key: "c"},
},
config: ReverseProxyConfig{
PassiveHealth: ReverseProxyPassiveHealthConfig{
FailDuration: time.Minute,
MaxFails: 2,
},
},
}
available := proxy.availableUpstreams(now, map[string]struct{}{"c": {}})
if len(available) != 1 {
t.Fatalf("expected only one available upstream, got %d", len(available))
}
if available[0].key != "a" {
t.Fatalf("expected upstream 'a', got %q", available[0].key)
}
}
var _ io.Writer = (*benchmarkResponseWriter)(nil)