Source:
private List<NetworkSend> prepareSends(List<ResponseInfo> responseInfoList) { List<NetworkSend> sends = new ArrayList<>(); ListIterator<RequestMetadata> iter = pendingRequests.listIterator(); while (iter.hasNext()) { RequestMetadata requestMetadata = iter.next(); if (time.milliseconds() - requestMetadata.requestQueuedAtMs > checkoutTimeoutMs) { responseInfoList .add(new ResponseInfo(requestMetadata.requestInfo, NetworkClientErrorCode.ConnectionUnavailable, null)); logger.trace("Failing request to host port due to connection unavailability", requestMetadata.requestInfo.getHost(), requestMetadata.requestInfo.getPort()); iter.remove(); networkMetrics.connectionTimeOutError.inc(); } else { iter.previous(); break; } } while (iter.hasNext()) { RequestMetadata requestMetadata = iter.next(); try { String host = requestMetadata.requestInfo.getHost(); Port port = requestMetadata.requestInfo.getPort(); String connId = connectionTracker.checkOutConnection(host, port); if (connId == null) { if (requestMetadata.pendingConnectionId == null && connectionTracker.mayCreateNewConnection(host, port)) { connId = selector.connect(new InetSocketAddress(host, port.getPort()), networkConfig.socketSendBufferBytes, networkConfig.socketReceiveBufferBytes, port.getPortType()); connectionTracker.startTrackingInitiatedConnection(host, port, connId); requestMetadata.pendingConnectionId = connId; pendingConnectionsToAssociatedRequests.put(connId, requestMetadata); logger.trace("Initiated a connection to host port ", host, port); } } else { if (requestMetadata.pendingConnectionId != null) { requestMetadata.pendingConnectionId = null; } logger.trace("Connection checkout succeeded for {}:{} with connectionId ", host, port, connId); sends.add(new NetworkSend(connId, requestMetadata.requestInfo.getRequest(), requestMetadata.clientNetworkRequestMetrics, time)); connectionIdToRequestInFlight.put(connId, requestMetadata); iter.remove(); requestMetadata.onRequestDequeue(); } } catch (IOException e) { networkMetrics.networkClientIOError.inc(); logger.error("Received exception while checking out a connection", e); } } return sends; }

Target:
private List<NetworkSend> prepareSends(List<ResponseInfo> responseInfoList) { List<NetworkSend> sends = new ArrayList<>(); ListIterator<RequestMetadata> iter = pendingRequests.listIterator(); while (iter.hasNext()) { RequestMetadata requestMetadata = iter.next(); if (time.milliseconds() - requestMetadata.requestQueuedAtMs > checkoutTimeoutMs) { responseInfoList .add(new ResponseInfo(requestMetadata.requestInfo, NetworkClientErrorCode.ConnectionUnavailable, null)); logger.trace("Failing request to host port due to connection unavailability", requestMetadata.requestInfo.getHost(), requestMetadata.requestInfo.getPort()); iter.remove(); networkMetrics.connectionTimeOutError.inc(); } else { iter.previous(); break; } } while (iter.hasNext()) { RequestMetadata requestMetadata = iter.next(); try { String host = requestMetadata.requestInfo.getHost(); Port port = requestMetadata.requestInfo.getPort(); String connId = connectionTracker.checkOutConnection(host, port); if (connId == null) { if (requestMetadata.pendingConnectionId == null && connectionTracker.mayCreateNewConnection(host, port)) { connId = selector.connect(new InetSocketAddress(host, port.getPort()), networkConfig.socketSendBufferBytes, networkConfig.socketReceiveBufferBytes, port.getPortType()); connectionTracker.startTrackingInitiatedConnection(host, port, connId); requestMetadata.pendingConnectionId = connId; pendingConnectionsToAssociatedRequests.put(connId, requestMetadata); logger.trace("Initiated a connection to host port ", host, port); } } else { if (requestMetadata.pendingConnectionId != null) { pendingConnectionsToAssociatedRequests.remove(requestMetadata.pendingConnectionId); requestMetadata.pendingConnectionId = null; } logger.trace("Connection checkout succeeded for {}:{} with connectionId ", host, port, connId); sends.add(new NetworkSend(connId, requestMetadata.requestInfo.getRequest(), requestMetadata.clientNetworkRequestMetrics, time)); connectionIdToRequestInFlight.put(connId, requestMetadata); iter.remove(); requestMetadata.onRequestDequeue(); } } catch (IOException e) { networkMetrics.networkClientIOError.inc(); logger.error("Received exception while checking out a connection", e); } } return sends; }