wy471x opened a new pull request, #7431:
URL: https://github.com/apache/shenyu/pull/7431

   <!-- Describe your PR here; e.g. Fixes #issueNo -->
   
   <!--
   Thank you for proposing a pull request. This template will guide you through 
the essential steps necessary for a pull request.
   -->
   Make sure that:
   
   - [X] You have read the [contribution 
guidelines](https://shenyu.apache.org/community/contributor-guide).
   - [X] You submit test cases (unit or integration tests) that back your 
changes.
   - [X] Your local test passed `./mvnw clean install 
-Dmaven.javadoc.skip=true`.
   
   ## Summary
   
   `LoadbalancerUtils.buildLoadBalanceData` runs on every proxied request 
(Divide, WebSocket, Apache Dubbo, retries) and used to copy the full request 
headers, the parsed cookies and the query params into fresh maps — seven 
`HashMap` allocations plus a full header copy per request. None of the load 
balancers consume that data: of all implementations the only field ever read is 
`LoadBalanceData.getIp()` (`HashLoadBalancer`), while 
Random/RoundRobin/P2C/LeastActive/ShortestResponse read nothing.
   
   ### Changes:
   
   1. `LoadBalanceData` (`shenyu-loadbalancer/.../entity/LoadBalanceData.java`) 
— the four map fields now default to `Collections.emptyMap()` instead of `new 
HashMap<>()`. Instances built without explicit maps (`getForNoExchange`, 
SDK/TCP/gRPC callers) no longer allocate four maps each; the full constructor 
keeps the same behavior for explicitly provided values.
   2. `LoadbalancerUtils.buildLoadBalanceData` 
(`shenyu-plugin-base/.../utils/LoadbalancerUtils.java`) — no longer builds the 
headers/cookies/queryParams copies; it passes shared empty maps and keeps only 
the cheap fields (ip/httpMethod/uri/attributes). The now-unused 
`buildMultiValueMap` and `buildCookies` helpers were removed.
   3. The `ServerWebExchange`-based lazy option suggested in the issue is not 
possible inside `shenyu-loadbalancer` (that module has no Spring dependency), 
and since no algorithm reads those maps, building them on demand would be dead 
code. Callers that need them can still provide them through the full 
constructor/setters.
   
   ### Test Cases:
   
   - `LoadbalancerUtilsTest#testGetForExchangeDoesNotCopyUnusedRequestData` 
(new) — verifies that a hash selection still works with a non-default remote 
address and that `getHeaders()`, `getCookies()` and `getQueryParams()` are 
never called on the request while building the load balance data.
   - `LoadbalancerUtilsTest#testMissingRemoteAddressAndMethodUseDefaults` 
(updated) — stubs for headers/cookies/query params were removed, proving the 
defaults path no longer touches them.
   - `LoadBalanceDataTest` (new) — default instance exposes empty non-null 
collections and shares the same empty instances across instances; the full 
constructor retains the provided maps.
   
   ## Verification
   
   - `mvn -pl shenyu-loadbalancer,shenyu-plugin/shenyu-plugin-base -am test` 
(JDK 21): BUILD SUCCESS — shenyu-plugin-base 156 tests, shenyu-loadbalancer 
tests all green, 0 failures.
   - Checkstyle passed (validate phase of the same build).
   - `apache-rat:check` on both modules: 0 unapproved licenses.
   
   close #6578
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to