Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 0b4c52a

Browse files
committed
PR comments addressed, tests fixed
1 parent 273cf24 commit 0b4c52a

File tree

12 files changed

+276
-82
lines changed

12 files changed

+276
-82
lines changed

‎client/src/main/java/org/asynchttpclient/AsyncHttpClient.java‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -266,4 +266,11 @@ public interface AsyncHttpClient extends Closeable {
266266
* @return a {@link Future} of type Response
267267
*/
268268
ListenableFuture<Response> executeRequest(RequestBuilder requestBuilder);
269+
270+
/***
271+
* Return details about pooled connections.
272+
*
273+
* @return a {@link ClientStats}
274+
*/
275+
ClientStats getClientStats();
269276
}
Lines changed: 48 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,78 @@
1+
/*
2+
* Copyright 2010 Ning, Inc.
3+
*
4+
* This program is licensed to you under the Apache License, version 2.0
5+
* (the "License"); you may not use this file except in compliance with the
6+
* License. You may obtain a copy of the License at:
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12+
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13+
* License for the specific language governing permissions and limitations
14+
* under the License.
15+
*
16+
*/
117
package org.asynchttpclient;
218

19+
import java.util.Objects;
20+
321
/**
4-
* Created by grenville on 9/24/16.
22+
* A record class representing the state of an (@link org.asynchttpclient.AsyncHttpClient)
523
*/
624
public class ClientStats {
725

8-
private final long totalConnectionCount;
926
private final long activeConnectionCount;
1027
private final long idleConnectionCount;
1128

1229
public ClientStats(final long activeConnectionCount,
1330
final long idleConnectionCount) {
14-
this.totalConnectionCount = activeConnectionCount + idleConnectionCount;
1531
this.activeConnectionCount = activeConnectionCount;
1632
this.idleConnectionCount = idleConnectionCount;
1733
}
1834

35+
/**
36+
* @return The sum of {@link #getActiveConnectionCount()} and {@link #getIdleConnectionCount()},
37+
* a long representing the total number of connections in the connection pool.
38+
*/
1939
public long getTotalConnectionCount() {
20-
return totalConnectionCount;
40+
return activeConnectionCount + idleConnectionCount;
2141
}
2242

43+
/**
44+
* @return A long representing the number of active connection in the connection pool.
45+
*/
2346
public long getActiveConnectionCount() {
2447
return activeConnectionCount;
2548
}
2649

50+
/**
51+
*
52+
* @return A long representing the number of idle connections in the connection pool.
53+
*/
2754
public long getIdleConnectionCount() {
2855
return idleConnectionCount;
2956
}
3057

3158
@Override
3259
public String toString() {
33-
return "There are " + totalConnectionCount +
34-
" total connections, " + activeConnectionCount +
35-
" are active and " + idleConnectionCount + " are idle.";
60+
return "There are " + getTotalConnectionCount() +
61+
" total connections, " + getActiveConnectionCount() +
62+
" are active and " + getIdleConnectionCount() + " are idle.";
63+
}
64+
65+
@Override
66+
public boolean equals(final Object o) {
67+
if (this == o) return true;
68+
if (o == null || getClass() != o.getClass()) return false;
69+
final ClientStats that = (ClientStats) o;
70+
return activeConnectionCount == that.activeConnectionCount &&
71+
idleConnectionCount == that.idleConnectionCount;
72+
}
73+
74+
@Override
75+
public int hashCode() {
76+
return Objects.hash(activeConnectionCount, idleConnectionCount);
3677
}
3778
}

‎client/src/main/java/org/asynchttpclient/DefaultAsyncHttpClient.java‎

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,6 @@
1818

1919
import static org.asynchttpclient.util.Assertions.assertNotNull;
2020

21-
import io.netty.channel.Channel;
22-
import io.netty.channel.EventLoopGroup;
23-
import io.netty.util.HashedWheelTimer;
24-
import io.netty.util.Timer;
25-
2621
import java.util.concurrent.atomic.AtomicBoolean;
2722

2823
import org.asynchttpclient.channel.ChannelPool;
@@ -35,6 +30,10 @@
3530
import org.slf4j.Logger;
3631
import org.slf4j.LoggerFactory;
3732

33+
import io.netty.channel.EventLoopGroup;
34+
import io.netty.util.HashedWheelTimer;
35+
import io.netty.util.Timer;
36+
3837
/**
3938
* Default and threadsafe implementation of {@link AsyncHttpClient}.
4039
*/
@@ -257,11 +256,9 @@ public EventLoopGroup getEventLoopGroup() {
257256
return channelManager.getEventLoopGroup();
258257
}
259258

259+
@Override
260260
public ClientStats getClientStats() {
261-
final ChannelManager channelManager = requestSender.getChannelManager();
262-
final long activeConnectionCount = channelManager.getOpenChannels().stream().filter(channel -> !channel.isOpen()).count();
263-
final long idleConnectionCount = channelManager.getOpenChannels().stream().filter(Channel::isOpen).count();
264-
return new ClientStats(activeConnectionCount, idleConnectionCount);
261+
return channelManager.getClientStats();
265262
}
266263

267264
protected BoundRequestBuilder requestBuilder(String method, String url) {

‎client/src/main/java/org/asynchttpclient/channel/ChannelPool.java‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,4 +70,9 @@ public interface ChannelPool {
7070
* @param selector the selector
7171
*/
7272
void flushPartitions(ChannelPoolPartitionSelector selector);
73+
74+
/**
75+
* @return The number of idle channels.
76+
*/
77+
long getIdleChannelCount();
7378
}

‎client/src/main/java/org/asynchttpclient/channel/NoopChannelPool.java‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,4 +50,11 @@ public void flushPartition(Object partitionKey) {
5050
@Override
5151
public void flushPartitions(ChannelPoolPartitionSelector selector) {
5252
}
53+
54+
@Override
55+
public long getIdleChannelCount() {
56+
return 0;
57+
}
58+
59+
5360
}

‎client/src/main/java/org/asynchttpclient/netty/channel/ChannelManager.java‎

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
package org.asynchttpclient.netty.channel;
1515

1616
import static org.asynchttpclient.util.MiscUtils.trimStackTrace;
17+
1718
import io.netty.bootstrap.Bootstrap;
1819
import io.netty.bootstrap.ChannelFactory;
1920
import io.netty.buffer.ByteBufAllocator;
@@ -45,12 +46,12 @@
4546
import java.util.concurrent.Semaphore;
4647
import java.util.concurrent.ThreadFactory;
4748
import java.util.concurrent.TimeUnit;
48-
4949
import javax.net.ssl.SSLEngine;
5050
import javax.net.ssl.SSLException;
5151

5252
import org.asynchttpclient.AsyncHandler;
5353
import org.asynchttpclient.AsyncHttpClientConfig;
54+
import org.asynchttpclient.ClientStats;
5455
import org.asynchttpclient.SslEngineFactory;
5556
import org.asynchttpclient.channel.ChannelPool;
5657
import org.asynchttpclient.channel.ChannelPoolPartitioning;
@@ -71,6 +72,28 @@
7172
import org.slf4j.Logger;
7273
import org.slf4j.LoggerFactory;
7374

75+
import io.netty.bootstrap.Bootstrap;
76+
import io.netty.bootstrap.ChannelFactory;
77+
import io.netty.buffer.PooledByteBufAllocator;
78+
import io.netty.buffer.UnpooledByteBufAllocator;
79+
import io.netty.channel.*;
80+
import io.netty.channel.group.ChannelGroup;
81+
import io.netty.channel.group.DefaultChannelGroup;
82+
import io.netty.channel.nio.NioEventLoopGroup;
83+
import io.netty.channel.oio.OioEventLoopGroup;
84+
import io.netty.handler.codec.http.HttpClientCodec;
85+
import io.netty.handler.codec.http.HttpContentDecompressor;
86+
import io.netty.handler.codec.http.websocketx.WebSocket08FrameDecoder;
87+
import io.netty.handler.codec.http.websocketx.WebSocket08FrameEncoder;
88+
import io.netty.handler.codec.http.websocketx.WebSocketFrameAggregator;
89+
import io.netty.handler.logging.LogLevel;
90+
import io.netty.handler.logging.LoggingHandler;
91+
import io.netty.handler.ssl.SslHandler;
92+
import io.netty.handler.stream.ChunkedWriteHandler;
93+
import io.netty.util.Timer;
94+
import io.netty.util.concurrent.DefaultThreadFactory;
95+
import io.netty.util.concurrent.GlobalEventExecutor;
96+
7497
public class ChannelManager {
7598

7699
private static final Logger LOGGER = LoggerFactory.getLogger(ChannelManager.class);
@@ -489,7 +512,10 @@ public EventLoopGroup getEventLoopGroup() {
489512
return eventLoopGroup;
490513
}
491514

492-
public ChannelGroup getOpenChannels() {
493-
return openChannels;
515+
public ClientStats getClientStats() {
516+
final long totalConnectionCount = openChannels.size();
517+
final long idleConnectionCount = channelPool.getIdleChannelCount();
518+
final long activeConnectionCount = totalConnectionCount - idleConnectionCount;
519+
return new ClientStats(activeConnectionCount, idleConnectionCount);
494520
}
495521
}

‎client/src/main/java/org/asynchttpclient/netty/channel/DefaultChannelPool.java‎

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,6 @@
1515

1616
import static org.asynchttpclient.util.Assertions.assertNotNull;
1717
import static org.asynchttpclient.util.DateUtils.unpreciseMillisTime;
18-
import io.netty.channel.Channel;
19-
import io.netty.channel.ChannelId;
20-
import io.netty.util.Timeout;
21-
import io.netty.util.Timer;
22-
import io.netty.util.TimerTask;
2318

2419
import java.util.*;
2520
import java.util.concurrent.ConcurrentHashMap;
@@ -33,6 +28,12 @@
3328
import org.slf4j.Logger;
3429
import org.slf4j.LoggerFactory;
3530

31+
import io.netty.channel.Channel;
32+
import io.netty.channel.ChannelId;
33+
import io.netty.util.Timeout;
34+
import io.netty.util.Timer;
35+
import io.netty.util.TimerTask;
36+
3637
/**
3738
* A simple implementation of {@link ChannelPool} based on a {@link java.util.concurrent.ConcurrentHashMap}
3839
*/
@@ -356,6 +357,16 @@ public void flushPartitions(ChannelPoolPartitionSelector selector) {
356357
}
357358
}
358359

360+
@Override
361+
public long getIdleChannelCount() {
362+
return partitions.reduceValuesToLong(
363+
Long.MAX_VALUE,
364+
ConcurrentLinkedDeque::size,
365+
0,
366+
(left, right) -> left + right
367+
);
368+
}
369+
359370
public enum PoolLeaseStrategy {
360371
LIFO {
361372
public <E> E lease(Deque<E> d) {

‎client/src/main/java/org/asynchttpclient/netty/request/NettyRequestSender.java‎

Lines changed: 16 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -14,32 +14,19 @@
1414
package org.asynchttpclient.netty.request;
1515

1616
import static org.asynchttpclient.util.Assertions.assertNotNull;
17-
import static org.asynchttpclient.util.AuthenticatorUtils.*;
18-
import static org.asynchttpclient.util.HttpConstants.Methods.*;
17+
import static org.asynchttpclient.util.AuthenticatorUtils.perConnectionAuthorizationHeader;
18+
import static org.asynchttpclient.util.AuthenticatorUtils.perConnectionProxyAuthorizationHeader;
19+
import static org.asynchttpclient.util.HttpConstants.Methods.CONNECT;
20+
import static org.asynchttpclient.util.HttpConstants.Methods.GET;
1921
import static org.asynchttpclient.util.MiscUtils.getCause;
2022
import static org.asynchttpclient.util.ProxyUtils.getProxyServer;
21-
import io.netty.bootstrap.Bootstrap;
22-
import io.netty.channel.Channel;
23-
import io.netty.channel.ChannelFuture;
24-
import io.netty.channel.ChannelProgressivePromise;
25-
import io.netty.channel.ChannelPromise;
26-
import io.netty.handler.codec.http.DefaultHttpHeaders;
27-
import io.netty.handler.codec.http.HttpHeaders;
28-
import io.netty.handler.codec.http.HttpMethod;
29-
import io.netty.handler.codec.http.HttpRequest;
30-
import io.netty.util.Timer;
3123

3224
import java.io.IOException;
3325
import java.net.InetSocketAddress;
3426
import java.util.List;
3527

36-
import org.asynchttpclient.AsyncHandler;
37-
import org.asynchttpclient.AsyncHttpClientConfig;
38-
import org.asynchttpclient.AsyncHttpClientState;
39-
import org.asynchttpclient.ListenableFuture;
40-
import org.asynchttpclient.Realm;
28+
import org.asynchttpclient.*;
4129
import org.asynchttpclient.Realm.AuthScheme;
42-
import org.asynchttpclient.Request;
4330
import org.asynchttpclient.exception.RemotelyClosedException;
4431
import org.asynchttpclient.filter.FilterContext;
4532
import org.asynchttpclient.filter.FilterException;
@@ -61,6 +48,17 @@
6148
import org.slf4j.Logger;
6249
import org.slf4j.LoggerFactory;
6350

51+
import io.netty.bootstrap.Bootstrap;
52+
import io.netty.channel.Channel;
53+
import io.netty.channel.ChannelFuture;
54+
import io.netty.channel.ChannelProgressivePromise;
55+
import io.netty.channel.ChannelPromise;
56+
import io.netty.handler.codec.http.DefaultHttpHeaders;
57+
import io.netty.handler.codec.http.HttpHeaders;
58+
import io.netty.handler.codec.http.HttpMethod;
59+
import io.netty.handler.codec.http.HttpRequest;
60+
import io.netty.util.Timer;
61+
6462
public final class NettyRequestSender {
6563

6664
private static final Logger LOGGER = LoggerFactory.getLogger(NettyRequestSender.class);
@@ -527,8 +525,4 @@ public void call() {
527525
}
528526
});
529527
}
530-
531-
public ChannelManager getChannelManager() {
532-
return channelManager;
533-
}
534528
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /