Search in sources :

Example 26 with ConnectionPool

use of okhttp3.ConnectionPool in project okhttp by square.

the class StreamAllocation method newStream.

public HttpCodec newStream(OkHttpClient client, boolean doExtensiveHealthChecks) {
    int connectTimeout = client.connectTimeoutMillis();
    int readTimeout = client.readTimeoutMillis();
    int writeTimeout = client.writeTimeoutMillis();
    boolean connectionRetryEnabled = client.retryOnConnectionFailure();
    try {
        RealConnection resultConnection = findHealthyConnection(connectTimeout, readTimeout, writeTimeout, connectionRetryEnabled, doExtensiveHealthChecks);
        HttpCodec resultCodec = resultConnection.newCodec(client, this);
        synchronized (connectionPool) {
            codec = resultCodec;
            return resultCodec;
        }
    } catch (IOException e) {
        throw new RouteException(e);
    }
}
Also used : HttpCodec(okhttp3.internal.http.HttpCodec) IOException(java.io.IOException)

Example 27 with ConnectionPool

use of okhttp3.ConnectionPool in project okhttp by square.

the class StreamAllocation method streamFailed.

public void streamFailed(IOException e) {
    Socket socket;
    boolean noNewStreams = false;
    synchronized (connectionPool) {
        if (e instanceof StreamResetException) {
            StreamResetException streamResetException = (StreamResetException) e;
            if (streamResetException.errorCode == ErrorCode.REFUSED_STREAM) {
                refusedStreamCount++;
            }
            // other errors must be retried on a new connection.
            if (streamResetException.errorCode != ErrorCode.REFUSED_STREAM || refusedStreamCount > 1) {
                noNewStreams = true;
                route = null;
            }
        } else if (connection != null && (!connection.isMultiplexed() || e instanceof ConnectionShutdownException)) {
            noNewStreams = true;
            // If this route hasn't completed a call, avoid it for new connections.
            if (connection.successCount == 0) {
                if (route != null && e != null) {
                    routeSelector.connectFailed(route, e);
                }
                route = null;
            }
        }
        socket = deallocate(noNewStreams, false, true);
    }
    closeQuietly(socket);
}
Also used : ConnectionShutdownException(okhttp3.internal.http2.ConnectionShutdownException) StreamResetException(okhttp3.internal.http2.StreamResetException) Socket(java.net.Socket)

Example 28 with ConnectionPool

use of okhttp3.ConnectionPool in project twitter4j by yusuke.

the class Http2ClientTest method testSpdy.

public void testSpdy() throws Exception {
    AlternativeHttpClientImpl.sPreferSpdy = true;
    AlternativeHttpClientImpl.sPreferHttp2 = false;
    AlternativeHttpClientImpl http = callOembed();
    // check SPDY
    Field f = http.getClass().getDeclaredField("okHttpClient");
    f.setAccessible(true);
    OkHttpClient client = (OkHttpClient) f.get(http);
    assertNotNull("ensure that OkHttpClient is used", client);
    ConnectionPool p = client.connectionPool();
    assertEquals(1, p.connectionCount());
    assertEquals(Protocol.SPDY_3, http.getLastRequestProtocol());
}
Also used : ConnectionPool(okhttp3.ConnectionPool) Field(java.lang.reflect.Field) OkHttpClient(okhttp3.OkHttpClient)

Example 29 with ConnectionPool

use of okhttp3.ConnectionPool in project atlasdb by palantir.

the class FeignOkHttpClients method newRawOkHttpClient.

@VisibleForTesting
static okhttp3.OkHttpClient newRawOkHttpClient(Optional<SSLSocketFactory> sslSocketFactory, Optional<ProxySelector> proxySelector, String userAgent) {
    // Don't allow retrying on connection failures - see ticket #2194
    okhttp3.OkHttpClient.Builder builder = new okhttp3.OkHttpClient.Builder().connectionSpecs(CONNECTION_SPEC_WITH_CYPHER_SUITES).connectionPool(new ConnectionPool(CONNECTION_POOL_SIZE, KEEP_ALIVE_TIME_MILLIS, TimeUnit.MILLISECONDS)).proxySelector(proxySelector.orElse(ProxySelector.getDefault())).retryOnConnectionFailure(false);
    if (sslSocketFactory.isPresent()) {
        builder.sslSocketFactory(sslSocketFactory.get());
    }
    builder.interceptors().add(new UserAgentAddingInterceptor(userAgent));
    globalClientSettings.accept(builder);
    return builder.build();
}
Also used : ConnectionPool(okhttp3.ConnectionPool) OkHttpClient(feign.okhttp.OkHttpClient) VisibleForTesting(com.google.common.annotations.VisibleForTesting)

Example 30 with ConnectionPool

use of okhttp3.ConnectionPool in project protools by SeanDragon.

the class ToolHttpBuilder method init.

private static void init() {
    // 连接池
    connectionPool = new ConnectionPool(DEFAULT_MAX_IDLE_CONNECTIONS, DEFAULT_KEEP_ALIVE_DURATION, TimeUnit.MINUTES);
    // 客户端构建对象
    defaultBuilder = new OkHttpClient.Builder();
    // 设置超时时间
    defaultBuilder.connectTimeout(DEFAULT_CONNECT_TIMEOUT, TimeUnit.SECONDS).writeTimeout(DEFAULT_WRITE_TIMEOUT, TimeUnit.SECONDS).readTimeout(DEFAULT_READ_TIMEOUT, TimeUnit.SECONDS).connectionPool(connectionPool).retryOnConnectionFailure(true).followRedirects(true).followSslRedirects(true);
    defaultClient = defaultBuilder.build();
// Runtime.getRuntime().addShutdownHook(new Thread(() -> connectionPool.evictAll()));
}
Also used : ConnectionPool(okhttp3.ConnectionPool) OkHttpClient(okhttp3.OkHttpClient)

Aggregations

ConnectionPool (okhttp3.ConnectionPool)12 Test (org.junit.Test)12 OkHttpClient (okhttp3.OkHttpClient)10 MockResponse (okhttp3.mockwebserver.MockResponse)8 IOException (java.io.IOException)5 RealConnection (okhttp3.internal.connection.RealConnection)5 Field (java.lang.reflect.Field)4 Dispatcher (okhttp3.Dispatcher)3 Socket (java.net.Socket)2 Level (java.util.logging.Level)2 SimpleFormatter (java.util.logging.SimpleFormatter)2 HttpCodec (okhttp3.internal.http.HttpCodec)2 RecordedRequest (okhttp3.mockwebserver.RecordedRequest)2 BuckConfig (com.facebook.buck.cli.BuckConfig)1 BuckEventBus (com.facebook.buck.event.BuckEventBus)1 DirCacheExperimentEvent (com.facebook.buck.event.DirCacheExperimentEvent)1 BytesReceivedEvent (com.facebook.buck.event.NetworkEvent.BytesReceivedEvent)1 ProjectFilesystem (com.facebook.buck.io.ProjectFilesystem)1 CommandThreadFactory (com.facebook.buck.log.CommandThreadFactory)1 Logger (com.facebook.buck.log.Logger)1