hengyuss opened a new issue, #6426:
URL: https://github.com/apache/shenyu/issues/6426

   ### Is there an existing issue for this?
   
   - [x] I have searched the existing issues
   
   ### Current Behavior
   
   Currently, DividePlugin contains strategy-specific callback logic for P2C 
and ShortestResponse load balance algorithms:
   ```java
   if (ruleHandle.getLoadBalance().equals(P2C)) {
       return chain.execute(exchange)
           .doOnSuccess(e -> responseTrigger(upstream))
           .doOnError(throwable -> responseTrigger(upstream));
   } else if (ruleHandle.getLoadBalance().equals(SHORTEST_RESPONSE)) {
       beginTime = System.currentTimeMillis();
       return chain.execute(exchange)
           .doOnSuccess(e -> successResponseTrigger(upstream));
   }
   return chain.execute(exchange);
   ```
   This design has several issues:
   1. Violates Single Responsibility Principle: DividePlugin's core 
responsibility is selecting an upstream and writing routing info into exchange. 
Metrics collection for specific load balance strategies should not be mixed in.
   2. Violates Open/Closed Principle: Adding a new load balance strategy that 
requires runtime metrics (e.g., adaptive load balancing) would require 
modifying DividePlugin, rather than simply implementing a new LoadBalance class.
   3. Thread safety issue: beginTime is an instance variable shared across 
concurrent requests, which is not thread-safe. It should be stored per-request 
(e.g., in exchange attributes).
   
   ### Expected Behavior
   
   Add onSuccess and onError callback methods to the LoadBalance interface with 
default empty implementations:
   ```java
   public interface LoadBalance {
       Upstream select(List<Upstream> upstreamList, LoadBalanceData data);
   
       default void onSuccess(Upstream upstream) {}
   
       default void onError(Upstream upstream) {}
   }
   
   return chain.execute(exchange)
       .doOnSuccess(e -> loadBalance.onSuccess(upstream))
       .doOnError(t -> loadBalance.onError(upstream));
   ```
   
   ### Steps To Reproduce
   
   _No response_
   
   ### Environment
   
   ```markdown
   ShenYu version(s):latest
   ```
   
   ### Debug logs
   
   _No response_
   
   ### Anything else?
   
   i will do it


-- 
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