Search in sources :

Example 11 with ConnectionPool

use of okhttp3.ConnectionPool in project okhttp by square.

the class CallTest method leakedResponseBodyLogsStackTrace.

@Test
public void leakedResponseBodyLogsStackTrace() throws Exception {
    server.enqueue(new MockResponse().setBody("This gets leaked."));
    client = defaultClient().newBuilder().connectionPool(new ConnectionPool(0, 10, TimeUnit.MILLISECONDS)).build();
    Request request = new Request.Builder().url(server.url("/")).build();
    Level original = logger.getLevel();
    logger.setLevel(Level.FINE);
    logHandler.setFormatter(new SimpleFormatter());
    try {
        // Ignore the response so it gets leaked then GC'd.
        client.newCall(request).execute();
        awaitGarbageCollection();
        String message = logHandler.take();
        assertTrue(message.contains("A connection to " + server.url("/") + " was leaked." + " Did you forget to close a response body?"));
        assertTrue(message.contains("okhttp3.RealCall.execute("));
        assertTrue(message.contains("okhttp3.CallTest.leakedResponseBodyLogsStackTrace("));
    } finally {
        logger.setLevel(original);
    }
}
Also used : MockResponse(okhttp3.mockwebserver.MockResponse) SimpleFormatter(java.util.logging.SimpleFormatter) RecordedRequest(okhttp3.mockwebserver.RecordedRequest) Level(java.util.logging.Level) Test(org.junit.Test)

Example 12 with ConnectionPool

use of okhttp3.ConnectionPool in project hmftools by hartwigmedical.

the class SlicerHttpClient method create.

@NotNull
static OkHttpClient create(final int maxRequests) {
    final Dispatcher requestDispatcher = new Dispatcher();
    requestDispatcher.setMaxRequests(maxRequests);
    requestDispatcher.setMaxRequestsPerHost(maxRequests);
    return new OkHttpClient.Builder().connectionPool(new ConnectionPool(20, 1, TimeUnit.MINUTES)).readTimeout(20, TimeUnit.SECONDS).connectTimeout(20, TimeUnit.SECONDS).writeTimeout(20, TimeUnit.SECONDS).dispatcher(requestDispatcher).build();
}
Also used : ConnectionPool(okhttp3.ConnectionPool) Dispatcher(okhttp3.Dispatcher) NotNull(org.jetbrains.annotations.NotNull)

Example 13 with ConnectionPool

use of okhttp3.ConnectionPool in project apollo by spotify.

the class OkHttpClientProvider method get.

@Override
public OkHttpClient get() {
    final OkHttpClient.Builder clientBuilder = new OkHttpClient.Builder();
    // timeouts settings
    config.connectTimeoutMillis().ifPresent(millis -> clientBuilder.connectTimeout(millis, TimeUnit.MILLISECONDS));
    config.readTimeoutMillis().ifPresent(millis -> clientBuilder.readTimeout(millis, TimeUnit.MILLISECONDS));
    config.writeTimeoutMillis().ifPresent(millis -> clientBuilder.writeTimeout(millis, TimeUnit.MILLISECONDS));
    // connection pool settings
    clientBuilder.connectionPool(new ConnectionPool(// defaults that come from com.squareup.okhttp.ConnectionPool
    config.maxIdleConnections().orElse(5), config.connectionKeepAliveDurationMillis().orElse(5 * 60 * 1000), TimeUnit.MILLISECONDS));
    config.followRedirects().ifPresent(clientBuilder::followRedirects);
    final OkHttpClient client = clientBuilder.build();
    // async dispatcher settings
    config.maxAsyncRequests().ifPresent(max -> client.dispatcher().setMaxRequests(max));
    config.maxAsyncRequestsPerHost().ifPresent(max -> client.dispatcher().setMaxRequestsPerHost(max));
    closer.register(ExecutorServiceCloser.of(client.dispatcher().executorService()));
    return client;
}
Also used : ConnectionPool(okhttp3.ConnectionPool) OkHttpClient(okhttp3.OkHttpClient)

Example 14 with ConnectionPool

use of okhttp3.ConnectionPool in project jianshi by wingjay.

the class AppModule method provideOkHttpClient.

@Provides
@Singleton
OkHttpClient provideOkHttpClient(GlobalRequestInterceptor globalRequestInterceptor) {
    OkHttpClient.Builder builder = new OkHttpClient.Builder().connectionPool(new ConnectionPool(5, 59, TimeUnit.SECONDS)).connectTimeout(20, TimeUnit.SECONDS).readTimeout(20, TimeUnit.SECONDS).addInterceptor(globalRequestInterceptor).retryOnConnectionFailure(false);
    if (BuildConfig.DEBUG) {
        builder.addNetworkInterceptor(new StethoInterceptor());
    }
    HttpLoggingInterceptor httpLoggingInterceptor = new HttpLoggingInterceptor();
    httpLoggingInterceptor.setLevel(BuildConfig.DEBUG ? HttpLoggingInterceptor.Level.BODY : HttpLoggingInterceptor.Level.NONE);
    builder.addInterceptor(httpLoggingInterceptor);
    return builder.build();
}
Also used : ConnectionPool(okhttp3.ConnectionPool) OkHttpClient(okhttp3.OkHttpClient) StethoInterceptor(com.facebook.stetho.okhttp3.StethoInterceptor) HttpLoggingInterceptor(okhttp3.logging.HttpLoggingInterceptor) Singleton(javax.inject.Singleton) Provides(dagger.Provides)

Example 15 with ConnectionPool

use of okhttp3.ConnectionPool in project Signal-Android by WhisperSystems.

the class PushServiceSocket method createConnectionClient.

private static OkHttpClient createConnectionClient(SignalUrl url, List<Interceptor> interceptors, Optional<Dns> dns, Optional<SignalProxy> proxy) {
    try {
        TrustManager[] trustManagers = BlacklistingTrustManager.createFor(url.getTrustStore());
        SSLContext context = SSLContext.getInstance("TLS");
        context.init(null, trustManagers, null);
        OkHttpClient.Builder builder = new OkHttpClient.Builder().sslSocketFactory(new Tls12SocketFactory(context.getSocketFactory()), (X509TrustManager) trustManagers[0]).connectionSpecs(url.getConnectionSpecs().or(Util.immutableList(ConnectionSpec.RESTRICTED_TLS))).dns(dns.or(Dns.SYSTEM));
        if (proxy.isPresent()) {
            builder.socketFactory(new TlsProxySocketFactory(proxy.get().getHost(), proxy.get().getPort(), dns));
        }
        builder.sslSocketFactory(new Tls12SocketFactory(context.getSocketFactory()), (X509TrustManager) trustManagers[0]).connectionSpecs(url.getConnectionSpecs().or(Util.immutableList(ConnectionSpec.RESTRICTED_TLS))).build();
        builder.connectionPool(new ConnectionPool(5, 45, TimeUnit.SECONDS));
        for (Interceptor interceptor : interceptors) {
            builder.addInterceptor(interceptor);
        }
        return builder.build();
    } catch (NoSuchAlgorithmException | KeyManagementException e) {
        throw new AssertionError(e);
    }
}
Also used : ConnectionPool(okhttp3.ConnectionPool) OkHttpClient(okhttp3.OkHttpClient) SSLContext(javax.net.ssl.SSLContext) Tls12SocketFactory(org.whispersystems.signalservice.api.util.Tls12SocketFactory) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) KeyManagementException(java.security.KeyManagementException) X509TrustManager(javax.net.ssl.X509TrustManager) BlacklistingTrustManager(org.whispersystems.signalservice.internal.util.BlacklistingTrustManager) TrustManager(javax.net.ssl.TrustManager) Interceptor(okhttp3.Interceptor) TlsProxySocketFactory(org.whispersystems.signalservice.api.util.TlsProxySocketFactory)

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