Add node count and next-node selection to LoadBalancer API

Closes #46156

Signed-off-by: Marco Neuhaus <m.neuhaus@smf.de>
This commit is contained in:
Marco N. 2026-02-10 21:18:49 +01:00 committed by GitHub
parent fc150d1bca
commit b7fe294fa0
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -25,6 +25,8 @@ public class LoadBalancer {
private final Vertx vertx;
private final HttpProxy proxy;
private int currentNodeIndex = 0;
public LoadBalancer(ClusteredKeycloakServer server) {
this.server = server;
@ -38,7 +40,7 @@ public class LoadBalancer {
return ProxyInterceptor.super.handleProxyRequest(context);
}
});
node(0);
node(currentNodeIndex);
HttpServer proxyServer = vertx.createHttpServer();
proxyServer.requestHandler(proxy).listen(9999, "localhost");
@ -46,6 +48,7 @@ public class LoadBalancer {
public void node(int index) {
Origin origin = origin(index);
currentNodeIndex = index;
LOGGER.debugf("Setting proxy origin to: %s:%d", origin.host, origin.port);
proxy.origin(origin.port, origin.host);
}
@ -54,6 +57,14 @@ public class LoadBalancer {
return origin(index).urls;
}
public int nodeCount() {
return server.clusterSize();
}
public void nextNode() {
node((currentNodeIndex + 1) % nodeCount());
}
private Origin origin(int index) {
if (index >= server.clusterSize()) {
throw new IllegalArgumentException("Node index out of bounds. Requested nodeIndex: %d, cluster size: %d".formatted(server.clusterSize(), index));