Skip to content

Commit d4170ac

Browse files
author
Stephane Landelle
committed
Major Netty provider refactoring
1 parent 8797d29 commit d4170ac

19 files changed

+1120
-1004
lines changed

src/main/java/com/ning/http/client/AsyncHttpClientConfig.java

Lines changed: 4 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,6 @@ public class AsyncHttpClientConfig {
6767
protected ProxyServerSelector proxyServerSelector;
6868
protected SSLContext sslContext;
6969
protected AsyncHttpProviderConfig<?, ?> providerConfig;
70-
protected ConnectionsPool<?, ?> connectionsPool;
7170
protected Realm realm;
7271
protected List<RequestFilter> requestFilters;
7372
protected List<ResponseFilter> responseFilters;
@@ -105,7 +104,7 @@ private AsyncHttpClientConfig(int maxTotalConnections,
105104
ProxyServerSelector proxyServerSelector,
106105
SSLContext sslContext,
107106
AsyncHttpProviderConfig<?, ?> providerConfig,
108-
ConnectionsPool<?, ?> connectionsPool, Realm realm,
107+
Realm realm,
109108
List<RequestFilter> requestFilters,
110109
List<ResponseFilter> responseFilters,
111110
List<IOExceptionFilter> ioExceptionFilters,
@@ -136,7 +135,6 @@ private AsyncHttpClientConfig(int maxTotalConnections,
136135
this.allowPoolingConnection = keepAlive;
137136
this.sslContext = sslContext;
138137
this.providerConfig = providerConfig;
139-
this.connectionsPool = connectionsPool;
140138
this.realm = realm;
141139
this.requestFilters = requestFilters;
142140
this.responseFilters = responseFilters;
@@ -244,7 +242,7 @@ public int getMaxRedirects() {
244242
}
245243

246244
/**
247-
* Is the {@link ConnectionsPool} support enabled.
245+
* Is the {@link ChannelPool} support enabled.
248246
*
249247
* @return true if keep-alive is enabled
250248
*/
@@ -299,15 +297,6 @@ public SSLContext getSSLContext() {
299297
return sslContext;
300298
}
301299

302-
/**
303-
* Return an instance of {@link ConnectionsPool}
304-
*
305-
* @return an instance of {@link ConnectionsPool}
306-
*/
307-
public ConnectionsPool<?, ?> getConnectionsPool() {
308-
return connectionsPool;
309-
}
310-
311300
/**
312301
* Return the {@link com.ning.http.client.AsyncHttpProviderConfig}
313302
*
@@ -514,7 +503,6 @@ public static class Builder {
514503
private ProxyServerSelector proxyServerSelector = null;
515504
private SSLContext sslContext;
516505
private AsyncHttpProviderConfig<?, ?> providerConfig;
517-
private ConnectionsPool<?, ?> connectionsPool;
518506
private Realm realm;
519507
private final List<RequestFilter> requestFilters = new LinkedList<RequestFilter>();
520508
private final List<ResponseFilter> responseFilters = new LinkedList<ResponseFilter>();
@@ -651,9 +639,9 @@ public Builder setUserAgent(String userAgent) {
651639
}
652640

653641
/**
654-
* Set true if connection can be pooled by a {@link ConnectionsPool}. Default is true.
642+
* Set true if connection can be pooled by a {@link ChannelPool}. Default is true.
655643
*
656-
* @param allowPoolingConnection true if connection can be pooled by a {@link ConnectionsPool}
644+
* @param allowPoolingConnection true if connection can be pooled by a {@link ChannelPool}
657645
* @return a {@link Builder}
658646
*/
659647
public Builder setAllowPoolingConnection(boolean allowPoolingConnection) {
@@ -718,17 +706,6 @@ public Builder setAsyncHttpClientProviderConfig(AsyncHttpProviderConfig<?, ?> pr
718706
return this;
719707
}
720708

721-
/**
722-
* Set the {@link ConnectionsPool}
723-
*
724-
* @param connectionsPool the {@link ConnectionsPool}
725-
* @return a {@link Builder}
726-
*/
727-
public Builder setConnectionsPool(ConnectionsPool<?, ?> connectionsPool) {
728-
this.connectionsPool = connectionsPool;
729-
return this;
730-
}
731-
732709
/**
733710
* Set the {@link Realm} that will be used for all requests.
734711
*
@@ -976,7 +953,6 @@ public Builder setAcceptAnyCertificate(boolean acceptAnyCertificate) {
976953
public Builder(AsyncHttpClientConfig prototype) {
977954
allowPoolingConnection = prototype.isAllowPoolingConnection();
978955
providerConfig = prototype.getAsyncHttpProviderConfig();
979-
connectionsPool = prototype.getConnectionsPool();
980956
connectionTimeOutInMs = prototype.getConnectionTimeoutInMs();
981957
idleConnectionInPoolTimeoutInMs = prototype.getIdleConnectionInPoolTimeoutInMs();
982958
idleConnectionTimeoutInMs = prototype.getIdleConnectionTimeoutInMs();
@@ -1060,7 +1036,6 @@ public Thread newThread(Runnable r) {
10601036
proxyServerSelector, //
10611037
sslContext, //
10621038
providerConfig, //
1063-
connectionsPool, //
10641039
realm, //
10651040
requestFilters, //
10661041
responseFilters, //

src/main/java/com/ning/http/client/AsyncHttpClientConfigBean.java

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -179,11 +179,6 @@ public AsyncHttpClientConfigBean setProviderConfig(AsyncHttpProviderConfig<?, ?>
179179
return this;
180180
}
181181

182-
public AsyncHttpClientConfigBean setConnectionsPool(ConnectionsPool<?, ?> connectionsPool) {
183-
this.connectionsPool = connectionsPool;
184-
return this;
185-
}
186-
187182
public AsyncHttpClientConfigBean setRealm(Realm realm) {
188183
this.realm = realm;
189184
return this;

src/main/java/com/ning/http/client/AsyncHttpProviderConfig.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ public interface AsyncHttpProviderConfig<U, V> {
2929
* @param value the value of the property
3030
* @return this instance of AsyncHttpProviderConfig
3131
*/
32-
AsyncHttpProviderConfig addProperty(U name, V value);
32+
AsyncHttpProviderConfig<U, V> addProperty(U name, V value);
3333

3434
/**
3535
* Return the value associated with the property's name

src/main/java/com/ning/http/client/ConnectionsPool.java renamed to src/main/java/com/ning/http/client/providers/grizzly/ConnectionPool.java

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,14 @@
1414
* under the License.
1515
*
1616
*/
17-
package com.ning.http.client;
17+
package com.ning.http.client.providers.grizzly;
18+
19+
import org.glassfish.grizzly.Connection;
1820

1921
/**
2022
* An interface used by an {@link AsyncHttpProvider} for caching http connections.
2123
*/
22-
public interface ConnectionsPool<U, V> {
24+
public interface ConnectionPool {
2325

2426
/**
2527
* Add a connection tpo the pool
@@ -28,27 +30,27 @@ public interface ConnectionsPool<U, V> {
2830
* @param connection an I/O connection
2931
* @return true if added.
3032
*/
31-
boolean offer(U uri, V connection);
33+
boolean offer(String uri, Connection connection);
3234

3335
/**
3436
* Remove the connection associated with the uri.
3537
*
3638
* @param uri the uri used when invoking addConnection
3739
* @return the connection associated with the uri
3840
*/
39-
V poll(U uri);
41+
Connection poll(String uri);
4042

4143
/**
4244
* Remove all connections from the cache. A connection might have been associated with several uri.
4345
*
4446
* @param connection a connection
4547
* @return the true if the connection has been removed
4648
*/
47-
boolean removeAll(V connection);
49+
boolean removeAll(Connection connection);
4850

4951
/**
5052
* Return true if a connection can be cached. A implementation can decide based on some rules to allow caching
51-
* Calling this method is equivalent of checking the returned value of {@link ConnectionsPool#offer(Object, Object)}
53+
* Calling this method is equivalent of checking the returned value of {@link ConnectionPool#offer(Object, Object)}
5254
*
5355
* @return true if a connection can be cached.
5456
*/

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

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,6 @@
8888
import org.glassfish.grizzly.ssl.SSLFilter;
8989
import org.glassfish.grizzly.strategies.SameThreadIOStrategy;
9090
import org.glassfish.grizzly.strategies.WorkerThreadIOStrategy;
91-
import org.glassfish.grizzly.utils.BufferOutputStream;
9291
import org.glassfish.grizzly.utils.Charsets;
9392
import org.glassfish.grizzly.utils.DelayedExecutor;
9493
import org.glassfish.grizzly.utils.Futures;
@@ -112,7 +111,6 @@
112111
import com.ning.http.client.AsyncHttpProviderConfig;
113112
import com.ning.http.client.Body;
114113
import com.ning.http.client.BodyGenerator;
115-
import com.ning.http.client.ConnectionsPool;
116114
import com.ning.http.client.FluentCaseInsensitiveStringsMap;
117115
import com.ning.http.client.HttpResponseBodyPart;
118116
import com.ning.http.client.HttpResponseHeaders;
@@ -180,6 +178,7 @@ public class GrizzlyAsyncHttpProvider implements AsyncHttpProvider {
180178

181179
private final TCPNIOTransport clientTransport;
182180
private final AsyncHttpClientConfig clientConfig;
181+
private final GrizzlyAsyncHttpProviderConfig providerConfig;
183182
private final ConnectionManager connectionManager;
184183

185184
DelayedExecutor.Resolver<Connection> resolver;
@@ -194,10 +193,14 @@ public class GrizzlyAsyncHttpProvider implements AsyncHttpProvider {
194193
public GrizzlyAsyncHttpProvider(final AsyncHttpClientConfig clientConfig) {
195194

196195
this.clientConfig = clientConfig;
196+
this.providerConfig =
197+
clientConfig.getAsyncHttpProviderConfig() instanceof GrizzlyAsyncHttpProviderConfig ?
198+
(GrizzlyAsyncHttpProviderConfig) clientConfig.getAsyncHttpProviderConfig()
199+
: new GrizzlyAsyncHttpProviderConfig();
197200
final TCPNIOTransportBuilder builder = TCPNIOTransportBuilder.newInstance();
198201
clientTransport = builder.build();
199202
initializeTransport(clientConfig);
200-
connectionManager = new ConnectionManager(this, clientTransport);
203+
connectionManager = new ConnectionManager(this, clientTransport, providerConfig);
201204
try {
202205
clientTransport.start();
203206
} catch (IOException ioe) {
@@ -395,10 +398,6 @@ public void onTimeout(Connection connection) {
395398
false);
396399
final SwitchingSSLFilter filter = new SwitchingSSLFilter(configurator, defaultSecState);
397400
fcb.add(filter);
398-
final GrizzlyAsyncHttpProviderConfig providerConfig =
399-
clientConfig.getAsyncHttpProviderConfig() instanceof GrizzlyAsyncHttpProviderConfig ?
400-
(GrizzlyAsyncHttpProviderConfig) clientConfig.getAsyncHttpProviderConfig()
401-
: new GrizzlyAsyncHttpProviderConfig();
402401
final AsyncHttpClientEventFilter eventFilter = new
403402
AsyncHttpClientEventFilter(this, (Integer) providerConfig.getProperty(MAX_HTTP_PACKET_HEADER_SIZE));
404403
final AsyncHttpClientFilter clientFilter =
@@ -1789,7 +1788,7 @@ public boolean applyDecoding(HttpHeader httpPacket) {
17891788
} // END ClientContentEncoding
17901789

17911790

1792-
private static final class NonCachingPool implements ConnectionsPool<String,Connection> {
1791+
private static final class NonCachingPool implements ConnectionPool {
17931792

17941793

17951794
// ---------------------------------------- Methods from ConnectionsPool
@@ -2316,26 +2315,26 @@ static class ConnectionManager {
23162315

23172316
private static final Attribute<Boolean> DO_NOT_CACHE =
23182317
Grizzly.DEFAULT_ATTRIBUTE_BUILDER.createAttribute(ConnectionManager.class.getName());
2319-
private final ConnectionsPool<String,Connection> pool;
2318+
private final ConnectionPool pool;
23202319
private final TCPNIOConnectorHandler connectionHandler;
23212320
private final ConnectionMonitor connectionMonitor;
23222321
private final GrizzlyAsyncHttpProvider provider;
23232322

23242323
// -------------------------------------------------------- Constructors
23252324

23262325
ConnectionManager(final GrizzlyAsyncHttpProvider provider,
2327-
final TCPNIOTransport transport) {
2326+
final TCPNIOTransport transport,
2327+
final GrizzlyAsyncHttpProviderConfig providerConfig) {
23282328

2329-
ConnectionsPool<String,Connection> connectionPool;
2329+
ConnectionPool connectionPool;
23302330
this.provider = provider;
23312331
final AsyncHttpClientConfig config = provider.clientConfig;
23322332
if (config.isAllowPoolingConnection()) {
2333-
ConnectionsPool pool = config.getConnectionsPool();
2333+
ConnectionPool pool = providerConfig != null ? providerConfig.getConnectionPool() : null;
23342334
if (pool != null) {
2335-
//noinspection unchecked
2336-
connectionPool = (ConnectionsPool<String, Connection>) pool;
2335+
connectionPool = pool;
23372336
} else {
2338-
connectionPool = new GrizzlyConnectionsPool((config));
2337+
connectionPool = new GrizzlyConnectionPool((config));
23392338
}
23402339
} else {
23412340
connectionPool = new NonCachingPool();

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

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
package com.ning.http.client.providers.grizzly;
1515

1616
import com.ning.http.client.AsyncHttpProviderConfig;
17+
1718
import org.glassfish.grizzly.http.HttpCodecFilter;
1819
import org.glassfish.grizzly.nio.transport.TCPNIOTransport;
1920

@@ -90,7 +91,9 @@ boolean hasDefaultValue() {
9091
} // END PROPERTY
9192

9293
private final Map<Property,Object> attributes = new HashMap<Property,Object>();
93-
94+
95+
protected ConnectionPool connectionPool;
96+
9497
// ------------------------------------ Methods from AsyncHttpProviderConfig
9598

9699
/**
@@ -157,4 +160,11 @@ public Set<Map.Entry<Property,Object>> propertiesSet() {
157160
return attributes.entrySet();
158161
}
159162

163+
public ConnectionPool getConnectionPool() {
164+
return connectionPool;
165+
}
166+
167+
public void setConnectionPool(ConnectionPool connectionPool) {
168+
this.connectionPool = connectionPool;
169+
}
160170
}

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

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
import static com.ning.http.util.DateUtils.millisTime;
1717

1818
import com.ning.http.client.AsyncHttpClientConfig;
19-
import com.ning.http.client.ConnectionsPool;
2019

2120
import org.glassfish.grizzly.CloseListener;
2221
import org.glassfish.grizzly.CloseType;
@@ -41,14 +40,14 @@
4140
import java.util.concurrent.atomic.AtomicInteger;
4241

4342
/**
44-
* {@link ConnectionsPool} implementation.
43+
* {@link ConnectionPool} implementation.
4544
*
4645
* @author The Grizzly Team
4746
* @since 1.7.0
4847
*/
49-
public class GrizzlyConnectionsPool implements ConnectionsPool<String,Connection> {
48+
public class GrizzlyConnectionPool implements ConnectionPool {
5049

51-
private final static Logger LOG = LoggerFactory.getLogger(GrizzlyConnectionsPool.class);
50+
private final static Logger LOG = LoggerFactory.getLogger(GrizzlyConnectionPool.class);
5251

5352
private final ConcurrentHashMap<String,DelayedExecutor.IdleConnectionQueue> connectionsPool =
5453
new ConcurrentHashMap<String,DelayedExecutor.IdleConnectionQueue>();
@@ -74,7 +73,7 @@ public void onClosed(Connection connection, CloseType closeType)
7473
connection.toString());
7574
}
7675
}
77-
GrizzlyConnectionsPool.this.removeAll(connection);
76+
GrizzlyConnectionPool.this.removeAll(connection);
7877
}
7978
};
8079

@@ -83,7 +82,7 @@ public void onClosed(Connection connection, CloseType closeType)
8382

8483

8584
@SuppressWarnings("UnusedDeclaration")
86-
public GrizzlyConnectionsPool(final boolean cacheSSLConnections,
85+
public GrizzlyConnectionPool(final boolean cacheSSLConnections,
8786
final int timeout,
8887
final int maxConnectionLifeTimeInMs,
8988
final int maxConnectionsPerHost,
@@ -109,8 +108,7 @@ public GrizzlyConnectionsPool(final boolean cacheSSLConnections,
109108
}
110109
}
111110

112-
113-
public GrizzlyConnectionsPool(final AsyncHttpClientConfig config) {
111+
public GrizzlyConnectionPool(final AsyncHttpClientConfig config) {
114112

115113
cacheSSLConnections = config.isSslConnectionPoolEnabled();
116114
timeout = config.getIdleConnectionInPoolTimeoutInMs();
@@ -299,15 +297,15 @@ public static final class DelayedExecutor {
299297

300298

301299
public DelayedExecutor(final ExecutorService threadPool,
302-
final GrizzlyConnectionsPool connectionsPool) {
300+
final GrizzlyConnectionPool connectionsPool) {
303301
this(threadPool, 1000, TimeUnit.MILLISECONDS, connectionsPool);
304302
}
305303

306304

307305
public DelayedExecutor(final ExecutorService threadPool,
308306
final long checkInterval,
309307
final TimeUnit timeunit,
310-
final GrizzlyConnectionsPool connectionsPool) {
308+
final GrizzlyConnectionPool connectionsPool) {
311309
this.threadPool = threadPool;
312310
this.checkIntervalMs = TimeUnit.MILLISECONDS.convert(checkInterval, timeunit);
313311
totalCachedConnections = connectionsPool.totalCachedConnections;

0 commit comments

Comments
 (0)