28
28
import io .netty .handler .codec .DecoderException ;
29
29
import io .netty .util .AttributeKey ;
30
30
import io .netty .util .concurrent .Promise ;
31
+ import org .slf4j .Logger ;
32
+ import org .slf4j .LoggerFactory ;
33
+
34
+ import javax .annotation .Nullable ;
31
35
import java .net .InetAddress ;
32
36
import java .net .InetSocketAddress ;
33
37
import java .net .SocketAddress ;
37
41
import java .util .concurrent .ConcurrentLinkedDeque ;
38
42
import java .util .concurrent .atomic .AtomicInteger ;
39
43
import java .util .concurrent .atomic .AtomicReference ;
40
- import javax .annotation .Nullable ;
41
- import org .slf4j .Logger ;
42
- import org .slf4j .LoggerFactory ;
43
44
44
45
/**
45
46
@@ -166,12 +167,7 @@ public Promise<PooledConnection> acquire(
166
167
final PooledConnection conn = tryGettingFromConnectionPool (eventLoop );
167
168
if (conn != null ) {
168
169
// There was a pooled connection available, so use this one.
169
- conn .startRequestTimer ();
170
- conn .incrementUsageCount ();
171
- conn .getChannel ().read ();
172
- onAcquire (conn , passport );
173
- initPooledConnection (conn , promise );
174
- selectedHostAddr .set (getSelectedHostString (serverAddr ));
170
+ reusePooledConnection (passport , selectedHostAddr , conn , promise );
175
171
} else {
176
172
// connection pool empty, create new connection using client connection factory.
177
173
tryMakingNewConnection (eventLoop , promise , passport , selectedHostAddr );
@@ -180,6 +176,15 @@ public Promise<PooledConnection> acquire(
180
176
return promise ;
181
177
}
182
178
179
+ protected void reusePooledConnection (CurrentPassport passport , AtomicReference <? super InetAddress > selectedHostAddr , PooledConnection conn , Promise <PooledConnection > promise ) {
180
+ conn .startRequestTimer ();
181
+ conn .incrementUsageCount ();
182
+ conn .getChannel ().read ();
183
+ onAcquire (conn , passport );
184
+ initPooledConnection (conn , promise );
185
+ selectedHostAddr .set (getSelectedHostString (serverAddr ));
186
+ }
187
+
183
188
protected void updateServerStatsOnAcquire () {
184
189
server .incrementActiveRequestsCount ();
185
190
}
@@ -232,21 +237,8 @@ protected void tryMakingNewConnection(
232
237
Promise <PooledConnection > promise ,
233
238
CurrentPassport passport ,
234
239
AtomicReference <? super InetAddress > selectedHostAddr ) {
235
- // Enforce MaxConnectionsPerHost config.
236
- int maxConnectionsPerHost = config .maxConnectionsPerHost ();
237
- int openAndOpeningConnectionCount = server .getOpenConnectionsCount () + connCreationsInProgress .get ();
238
- if (maxConnectionsPerHost != -1 && openAndOpeningConnectionCount >= maxConnectionsPerHost ) {
239
- maxConnsPerHostExceededCounter .increment ();
240
- promise .setFailure (new OriginConnectException (
241
- "maxConnectionsPerHost=" + maxConnectionsPerHost + ", connectionsPerHost="
242
- + openAndOpeningConnectionCount ,
243
- OutboundErrorType .ORIGIN_SERVER_MAX_CONNS ));
244
- LOG .warn (
245
- "Unable to create new connection because at MaxConnectionsPerHost! maxConnectionsPerHost={}, connectionsPerHost={}, host={}origin={}" ,
246
- maxConnectionsPerHost ,
247
- openAndOpeningConnectionCount ,
248
- server .getServerId (),
249
- config .getOriginName ());
240
+
241
+ if (!isWithinConnectionLimit (promise )) {
250
242
return ;
251
243
}
252
244
@@ -281,6 +273,27 @@ protected void tryMakingNewConnection(
281
273
}
282
274
}
283
275
276
+ protected boolean isWithinConnectionLimit (Promise <PooledConnection > promise ) {
277
+ // Enforce MaxConnectionsPerHost config.
278
+ int maxConnectionsPerHost = config .maxConnectionsPerHost ();
279
+ int openAndOpeningConnectionCount = server .getOpenConnectionsCount () + connCreationsInProgress .get ();
280
+ if (maxConnectionsPerHost != -1 && openAndOpeningConnectionCount >= maxConnectionsPerHost ) {
281
+ maxConnsPerHostExceededCounter .increment ();
282
+ promise .setFailure (new OriginConnectException (
283
+ "maxConnectionsPerHost=" + maxConnectionsPerHost + ", connectionsPerHost="
284
+ + openAndOpeningConnectionCount ,
285
+ OutboundErrorType .ORIGIN_SERVER_MAX_CONNS ));
286
+ LOG .warn (
287
+ "Unable to create new connection because at MaxConnectionsPerHost! maxConnectionsPerHost={}, connectionsPerHost={}, host={}origin={}" ,
288
+ maxConnectionsPerHost ,
289
+ openAndOpeningConnectionCount ,
290
+ server .getServerId (),
291
+ config .getOriginName ());
292
+ return false ;
293
+ }
294
+ return true ;
295
+ }
296
+
284
297
protected ChannelFuture connectToServer (EventLoop eventLoop , CurrentPassport passport , SocketAddress serverAddr ) {
285
298
return connectionFactory .connect (eventLoop , serverAddr , passport , this );
286
299
}
@@ -354,8 +367,7 @@ public boolean release(PooledConnection conn) {
354
367
CurrentPassport passport = CurrentPassport .fromChannel (conn .getChannel ());
355
368
356
369
// Discard conn if already at least above waterline in the pool already for this server.
357
- int poolWaterline = config .perServerWaterline ();
358
- if (poolWaterline > -1 && connections .size () >= poolWaterline ) {
370
+ if (isOverPerServerWaterline (connections .size ())) {
359
371
closeAboveHighWaterMarkCounter .increment ();
360
372
conn .close ();
361
373
conn .setInPool (false );
@@ -375,6 +387,11 @@ else if (connections.offer(conn)) {
375
387
}
376
388
}
377
389
390
+ protected boolean isOverPerServerWaterline (int connectionsInPool ) {
391
+ int poolWaterline = config .perServerWaterline ();
392
+ return poolWaterline > -1 && connectionsInPool >= poolWaterline ;
393
+ }
394
+
378
395
@ Override
379
396
public boolean remove (PooledConnection conn ) {
380
397
if (conn == null ) {
@@ -428,7 +445,7 @@ public int getConnsInUse() {
428
445
}
429
446
430
447
@ Nullable
431
- private static InetAddress getSelectedHostString (SocketAddress addr ) {
448
+ protected InetAddress getSelectedHostString (SocketAddress addr ) {
432
449
if (addr instanceof InetSocketAddress ) {
433
450
return ((InetSocketAddress ) addr ).getAddress ();
434
451
} else {
0 commit comments