allowPoolingConnections = prototype.isAllowPoolingConnections(); providerConfig = prototype.getAsyncHttpProviderConfig(); connectTimeout = prototype.getConnectTimeout(); pooledConnectionIdleTimeout = prototype.getPooledConnectionIdleTimeout(); readTimeout = prototype.getReadTimeout(); maxConnectionsPerHost = prototype.getMaxConnectionsPerHost(); connectionTTL = prototype.getConnectionTTL(); maxRedirects = prototype.getMaxRedirects(); maxConnections = prototype.getMaxConnections(); proxyServerSelector = prototype.getProxyServerSelector(); realm = prototype.getRealm(); requestTimeout = prototype.getRequestTimeout(); sslContext = prototype.getSSLContext(); userAgent = prototype.getUserAgent(); followRedirect = prototype.isFollowRedirect(); compressionEnforced = prototype.isCompressionEnforced(); applicationThreadPool = prototype.executorService(); requestFilters.addAll(prototype.getRequestFilters()); responseFilters.addAll(prototype.getResponseFilters()); ioExceptionFilters.addAll(prototype.getIOExceptionFilters()); disableUrlEncodingForBoundedRequests = prototype.isDisableUrlEncodingForBoundedRequests(); ioThreadMultiplier = prototype.getIoThreadMultiplier(); maxRequestRetry = prototype.getMaxRequestRetry(); allowPoolingSslConnections = prototype.isAllowPoolingConnections(); hostnameVerifier = prototype.getHostnameVerifier(); strict302Handling = prototype.isStrict302Handling(); enabledProtocols = prototype.enabledProtocols;
public DefaultChannelPool(AsyncHttpClientConfig config, Timer hashedWheelTimer) { this(config.getPooledConnectionIdleTimeout(),// config.getConnectionTTL(),// config.isAllowPoolingSslConnections(),// hashedWheelTimer); }
if (config.getMaxConnections() > -1 && (maxConnections.get() + 1) > config.getMaxConnections()) { throw new IOException(String.format("Too many connections %s", config.getMaxConnections())); Realm realm = request.getRealm() != null ? request.getRealm() : config.getRealm(); Proxy proxy = null; if (proxyServer != null || realm != null) { f.touch(); f.setInnerFuture(config.executorService().submit(new AsyncHttpUrlConnection(urlConnection, request, handler, f))); maxConnections.incrementAndGet();
log.debug("connect timeout: {}", config.getConnectTimeout()); log.debug("read timeout: {}", config.getReadTimeout()); log.debug("request timeout: {}", config.getRequestTimeout()); log.debug("max connections: {}", config.getMaxConnections()); log.debug("max connections per host: {}", config.getMaxConnectionsPerHost()); log.debug("enabled cipher suites: {}", Arrays.toString(config.getEnabledCipherSuites())); log.debug("response compression enforced: {}", config.isCompressionEnforced()); log.debug("request compression level: {}", builder.compressionLevel); log.debug("accept any certificate: {}", config.isAcceptAnyCertificate()); log.debug("follows redirect: {}", config.isFollowRedirect()); log.debug("pooled connection TTL: {}", config.getConnectionTTL()); log.debug("pooled connection idle timeout: {}", config.getPooledConnectionIdleTimeout()); log.debug("pooling connections: {}", config.isAllowPoolingConnections()); log.debug("pooling SSL connections: {}", config.isAllowPoolingSslConnections()); log.debug("user agent: {}", config.getUserAgent()); log.debug("max request retry: {}", config.getMaxRequestRetry()); new ConfigurableSSLSocketFactory(config.getEnabledCipherSuites(), (SSLSocketFactory) SSLSocketFactory.getDefault());
this.poolingEnabled = config.isAllowPoolingConnections(); this.poolingSSLConnections = config.isAllowPoolingSslConnections(); final MultiEndpointPool.Builder<SocketAddress> builder = MultiEndpointPool.builder(SocketAddress.class) .connectTimeout(config.getConnectTimeout(), TimeUnit.MILLISECONDS) .asyncPollTimeout(config.getConnectTimeout(), TimeUnit.MILLISECONDS) .maxConnectionsTotal(config.getMaxConnections()) .maxConnectionsPerEndpoint(config.getMaxConnectionsPerHost()) .keepAliveTimeout(config.getPooledConnectionIdleTimeout(), TimeUnit.MILLISECONDS) .keepAliveCheckInterval(1, TimeUnit.SECONDS) .connectorHandler(defaultConnectionHandler) .connectionTTL(config.getConnectionTTL(), TimeUnit.MILLISECONDS) .failFastWhenMaxSizeReached(true); } else { pool = MultiEndpointPool.builder(SocketAddress.class) .connectTimeout(config.getConnectTimeout(), TimeUnit.MILLISECONDS) .asyncPollTimeout(config.getConnectTimeout(), TimeUnit.MILLISECONDS) .maxConnectionsTotal(config.getMaxConnections()) .maxConnectionsPerEndpoint(config.getMaxConnectionsPerHost()) .keepAliveTimeout(0, TimeUnit.MILLISECONDS) // no pool .connectorHandler(defaultConnectionHandler)
if (channelPool == null && config.isAllowPoolingConnections()) { channelPool = new DefaultChannelPool(config, nettyTimer); } else if (channelPool == null) { tooManyConnections = buildStaticIOException(String.format("Too many connections %s", config.getMaxConnections())); tooManyConnectionsPerHost = buildStaticIOException(String.format("Too many connections per host %s", config.getMaxConnectionsPerHost())); poolAlreadyClosed = buildStaticIOException("Pool is already closed"); maxTotalConnectionsEnabled = config.getMaxConnections() > 0; maxConnectionsPerHostEnabled = config.getMaxConnectionsPerHost() > 0; freeChannels = new Semaphore(config.getMaxConnections()); } else { openChannels = new CleanupChannelGroup("asyncHttpClient"); if (e == null) e = Executors.newCachedThreadPool(); int numWorkers = config.getIoThreadMultiplier() * Runtime.getRuntime().availableProcessors(); LOGGER.trace("Number of application's worker threads is {}", numWorkers); socketChannelFactory = new NioClientSocketChannelFactory(e, config.executorService(), numWorkers); allowReleaseSocketChannelFactory = true; if (config.getConnectTimeout() > 0) nettyConfig.addProperty("connectTimeoutMillis", config.getConnectTimeout()); for (Entry<String, Object> entry : nettyConfig.propertiesSet()) { String key = entry.getKey();
public GrizzlyConnectionPool(final AsyncHttpClientConfig config) { cacheSSLConnections = config.isAllowPoolingSslConnections(); timeout = config.getPooledConnectionIdleTimeout(); maxConnectionLifeTime = config.getConnectionTTL(); maxConnectionsPerHost = config.getMaxConnectionsPerHost(); maxConnections = config.getMaxConnections(); unlimitedConnections = (maxConnections == -1); delayedExecutor = new DelayedExecutor(Executors.newSingleThreadExecutor(), this); delayedExecutor.start(); ownsDelayedExecutor = true; }
boolean connect = method == HttpMethod.CONNECT; boolean allowConnectionPooling = config.isAllowPoolingConnections() && (!isSecure(uri) || config.isAllowPoolingSslConnections()); headers.set(HttpHeaders.Names.COOKIE, CookieEncoder.encode(request.getCookies())); if (config.isCompressionEnforced() && !headers.contains(HttpHeaders.Names.ACCEPT_ENCODING)) headers.set(HttpHeaders.Names.ACCEPT_ENCODING, GZIP_DEFLATE); headers.set(HttpHeaders.Names.HOST, hostHeader(request, uri)); Realm realm = request.getRealm() != null ? request.getRealm() : config.getRealm(); if (!headers.contains(HttpHeaders.Names.USER_AGENT) && config.getUserAgent() != null) headers.set(HttpHeaders.Names.USER_AGENT, config.getUserAgent());
if (channelPool == null && config.isAllowPoolingConnections()) { channelPool = new DefaultChannelPool(config, nettyTimer); } else if (channelPool == null) { maxTotalConnectionsEnabled = config.getMaxConnections() > 0; maxConnectionsPerHostEnabled = config.getMaxConnectionsPerHost() > 0; freeChannels = new Semaphore(config.getMaxConnections()); } else { openChannels = new CleanupChannelGroup("asyncHttpClient"); if (e == null) e = Executors.newCachedThreadPool(); int numWorkers = config.getIoThreadMultiplier() * Runtime.getRuntime().availableProcessors(); LOGGER.trace("Number of application's worker threads is {}", numWorkers); socketChannelFactory = new NioClientSocketChannelFactory(e, config.executorService(), numWorkers); allowReleaseSocketChannelFactory = true; if (config.getConnectionTimeout() > 0) nettyConfig.addProperty("connectTimeoutMillis", config.getConnectionTimeout()); for (Entry<String, Object> entry : nettyConfig.propertiesSet()) { String key = entry.getKey();
for (ResponseFilter asyncFilter : config.getResponseFilters()) { fc = asyncFilter.filter(fc); if (fc == null) { if (currentRedirectCount++ < config.getMaxRedirects()) { String location = urlConnection.getHeaderField("Location"); Uri redirUri = Uri.create(uri, location); throw new MaxRedirectException("Maximum redirect reached: " + config.getMaxRedirects()); Realm realm = request.getRealm() != null ? request.getRealm() : config.getRealm(); if (statusCode == 401 && !isAuth.getAndSet(true) && realm != null) { String wwwAuth = urlConnection.getHeaderField("WWW-Authenticate"); if (t instanceof IOException && !config.getIOExceptionFilters().isEmpty()) { FilterContext fc = new FilterContext.FilterContextBuilder().asyncHandler(asyncHandler) .request(request).ioException(IOException.class.cast(t)).build(); fc = handleIoException(fc); } catch (FilterException e) { if (config.getMaxConnections() != -1) { maxConnections.decrementAndGet(); } finally { if (terminate) { if (config.getMaxConnections() != -1) { maxConnections.decrementAndGet();
private void verifyDefaultUnderlyingClientConfig(AsyncHttpClientHelper instance) { AsyncHttpClientConfig config = instance.asyncHttpClient.getConfig(); assertThat(config.isAllowPoolingConnections()).isTrue(); assertThat(config.getMaxRequestRetry()).isEqualTo(0); assertThat(config.getRequestTimeout()).isEqualTo(DEFAULT_REQUEST_TIMEOUT_MILLIS); assertThat(config.getConnectionTTL()).isEqualTo(DEFAULT_POOLED_DOWNSTREAM_CONNECTION_TTL_MILLIS); assertThat(Whitebox.getInternalState(instance.asyncHttpClient, "signatureCalculator")).isNull(); }
/** * Submits the response processing on Grizzly client's application thread pool. * * @param responseTask task to be processed on application thread pool. */ private void processResponse(Runnable responseTask) { this.grizzlyClient.getConfig().executorService().submit(responseTask); }
urlConnection.setConnectTimeout(config.getConnectTimeout()); if (config.isCompressionEnforced()) { urlConnection.setRequestProperty("Accept-Encoding", "gzip"); Realm realm = request.getRealm() != null ? request.getRealm() : config.getRealm(); if (realm != null && realm.getUsePreemptiveAuth()) { switch (realm.getScheme()) { } else if (config.getUserAgent() != null) { urlConnection.setRequestProperty("User-Agent", config.getUserAgent());
fcb.add(new TransportFilter()); final int timeout = clientConfig.getRequestTimeout(); if (timeout > 0) { int delay = 500; SSLContext context = clientConfig.getSSLContext(); boolean defaultSecState = (context != null); if (context == null) { try { context = SslUtils.getInstance().getSSLContext(clientConfig.isAcceptAnyCertificate()); } catch (Exception e) { throw new IllegalStateException(e); final SwitchingSSLFilter sslFilter = new SwitchingSSLFilter(configurator, defaultSecState); if (clientConfig.getHostnameVerifier() != null) { sslFilter.addHandshakeListener(new HostnameVerifierListener()); if (clientConfig.isCompressionEnabled()) { eventFilter.addContentEncoding( new GZipContentEncoding(512, clientConfig.getIoThreadMultiplier() * Runtime.getRuntime().availableProcessors();
fcb.add(new TransportFilter()); final int timeout = clientConfig.getRequestTimeout(); if (timeout > 0) { int delay = 500; final boolean defaultSecState = (clientConfig.getSSLContext() != null); final SSLEngineConfigurator configurator = new AhcSSLEngineConfigurator( clientConfig.getIoThreadMultiplier() * Runtime.getRuntime().availableProcessors();
currentRedirectCount = config.getMaxRedirects(); for (ResponseFilter asyncFilter : config.getResponseFilters()) { fc = asyncFilter.filter(fc); if (fc == null) { if (currentRedirectCount++ < config.getMaxRedirects()) { String location = method.getResponseHeader("Location").getValue(); UriComponents rediUri = UriComponents.create(uri, location); throw new MaxRedirectException("Maximum redirect reached: " + config.getMaxRedirects()); if (t instanceof IOException && !config.getIOExceptionFilters().isEmpty()) { FilterContext fc = new FilterContext.FilterContextBuilder().asyncHandler(asyncHandler) .request(future.getRequest()).ioException(IOException.class.cast(t)).build(); fc = handleIoException(fc); } catch (FilterException e) { if (config.getMaxConnections() != -1) { maxConnections.decrementAndGet(); if (config.getMaxConnections() != -1) { maxConnections.decrementAndGet(); config.executorService().submit(new Runnable() {
private void addServiceHeaders(final HttpRequestPacket requestPacket) { final MimeHeaders headers = requestPacket.getHeaders(); if (!headers.contains(Header.UserAgent)) { headers.addValue(Header.UserAgent).setString(config.getUserAgent()); } setKeepAliveForHeader(Header.Connection, requestPacket); }
if (config.getMaxConnections() > -1 && (maxConnections.get() + 1) > config.getMaxConnections()) { throw new IOException(String.format("Too many connections %s", config.getMaxConnections())); if (config.getReadTimeout() > 0 && requestTimeout != -1 && requestTimeout < config.getReadTimeout()) { idleConnectionTimeoutThread = new IdleConnectionTimeoutThread(); idleConnectionTimeoutThread.setConnectionTimeout(config.getReadTimeout()); idleConnectionTimeoutThread.addConnectionManager(connectionManager); idleConnectionTimeoutThread.start(); Realm realm = request.getRealm() != null ? request.getRealm() : config.getRealm(); if (realm != null) { httpClient.getParams().setAuthenticationPreemptive(realm.getUsePreemptiveAuth()); f.touch(); f.setInnerFuture(config.executorService().submit(new ApacheClientRunnable(request, handler, method, f, httpClient))); maxConnections.incrementAndGet(); return f;
public static int requestTimeout(AsyncHttpClientConfig config, Request request) { return request.getRequestTimeout() != 0 ? request.getRequestTimeout() : config.getRequestTimeout(); }
urlConnection.setConnectTimeout(config.getConnectionTimeout()); if (config.isCompressionEnabled()) { urlConnection.setRequestProperty("Accept-Encoding", "gzip"); Realm realm = request.getRealm() != null ? request.getRealm() : config.getRealm(); if (realm != null && realm.getUsePreemptiveAuth()) { switch (realm.getAuthScheme()) { } else if (config.getUserAgent() != null) { urlConnection.setRequestProperty("User-Agent", config.getUserAgent());