Is there an existing issue for this?
Current Behavior
Currently, DividePlugin contains strategy-specific callback logic for P2C and ShortestResponse load balance algorithms:
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:
- 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.
- 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.
- 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:
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
Debug logs
No response
Anything else?
i will do it
Is there an existing issue for this?
Current Behavior
Currently, DividePlugin contains strategy-specific callback logic for P2C and ShortestResponse load balance algorithms:
This design has several issues:
Expected Behavior
Add onSuccess and onError callback methods to the LoadBalance interface with default empty implementations:
Steps To Reproduce
No response
Environment
Debug logs
No response
Anything else?
i will do it