ConfigServerRestExecutorImpl extends AbstractComponent implements ConfigServerRestExecutor { @Override public ProxyResponse handle(ProxyRequest request) { List<URI> targets = new ArrayList<>(request.getTargets()); StringBuilder errorBuilder = new StringBuilder(); boolean singleTarget = targets.size() == 1; if (singleTarget) { for (int i = 0; i < SINGLE_TARGET_RETRIES - 1; i++) { targets.add(targets.get(0)); } } else if (queueFirstServerIfDown(targets)) { errorBuilder.append("Change ordering due to failed ping."); } for (URI url : targets) { Optional<ProxyResponse> proxyResponse = proxy(request, url, errorBuilder); if (proxyResponse.isPresent()) { return proxyResponse.get(); } if (singleTarget) { sleeper.sleep(SINGLE_TARGET_WAIT); } } throw new RuntimeException("Failed talking to config servers: " + errorBuilder.toString()); } @Inject  ConfigServerRestExecutorImpl(ZoneRegistry zoneRegistry, ServiceIdentityProvider sslContextProvider);   ConfigServerRestExecutorImpl(ZoneRegistry zoneRegistry, SSLContext sslContext,
                                 Sleeper sleeper, ConnectionReuseStrategy connectionReuseStrategy); @Override ProxyResponse handle(ProxyRequest request); @Override void deconstruct();  }
@Test public void proxy_with_retries() throws Exception { var connectionReuseStrategy = new CountingConnectionReuseStrategy(Set.of("127.0.0.1")); var proxy = new ConfigServerRestExecutorImpl(new ZoneRegistryMock(SystemName.cd), SSLContext.getDefault(), (duration) -> {}, connectionReuseStrategy); URI url = url(); String path = url.getPath(); stubRequests(path); HttpRequest request = HttpRequest.createTestRequest(url.toString(), com.yahoo.jdisc.http.HttpRequest.Method.GET); ProxyRequest proxyRequest = ProxyRequest.tryOne(url, path, request); HttpResponse response = proxy.handle(proxyRequest); wireMock.verify(3, getRequestedFor(urlEqualTo(path))); assertEquals(200, response.getStatus()); ByteArrayOutputStream out = new ByteArrayOutputStream(); response.render(out); assertEquals("OK", out.toString()); assertEquals(0, connectionReuseStrategy.reusedConnections.get(url.getHost()).intValue()); }
@Test public void proxy_without_connection_reuse() throws Exception { var connectionReuseStrategy = new CountingConnectionReuseStrategy(Set.of()); var proxy = new ConfigServerRestExecutorImpl(new ZoneRegistryMock(SystemName.cd), SSLContext.getDefault(), (duration) -> {}, connectionReuseStrategy); URI url = url(); String path = url.getPath(); stubRequests(path); HttpRequest request = HttpRequest.createTestRequest(url.toString(), com.yahoo.jdisc.http.HttpRequest.Method.GET); ProxyRequest proxyRequest = ProxyRequest.tryOne(url, path, request); assertEquals(200, proxy.handle(proxyRequest).getStatus()); assertEquals(3, connectionReuseStrategy.reusedConnections.get(url.getHost()).intValue()); }