@@ -1121,7 +1121,7 @@ else if (methodName.equals("close")) {
1121
1121
return null ;
1122
1122
}
1123
1123
else {
1124
- physicalClose (proxy );
1124
+ physicalClose ();
1125
1125
return null ;
1126
1126
}
1127
1127
}
@@ -1193,24 +1193,16 @@ else if (txEnds.contains(methodName)) {
1193
1193
}
1194
1194
}
1195
1195
1196
- private void releasePermitIfNecessary (Object proxy ) {
1196
+ private void releasePermitIfNecessary () {
1197
1197
if (CachingConnectionFactory .this .channelCheckoutTimeout > 0 ) {
1198
- /*
1199
- * Only release a permit if this is a normal close; if the channel is
1200
- * in the list, it means we're closing a cached channel (for which a permit
1201
- * has already been released).
1202
- */
1203
- synchronized (this .channelList ) {
1204
- if (this .channelList .contains (proxy )) {
1205
- return ;
1206
- }
1207
- }
1208
1198
Semaphore permits = CachingConnectionFactory .this .checkoutPermits .get (this .theConnection );
1209
1199
if (permits != null ) {
1210
- permits .release ();
1211
- if (logger .isDebugEnabled ()) {
1212
- logger .debug ("Released permit for '" + this .theConnection + "', remaining: "
1213
- + permits .availablePermits ());
1200
+ if (permits .availablePermits () < CachingConnectionFactory .this .channelCacheSize ) {
1201
+ permits .release ();
1202
+ if (logger .isDebugEnabled ()) {
1203
+ logger .debug ("Released permit for '" + this .theConnection + "', remaining: "
1204
+ + permits .availablePermits ());
1205
+ }
1214
1206
}
1215
1207
}
1216
1208
else {
@@ -1235,11 +1227,8 @@ private void logicalClose(ChannelProxy proxy) throws IOException, TimeoutExcepti
1235
1227
if (this .target instanceof PublisherCallbackChannel ) {
1236
1228
this .target .close (); // emit nacks if necessary
1237
1229
}
1238
- if (this .channelList .contains (proxy )) {
1239
- this .channelList .remove (proxy );
1240
- }
1241
- else {
1242
- releasePermitIfNecessary (proxy );
1230
+ if (!this .channelList .remove (proxy )) {
1231
+ releasePermitIfNecessary ();
1243
1232
}
1244
1233
this .target = null ;
1245
1234
return ;
@@ -1275,7 +1264,7 @@ private void returnToCache(ChannelProxy proxy) {
1275
1264
// The channel didn't handle confirms, so close it altogether to avoid
1276
1265
// memory leaks for pending confirms
1277
1266
try {
1278
- physicalClose (this . theConnection . channelsAwaitingAcks . remove ( this . target ) );
1267
+ physicalClose ();
1279
1268
}
1280
1269
catch (@ SuppressWarnings (UNUSED ) Exception e ) {
1281
1270
}
@@ -1298,7 +1287,7 @@ private void doReturnToCache(Channel proxy) {
1298
1287
else {
1299
1288
if (proxy .isOpen ()) {
1300
1289
try {
1301
- physicalClose (proxy );
1290
+ physicalClose ();
1302
1291
}
1303
1292
catch (@ SuppressWarnings (UNUSED ) Exception e ) {
1304
1293
}
@@ -1315,7 +1304,7 @@ private void cacheOrClose(Channel proxy) {
1315
1304
logger .trace ("Cache limit reached: " + this .target );
1316
1305
}
1317
1306
try {
1318
- physicalClose (proxy );
1307
+ physicalClose ();
1319
1308
}
1320
1309
catch (@ SuppressWarnings (UNUSED ) Exception e ) {
1321
1310
}
@@ -1324,8 +1313,8 @@ else if (!alreadyCached) {
1324
1313
if (logger .isTraceEnabled ()) {
1325
1314
logger .trace ("Returning cached Channel: " + this .target );
1326
1315
}
1327
- releasePermitIfNecessary (proxy );
1328
1316
this .channelList .addLast ((ChannelProxy ) proxy );
1317
+ releasePermitIfNecessary ();
1329
1318
setHighWaterMark ();
1330
1319
}
1331
1320
}
@@ -1342,7 +1331,7 @@ private void setHighWaterMark() {
1342
1331
}
1343
1332
}
1344
1333
1345
- private void physicalClose (Object proxy ) throws IOException , TimeoutException {
1334
+ private void physicalClose () throws IOException , TimeoutException {
1346
1335
if (logger .isDebugEnabled ()) {
1347
1336
logger .debug ("Closing cached Channel: " + this .target );
1348
1337
}
@@ -1356,7 +1345,7 @@ private void physicalClose(Object proxy) throws IOException, TimeoutException {
1356
1345
(ConfirmType .CORRELATED .equals (CachingConnectionFactory .this .confirmType ) ||
1357
1346
CachingConnectionFactory .this .publisherReturns )) {
1358
1347
async = true ;
1359
- asyncClose (proxy );
1348
+ asyncClose ();
1360
1349
}
1361
1350
else {
1362
1351
this .target .close ();
@@ -1373,12 +1362,12 @@ private void physicalClose(Object proxy) throws IOException, TimeoutException {
1373
1362
finally {
1374
1363
this .target = null ;
1375
1364
if (!async ) {
1376
- releasePermitIfNecessary (proxy );
1365
+ releasePermitIfNecessary ();
1377
1366
}
1378
1367
}
1379
1368
}
1380
1369
1381
- private void asyncClose (Object proxy ) {
1370
+ private void asyncClose () {
1382
1371
ExecutorService executorService = getChannelsExecutor ();
1383
1372
final Channel channel = CachedChannelInvocationHandler .this .target ;
1384
1373
CachingConnectionFactory .this .inFlightAsyncCloses .add (channel );
@@ -1414,7 +1403,7 @@ private void asyncClose(Object proxy) {
1414
1403
}
1415
1404
finally {
1416
1405
CachingConnectionFactory .this .inFlightAsyncCloses .release (channel );
1417
- releasePermitIfNecessary (proxy );
1406
+ releasePermitIfNecessary ();
1418
1407
}
1419
1408
}
1420
1409
});
0 commit comments