MCPcopy Create free account
hub / github.com/apache/cassandra-nodejs-driver / minInFlight

Method minInFlight

lib/host-connection-pool.js:130–167  ·  view source on GitHub ↗

* Gets the connection with the minimum number of in-flight requests. * Only checks for 2 connections (round-robin) and gets the one with minimum in-flight requests, as long as * the amount of in-flight requests is lower than maxRequests. * @param {Array. } connections * @param

(connections, maxRequests, previousConnection)

Source from the content-addressed store, hash-verified

128 * @returns {Connection!}
129 */
130 static minInFlight(connections, maxRequests, previousConnection) {
131 const length = connections.length;
132 if (length === 1) {
133 return connections[0];
134 }
135
136 // Use a single index for all hosts as a simplified way to balance the load between connections
137 connectionIndex++;
138 if (connectionIndex >= connectionIndexOverflow) {
139 connectionIndex = 0;
140 }
141
142 let current;
143 for (let index = connectionIndex; index < connectionIndex + length; index++) {
144 current = connections[index % length];
145 if (current === previousConnection) {
146 // Increment the index and skip
147 current = connections[(++index) % length];
148 }
149
150 let next = connections[(index + 1) % length];
151 if (next === previousConnection) {
152 // Skip
153 next = connections[(index + 2) % length];
154 }
155
156 if (next.getInFlight() < current.getInFlight()) {
157 current = next;
158 }
159
160 if (current.getInFlight() < maxRequests) {
161 // Check as few connections as possible, as long as the amount of in-flight
162 // requests is lower than maxRequests
163 break;
164 }
165 }
166 return current;
167 }
168
169 /**
170 * Creates all the connections in the pool and switches the keyspace of each connection if needed.

Callers 2

borrowConnectionMethod · 0.80
host-tests.jsFile · 0.80

Calls 1

getInFlightMethod · 0.45

Tested by

no test coverage detected