diff --git a/carapace-server/src/main/java/org/carapaceproxy/server/config/BackendConfiguration.java b/carapace-server/src/main/java/org/carapaceproxy/server/config/BackendConfiguration.java index ade1be13f..636f65b13 100644 --- a/carapace-server/src/main/java/org/carapaceproxy/server/config/BackendConfiguration.java +++ b/carapace-server/src/main/java/org/carapaceproxy/server/config/BackendConfiguration.java @@ -29,11 +29,12 @@ public record BackendConfiguration( String id, EndpointKey hostPort, - String probePath + String probePath, + int coldCapacity ) { - public BackendConfiguration(final String id, final String host, final int port, final String probePath) { - this(id, new EndpointKey(host, port), probePath); + public BackendConfiguration(final String id, final String host, final int port, final String probePath, final int coldCapacity) { + this(id, new EndpointKey(host, port), probePath, coldCapacity); } public String host() { diff --git a/carapace-server/src/main/java/org/carapaceproxy/server/mapper/StandardEndpointMapper.java b/carapace-server/src/main/java/org/carapaceproxy/server/mapper/StandardEndpointMapper.java index 833369e95..7731fb48e 100644 --- a/carapace-server/src/main/java/org/carapaceproxy/server/mapper/StandardEndpointMapper.java +++ b/carapace-server/src/main/java/org/carapaceproxy/server/mapper/StandardEndpointMapper.java @@ -224,7 +224,8 @@ public MapResult map(final ProxyRequest request) { case DOWN: continue; case COLD: - if (backendStatus.getConnections() > THRESHOLD) { + final int capacity = backend.coldCapacity(); + if (capacity > 0 && backendStatus.getConnections() > capacity) { // can't use this continue; } @@ -467,14 +468,14 @@ public void configure(ConfigurationStore properties) throws ConfigurationNotVali String prefix = "backend." + i + "."; String id = properties.getString(prefix + "id", ""); if (!id.isEmpty()) { - boolean enabled = properties.getBoolean(prefix + "enabled", false); - String host = properties.getString(prefix + "host", "localhost"); - int port = properties.getInt(prefix + "port", 8086); - String probePath = properties.getString(prefix + "probePath", ""); - LOG.info("configured backend {} {}:{} enabled:{}", id, host, port, enabled); + final boolean enabled = properties.getBoolean(prefix + "enabled", false); + final String host = properties.getString(prefix + "host", "localhost"); + final int port = properties.getInt(prefix + "port", 8086); + final String probePath = properties.getString(prefix + "probePath", ""); + final int coldCapacity = properties.getInt(prefix + "coldCapacity", -1); + LOG.info("configured backend {} {}:{} enabled={} capacity={}", id, host, port, enabled, coldCapacity); if (enabled) { - BackendConfiguration config = new BackendConfiguration(id, host, port, probePath); - addBackend(config); + addBackend(new BackendConfiguration(id, host, port, probePath, coldCapacity)); } } } diff --git a/carapace-server/src/test/java/org/carapaceproxy/backends/StuckRequestsTest.java b/carapace-server/src/test/java/org/carapaceproxy/backends/StuckRequestsTest.java index 451644496..fe4d6bc2e 100644 --- a/carapace-server/src/test/java/org/carapaceproxy/backends/StuckRequestsTest.java +++ b/carapace-server/src/test/java/org/carapaceproxy/backends/StuckRequestsTest.java @@ -84,7 +84,7 @@ public void testBackendUnreachableOnStuckRequest(boolean backendsUnreachableOnSt EndpointKey key = new EndpointKey("localhost", theport); StandardEndpointMapper mapper = new StandardEndpointMapper(); - mapper.addBackend(new BackendConfiguration("backend-a", "localhost", theport, "/")); + mapper.addBackend(new BackendConfiguration("backend-a", "localhost", theport, "/", -1)); mapper.addDirector(new DirectorConfiguration("director-1").addBackend("backend-a")); mapper.addAction(new ActionConfiguration("proxy-1", ActionConfiguration.TYPE_PROXY, "director-1", null, -1)); mapper.addRoute(new RouteConfiguration("route-1", "proxy-1", true, new RegexpRequestMatcher(PROPERTY_URI, ".*index.html.*"))); diff --git a/carapace-server/src/test/java/org/carapaceproxy/server/mapper/BasicStandardEndpointMapperTest.java b/carapace-server/src/test/java/org/carapaceproxy/server/mapper/BasicStandardEndpointMapperTest.java index 7ebf5042c..bf1ef0934 100644 --- a/carapace-server/src/test/java/org/carapaceproxy/server/mapper/BasicStandardEndpointMapperTest.java +++ b/carapace-server/src/test/java/org/carapaceproxy/server/mapper/BasicStandardEndpointMapperTest.java @@ -95,8 +95,8 @@ public void test() throws Exception { int backendPort = backend.port(); StandardEndpointMapper mapper = new StandardEndpointMapper(); - mapper.addBackend(new BackendConfiguration("backend-a", "localhost", backendPort, "/")); - mapper.addBackend(new BackendConfiguration("backend-b", "localhost", backendPort, "/")); + mapper.addBackend(new BackendConfiguration("backend-a", "localhost", backendPort, "/", -1)); + mapper.addBackend(new BackendConfiguration("backend-b", "localhost", backendPort, "/", -1)); mapper.addDirector(new DirectorConfiguration("director-1").addBackend("backend-a")); mapper.addDirector(new DirectorConfiguration("director-2").addBackend("backend-b")); mapper.addDirector(new DirectorConfiguration("director-all").addBackend("*")); // all the known backends @@ -317,8 +317,8 @@ public void testDefaultRoute() throws Exception { int backendPort = backend.port(); StandardEndpointMapper mapper = new StandardEndpointMapper(); - mapper.addBackend(new BackendConfiguration("backend", "localhost", backendPort, "/")); - mapper.addBackend(new BackendConfiguration("backend-down", "localhost-down", backendPort, "/")); + mapper.addBackend(new BackendConfiguration("backend", "localhost", backendPort, "/", -1)); + mapper.addBackend(new BackendConfiguration("backend-down", "localhost-down", backendPort, "/", -1)); mapper.addDirector(new DirectorConfiguration("director").addBackend("backend")); mapper.addDirector(new DirectorConfiguration("director-down").addBackend("backend-down")); mapper.addAction(new ActionConfiguration("cache", ActionConfiguration.TYPE_CACHE, "director", null, -1)); diff --git a/carapace-server/src/test/java/org/carapaceproxy/server/mapper/ForceBackendTest.java b/carapace-server/src/test/java/org/carapaceproxy/server/mapper/ForceBackendTest.java index 852b17ded..bb3793a6d 100644 --- a/carapace-server/src/test/java/org/carapaceproxy/server/mapper/ForceBackendTest.java +++ b/carapace-server/src/test/java/org/carapaceproxy/server/mapper/ForceBackendTest.java @@ -77,8 +77,8 @@ public void test() throws Exception { assertEquals("thedirector", mapper.getForceDirectorParameter()); assertEquals("thebackend", mapper.getForceBackendParameter()); - mapper.addBackend(new BackendConfiguration("backend-a", "localhost", backendPort, "/")); - mapper.addBackend(new BackendConfiguration("backend-b", "localhost", backendPort, "/")); + mapper.addBackend(new BackendConfiguration("backend-a", "localhost", backendPort, "/", -1)); + mapper.addBackend(new BackendConfiguration("backend-b", "localhost", backendPort, "/", -1)); mapper.addDirector(new DirectorConfiguration("director-1").addBackend("backend-a")); mapper.addDirector(new DirectorConfiguration("director-2").addBackend("backend-b")); diff --git a/carapace-server/src/test/java/org/carapaceproxy/server/mapper/HealthCheckTest.java b/carapace-server/src/test/java/org/carapaceproxy/server/mapper/HealthCheckTest.java index bbbf12310..ef9339305 100644 --- a/carapace-server/src/test/java/org/carapaceproxy/server/mapper/HealthCheckTest.java +++ b/carapace-server/src/test/java/org/carapaceproxy/server/mapper/HealthCheckTest.java @@ -60,7 +60,7 @@ public class HealthCheckTest { @Test public void test() throws Exception { final Map backends = new HashMap<>(); - final BackendConfiguration b1conf = new BackendConfiguration("myid", "localhost", wireMockRule.port(), "/status.html"); + final BackendConfiguration b1conf = new BackendConfiguration("myid", "localhost", wireMockRule.port(), "/status.html", -1); backends.put(b1conf.hostPort().toString(), b1conf); final EndpointMapper mapper = new TestEndpointMapper(null, 0, false, backends); final RuntimeServerConfiguration conf = new RuntimeServerConfiguration(); diff --git a/carapace-server/src/test/java/org/carapaceproxy/utils/TestEndpointMapper.java b/carapace-server/src/test/java/org/carapaceproxy/utils/TestEndpointMapper.java index 30e75a21d..ee33f2e0d 100644 --- a/carapace-server/src/test/java/org/carapaceproxy/utils/TestEndpointMapper.java +++ b/carapace-server/src/test/java/org/carapaceproxy/utils/TestEndpointMapper.java @@ -56,7 +56,7 @@ public TestEndpointMapper(String host, int port) { } public TestEndpointMapper(String host, int port, boolean cacheAll) { - this(host, port, cacheAll, Map.of(host, new BackendConfiguration(host, host, port, "/index.html"))); + this(host, port, cacheAll, Map.of(host, new BackendConfiguration(host, host, port, "/index.html", -1))); } public TestEndpointMapper(@Nullable String host, int port, boolean cacheAll, Map backends) {