apache / apache/shenyu

[BUG] LoadbalancerUtils.buildLoadBalanceData copies all headers + cookies + queryParams per request though only ip is used

Open
#6,578 1 comment 0 reactions 0 assignees View on GitHub
plugin: divide priority: medium type: performance
Dominant language
Java
Stars
8.8k
Forks
3.1k
Avg merge
7d 1h
Merged PRs (30d)
85

Description

## Description
`getForExchange` (on the request path of every HTTP/WebSocket/Dubbo proxy) unconditionally allocates `new LoadBalanceData(...)` (which itself defaults to 4 empty HashMaps), `buildMultiValueMap(headers)` (full header copy), `buildMultiValueMap(queryParams)` (putAll), and `buildCookies(cookies)`. But of all load balancers only `HashLoadBalancer` reads anything from `LoadBalanceData`, and only `data.getIp()`. Random/RoundRobin/P2C/LeastActive/ShortestResponse read nothing.

## Location
```
shenyu-plugin-base/.../utils/LoadbalancerUtils.java:69-99
callers: DividePlugin.java:114/116, WebSocketPlugin.java:99, DefaultRetryStrategy.java:123, ApacheDubboProxyService.java:144
```

## Impact
3 HashMaps + LoadBalanceData per proxied request — significant short-lived garbage at high RPS for the 5 of 6 LB algorithms that don't use the data.

## Suggested fix
Make `LoadBalanceData` lazy — pass the `ServerWebExchange` and resolve fields on demand, or only build the sub-maps when an algorithm actually requests them. Remove the `new HashMap<>()` field initializers in `LoadBalanceData`.

## Related existing issue(s)
None

_Identified during the 2026-08-02 audit; full list in [`docs/issue-candidates-2026-08-02.md`](docs/issue-candidates-2026-08-02.md)._

Contributor guide

No contributing guide indexed for this repository

Research direction

Start with shenyu-plugin-base/.../utils/LoadbalancerUtils.java and LoadBalanceData, then trace getForExchange through the callers in DividePlugin.java, WebSocketPlugin.java, DefaultRetryStrategy.java, and ApacheDubboProxyService.java. Compare those paths with the load balancers, especially HashLoadBalancer; done means unused headers, cookies, query parameters, and maps are not allocated per request while the load-balancing behavior remains unchanged.

Written by the indexing model from the issue text.

Assessment

Tech stack
java
Domain
backend, performance
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Quiet
Clarity
Mostly clear
Newbie friendliness
55/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.