mirror of
https://github.com/keycloak/keycloak.git
synced 2026-05-28 04:13:22 -04:00
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:
parent
fc150d1bca
commit
b7fe294fa0
1 changed files with 12 additions and 1 deletions
|
|
@ -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));
|
||||
|
|
|
|||
Loading…
Reference in a new issue