@@ -194,7 +194,6 @@ public enum ConfirmType {
194
194
195
195
private final Condition connectionAvailableCondition = this .connectionLock .newCondition ();
196
196
197
-
198
197
private final ActiveObjectCounter <Channel > inFlightAsyncCloses = new ActiveObjectCounter <>();
199
198
200
199
private final AtomicBoolean running = new AtomicBoolean ();
@@ -1156,7 +1155,7 @@ public Object invoke(Object proxy, Method method, Object[] args) throws Throwabl
1156
1155
return null ;
1157
1156
}
1158
1157
else {
1159
- physicalClose (channelProxy );
1158
+ physicalClose ();
1160
1159
return null ;
1161
1160
}
1162
1161
}
@@ -1237,29 +1236,16 @@ else if (txEnds.contains(methodName)) {
1237
1236
}
1238
1237
}
1239
1238
1240
- private void releasePermitIfNecessary (ChannelProxy proxy ) {
1239
+ private void releasePermitIfNecessary () {
1241
1240
if (CachingConnectionFactory .this .channelCheckoutTimeout > 0 ) {
1242
- /*
1243
- * Only release a permit if this is a normal close; if the channel is
1244
- * in the list, it means we're closing a cached channel (for which a permit
1245
- * has already been released).
1246
- */
1247
-
1248
- this .theConnection .channelListLock .lock ();
1249
- try {
1250
- if (this .channelList .contains (proxy )) {
1251
- return ;
1252
- }
1253
- }
1254
- finally {
1255
- this .theConnection .channelListLock .unlock ();
1256
- }
1257
1241
Semaphore permits = CachingConnectionFactory .this .checkoutPermits .get (this .theConnection );
1258
1242
if (permits != null ) {
1259
- permits .release ();
1260
- if (logger .isDebugEnabled ()) {
1261
- logger .debug ("Released permit for '" + this .theConnection + "', remaining: "
1262
- + permits .availablePermits ());
1243
+ if (permits .availablePermits () < CachingConnectionFactory .this .channelCacheSize ) {
1244
+ permits .release ();
1245
+ if (logger .isDebugEnabled ()) {
1246
+ logger .debug ("Released permit for '" + this .theConnection + "', remaining: "
1247
+ + permits .availablePermits ());
1248
+ }
1263
1249
}
1264
1250
}
1265
1251
else {
@@ -1285,11 +1271,8 @@ private void logicalClose(ChannelProxy proxy) throws IOException, TimeoutExcepti
1285
1271
if (this .target instanceof PublisherCallbackChannel ) {
1286
1272
this .target .close (); // emit nacks if necessary
1287
1273
}
1288
- if (this .channelList .contains (proxy )) {
1289
- this .channelList .remove (proxy );
1290
- }
1291
- else {
1292
- releasePermitIfNecessary (proxy );
1274
+ if (!this .channelList .remove (proxy )) {
1275
+ releasePermitIfNecessary ();
1293
1276
}
1294
1277
this .target = null ;
1295
1278
return ;
@@ -1328,7 +1311,7 @@ private void returnToCache(ChannelProxy proxy) {
1328
1311
// The channel didn't handle confirms, so close it altogether to avoid
1329
1312
// memory leaks for pending confirms
1330
1313
try {
1331
- physicalClose (this . theConnection . channelsAwaitingAcks . remove ( this . target ) );
1314
+ physicalClose ();
1332
1315
}
1333
1316
catch (@ SuppressWarnings (UNUSED ) Exception e ) {
1334
1317
}
@@ -1352,7 +1335,7 @@ private void doReturnToCache(@Nullable ChannelProxy proxy) {
1352
1335
else {
1353
1336
if (proxy .isOpen ()) {
1354
1337
try {
1355
- physicalClose (proxy );
1338
+ physicalClose ();
1356
1339
}
1357
1340
catch (@ SuppressWarnings (UNUSED ) Exception e ) {
1358
1341
}
@@ -1372,7 +1355,7 @@ private void cacheOrClose(ChannelProxy proxy) {
1372
1355
logger .trace ("Cache limit reached: " + this .target );
1373
1356
}
1374
1357
try {
1375
- physicalClose (proxy );
1358
+ physicalClose ();
1376
1359
}
1377
1360
catch (@ SuppressWarnings (UNUSED ) Exception e ) {
1378
1361
}
@@ -1381,8 +1364,8 @@ else if (!alreadyCached) {
1381
1364
if (logger .isTraceEnabled ()) {
1382
1365
logger .trace ("Returning cached Channel: " + this .target );
1383
1366
}
1384
- releasePermitIfNecessary (proxy );
1385
1367
this .channelList .addLast (proxy );
1368
+ releasePermitIfNecessary ();
1386
1369
setHighWaterMark ();
1387
1370
}
1388
1371
}
@@ -1399,7 +1382,7 @@ private void setHighWaterMark() {
1399
1382
}
1400
1383
}
1401
1384
1402
- private void physicalClose (ChannelProxy proxy ) throws IOException , TimeoutException {
1385
+ private void physicalClose () throws IOException , TimeoutException {
1403
1386
if (logger .isDebugEnabled ()) {
1404
1387
logger .debug ("Closing cached Channel: " + this .target );
1405
1388
}
@@ -1413,7 +1396,7 @@ private void physicalClose(ChannelProxy proxy) throws IOException, TimeoutExcept
1413
1396
(ConfirmType .CORRELATED .equals (CachingConnectionFactory .this .confirmType ) ||
1414
1397
CachingConnectionFactory .this .publisherReturns )) {
1415
1398
async = true ;
1416
- asyncClose (proxy );
1399
+ asyncClose ();
1417
1400
}
1418
1401
else {
1419
1402
this .target .close ();
@@ -1430,12 +1413,12 @@ private void physicalClose(ChannelProxy proxy) throws IOException, TimeoutExcept
1430
1413
finally {
1431
1414
this .target = null ;
1432
1415
if (!async ) {
1433
- releasePermitIfNecessary (proxy );
1416
+ releasePermitIfNecessary ();
1434
1417
}
1435
1418
}
1436
1419
}
1437
1420
1438
- private void asyncClose (ChannelProxy proxy ) {
1421
+ private void asyncClose () {
1439
1422
ExecutorService executorService = getChannelsExecutor ();
1440
1423
final Channel channel = CachedChannelInvocationHandler .this .target ;
1441
1424
CachingConnectionFactory .this .inFlightAsyncCloses .add (channel );
@@ -1467,7 +1450,7 @@ private void asyncClose(ChannelProxy proxy) {
1467
1450
}
1468
1451
finally {
1469
1452
CachingConnectionFactory .this .inFlightAsyncCloses .release (channel );
1470
- releasePermitIfNecessary (proxy );
1453
+ releasePermitIfNecessary ();
1471
1454
}
1472
1455
}
1473
1456
});
0 commit comments