Skip to content

Commit 6184f2e

Browse files
committed
Bug fixing and cleanup.
- ensure total cached connections is decremented in all cases. - Remove deprecated API usage
1 parent f7bcdb0 commit 6184f2e

File tree

1 file changed

+42
-22
lines changed

1 file changed

+42
-22
lines changed

src/main/java/com/ning/http/client/providers/grizzly/GrizzlyConnectionsPool.java

Lines changed: 42 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,13 @@
1818
import com.ning.http.client.AsyncHttpClientConfig;
1919
import com.ning.http.client.ConnectionsPool;
2020

21+
import org.glassfish.grizzly.CloseListener;
22+
import org.glassfish.grizzly.CloseType;
2123
import org.glassfish.grizzly.Connection;
2224
import org.glassfish.grizzly.Grizzly;
2325
import org.glassfish.grizzly.attributes.Attribute;
24-
import org.glassfish.grizzly.attributes.NullaryFunction;
2526
import org.glassfish.grizzly.utils.DataStructures;
27+
import org.glassfish.grizzly.utils.NullaryFunction;
2628
import org.slf4j.Logger;
2729
import org.slf4j.LoggerFactory;
2830

@@ -62,12 +64,14 @@ public class GrizzlyConnectionsPool implements ConnectionsPool<String,Connection
6264

6365
private final boolean ownsDelayedExecutor;
6466

65-
private final Connection.CloseListener listener = new Connection.CloseListener() {
66-
@Override
67-
public void onClosed(Connection connection, Connection.CloseType closeType) throws IOException {
68-
if (closeType == Connection.CloseType.REMOTELY) {
67+
private final CloseListener listener =
68+
new CloseListener<Connection, CloseType>() {
69+
public void onClosed(Connection connection, CloseType closeType)
70+
throws IOException {
71+
if (closeType == CloseType.REMOTELY) {
6972
if (LOG.isInfoEnabled()) {
70-
LOG.info("Remote closed connection ({}). Removing from cache", connection.toString());
73+
LOG.info("Remote closed connection ({}). Removing from cache",
74+
connection.toString());
7175
}
7276
}
7377
GrizzlyConnectionsPool.this.removeAll(connection);
@@ -78,6 +82,7 @@ public void onClosed(Connection connection, Connection.CloseType closeType) thro
7882
// ------------------------------------------------------------ Constructors
7983

8084

85+
@SuppressWarnings("UnusedDeclaration")
8186
public GrizzlyConnectionsPool(final boolean cacheSSLConnections,
8287
final int timeout,
8388
final int maxConnectionLifeTimeInMs,
@@ -94,10 +99,14 @@ public GrizzlyConnectionsPool(final boolean cacheSSLConnections,
9499
this.delayedExecutor = delayedExecutor;
95100
ownsDelayedExecutor = false;
96101
} else {
97-
this.delayedExecutor = new DelayedExecutor(Executors.newSingleThreadExecutor());
98-
delayedExecutor.start();
102+
this.delayedExecutor =
103+
new DelayedExecutor(Executors.newSingleThreadExecutor(),
104+
this);
99105
ownsDelayedExecutor = true;
100106
}
107+
if (!this.delayedExecutor.isStarted) {
108+
this.delayedExecutor.start();
109+
}
101110
}
102111

103112

@@ -109,7 +118,7 @@ public GrizzlyConnectionsPool(final AsyncHttpClientConfig config) {
109118
maxConnectionsPerHost = config.getMaxConnectionPerHost();
110119
maxConnections = config.getMaxTotalConnections();
111120
unlimitedConnections = (maxConnections == -1);
112-
delayedExecutor = new DelayedExecutor(Executors.newSingleThreadExecutor());
121+
delayedExecutor = new DelayedExecutor(Executors.newSingleThreadExecutor(), this);
113122
delayedExecutor.start();
114123
ownsDelayedExecutor = true;
115124
}
@@ -148,13 +157,14 @@ public boolean offer(String uri, Connection connection) {
148157
final int total = totalCachedConnections.incrementAndGet();
149158
if (LOG.isDebugEnabled()) {
150159
LOG.debug("[offer] Pooling connection [{}] for uri [{}]. Current size (for host; before pooling): [{}]. Max size (for host): [{}]. Total number of cached connections: [{}].",
151-
new Object[]{connection, uri, size, maxConnectionsPerHost, total});
160+
connection, uri, size, maxConnectionsPerHost, total);
152161
}
153162
return true;
154163
}
155164
if (LOG.isDebugEnabled()) {
156165
LOG.debug("[offer] Unable to pool connection [{}] for uri [{}]. Current size (for host): [{}]. Max size (for host): [{}]. Total number of cached connections: [{}].",
157-
new Object[]{connection, uri, size, maxConnectionsPerHost, totalCachedConnections.get()});
166+
connection, uri, size, maxConnectionsPerHost,
167+
totalCachedConnections.get());
158168
}
159169

160170
return false;
@@ -217,6 +227,9 @@ public boolean removeAll(Connection connection) {
217227
boolean isRemoved = false;
218228
for (Map.Entry<String, DelayedExecutor.IdleConnectionQueue> entry : connectionsPool.entrySet()) {
219229
boolean removed = entry.getValue().remove(connection);
230+
if (removed) {
231+
totalCachedConnections.decrementAndGet();
232+
}
220233
isRemoved |= removed;
221234
}
222235
return isRemoved;
@@ -279,25 +292,31 @@ public static final class DelayedExecutor {
279292
private final Object sync = new Object();
280293
private volatile boolean isStarted;
281294
private final long checkIntervalMs;
295+
private final AtomicInteger totalCachedConnections;
282296

283297

284298
// -------------------------------------------------------- Constructors
285299

286300

287-
public DelayedExecutor(final ExecutorService threadPool) {
288-
this(threadPool, 1000, TimeUnit.MILLISECONDS);
301+
public DelayedExecutor(final ExecutorService threadPool,
302+
final GrizzlyConnectionsPool connectionsPool) {
303+
this(threadPool, 1000, TimeUnit.MILLISECONDS, connectionsPool);
289304
}
290305

291306

292-
// ----------------------------------------------------- Private Methods
293-
294307
public DelayedExecutor(final ExecutorService threadPool,
295308
final long checkInterval,
296-
final TimeUnit timeunit) {
309+
final TimeUnit timeunit,
310+
final GrizzlyConnectionsPool connectionsPool) {
297311
this.threadPool = threadPool;
298312
this.checkIntervalMs = TimeUnit.MILLISECONDS.convert(checkInterval, timeunit);
313+
totalCachedConnections = connectionsPool.totalCachedConnections;
299314
}
300315

316+
317+
// ----------------------------------------------------- Private Methods
318+
319+
301320
private void start() {
302321
synchronized (sync) {
303322
if (!isStarted) {
@@ -327,8 +346,8 @@ private IdleConnectionQueue createIdleConnectionQueue(final long timeout, final
327346
}
328347

329348
@SuppressWarnings({"NumberEquality"})
330-
private static boolean wasModified(final Long l1, final Long l2) {
331-
return l1 != l2 && (l1 != null ? !l1.equals(l2) : !l2.equals(l1));
349+
private static boolean wasModified(final long l1, final long l2) {
350+
return l1 != l2;
332351
}
333352

334353

@@ -352,7 +371,7 @@ public void run() {
352371
final Connection element = it.next();
353372
final Long timeoutMs = resolver.getTimeoutMs(element);
354373

355-
if (timeoutMs == null || timeoutMs == UNSET_TIMEOUT) {
374+
if (timeoutMs == UNSET_TIMEOUT) {
356375
it.remove();
357376
if (wasModified(timeoutMs,
358377
resolver.getTimeoutMs(element))) {
@@ -368,7 +387,8 @@ public void run() {
368387
if (LOG.isDebugEnabled()) {
369388
LOG.debug("Idle connection ({}) detected. Removing from cache.", element.toString());
370389
}
371-
element.close().markForRecycle(true);
390+
totalCachedConnections.decrementAndGet();
391+
element.close();
372392
} catch (Exception ignored) {
373393
}
374394
}
@@ -460,7 +480,7 @@ boolean isEmpty() {
460480

461481
void destroy() {
462482
for (Connection c : queue) {
463-
c.close().markForRecycle(true);
483+
c.close();
464484
}
465485
queue.clear();
466486
queues.remove(this);
@@ -494,7 +514,7 @@ boolean removeTimeout(final Connection c) {
494514
return true;
495515
}
496516

497-
Long getTimeoutMs(final Connection c) {
517+
long getTimeoutMs(final Connection c) {
498518
return IDLE_ATTR.get(c).timeoutMs;
499519
}
500520

0 commit comments

Comments
 (0)