Search in sources :

Example 1 with Dispatcher

use of okhttp3.Dispatcher in project buck by facebook.

the class ArtifactCaches method createHttpArtifactCache.

private static ArtifactCache createHttpArtifactCache(HttpCacheEntry cacheDescription, final String hostToReportToRemote, final BuckEventBus buckEventBus, ProjectFilesystem projectFilesystem, ListeningExecutorService httpWriteExecutorService, ArtifactCacheBuckConfig config, NetworkCacheFactory factory, boolean distributedBuildModeEnabled) {
    // Setup the default client to use.
    OkHttpClient.Builder storeClientBuilder = new OkHttpClient.Builder();
    storeClientBuilder.networkInterceptors().add(chain -> chain.proceed(chain.request().newBuilder().addHeader("X-BuckCache-User", stripNonAscii(System.getProperty("user.name", "<unknown>"))).addHeader("X-BuckCache-Host", stripNonAscii(hostToReportToRemote)).build()));
    int timeoutSeconds = cacheDescription.getTimeoutSeconds();
    setTimeouts(storeClientBuilder, timeoutSeconds);
    storeClientBuilder.connectionPool(new ConnectionPool(/* maxIdleConnections */
    (int) config.getThreadPoolSize(), /* keepAliveDurationMs */
    config.getThreadPoolKeepAliveDurationMillis(), TimeUnit.MILLISECONDS));
    // The artifact cache effectively only connects to a single host at a time. We should allow as
    // many concurrent connections to that host as we allow threads.
    Dispatcher dispatcher = new Dispatcher();
    dispatcher.setMaxRequestsPerHost((int) config.getThreadPoolSize());
    storeClientBuilder.dispatcher(dispatcher);
    final ImmutableMap<String, String> readHeaders = cacheDescription.getReadHeaders();
    final ImmutableMap<String, String> writeHeaders = cacheDescription.getWriteHeaders();
    // If write headers are specified, add them to every default client request.
    if (!writeHeaders.isEmpty()) {
        storeClientBuilder.networkInterceptors().add(chain -> chain.proceed(addHeadersToBuilder(chain.request().newBuilder(), writeHeaders).build()));
    }
    OkHttpClient storeClient = storeClientBuilder.build();
    // For fetches, use a client with a read timeout.
    OkHttpClient.Builder fetchClientBuilder = storeClient.newBuilder();
    setTimeouts(fetchClientBuilder, timeoutSeconds);
    // If read headers are specified, add them to every read client request.
    if (!readHeaders.isEmpty()) {
        fetchClientBuilder.networkInterceptors().add(chain -> chain.proceed(addHeadersToBuilder(chain.request().newBuilder(), readHeaders).build()));
    }
    fetchClientBuilder.networkInterceptors().add((chain -> {
        Response originalResponse = chain.proceed(chain.request());
        return originalResponse.newBuilder().body(new ProgressResponseBody(originalResponse.body(), buckEventBus)).build();
    }));
    OkHttpClient fetchClient = fetchClientBuilder.build();
    HttpService fetchService;
    HttpService storeService;
    switch(config.getLoadBalancingType()) {
        case CLIENT_SLB:
            HttpLoadBalancer clientSideSlb = config.getSlbConfig().createClientSideSlb(new DefaultClock(), buckEventBus, new CommandThreadFactory("ArtifactCaches.HttpLoadBalancer", SLB_THREAD_PRIORITY));
            fetchService = new RetryingHttpService(buckEventBus, new LoadBalancedService(clientSideSlb, fetchClient, buckEventBus), config.getMaxFetchRetries());
            storeService = new LoadBalancedService(clientSideSlb, storeClient, buckEventBus);
            break;
        case SINGLE_SERVER:
            URI url = cacheDescription.getUrl();
            fetchService = new SingleUriService(url, fetchClient);
            storeService = new SingleUriService(url, storeClient);
            break;
        default:
            throw new IllegalArgumentException("Unknown HttpLoadBalancer type: " + config.getLoadBalancingType());
    }
    String cacheName = cacheDescription.getName().map(input -> "http-" + input).orElse("http");
    boolean doStore = cacheDescription.getCacheReadMode().isDoStore();
    return factory.newInstance(NetworkCacheArgs.builder().setThriftEndpointPath(config.getHybridThriftEndpoint()).setCacheName(cacheName).setRepository(config.getRepository()).setScheduleType(config.getScheduleType()).setFetchClient(fetchService).setStoreClient(storeService).setDoStore(doStore).setProjectFilesystem(projectFilesystem).setBuckEventBus(buckEventBus).setHttpWriteExecutorService(httpWriteExecutorService).setErrorTextTemplate(cacheDescription.getErrorMessageFormat()).setDistributedBuildModeEnabled(distributedBuildModeEnabled).build());
}
Also used : ConnectionPool(okhttp3.ConnectionPool) BuckEventBus(com.facebook.buck.event.BuckEventBus) Okio(okio.Okio) Source(okio.Source) BytesReceivedEvent(com.facebook.buck.event.NetworkEvent.BytesReceivedEvent) RetryingHttpService(com.facebook.buck.slb.RetryingHttpService) Dispatcher(okhttp3.Dispatcher) ProjectFilesystem(com.facebook.buck.io.ProjectFilesystem) CommonGroups(com.facebook.buck.randomizedtrial.CommonGroups) BuckConfig(com.facebook.buck.cli.BuckConfig) ImmutableList(com.google.common.collect.ImmutableList) DefaultClock(com.facebook.buck.timing.DefaultClock) Map(java.util.Map) ForwardingSource(okio.ForwardingSource) Response(okhttp3.Response) HttpLoadBalancer(com.facebook.buck.slb.HttpLoadBalancer) URI(java.net.URI) LoadBalancedService(com.facebook.buck.slb.LoadBalancedService) Path(java.nio.file.Path) MediaType(okhttp3.MediaType) ResponseBody(okhttp3.ResponseBody) HttpService(com.facebook.buck.slb.HttpService) Logger(com.facebook.buck.log.Logger) Request(okhttp3.Request) Buffer(okio.Buffer) SingleUriService(com.facebook.buck.slb.SingleUriService) ImmutableSet(com.google.common.collect.ImmutableSet) ImmutableMap(com.google.common.collect.ImmutableMap) CharMatcher(com.google.common.base.CharMatcher) IOException(java.io.IOException) HumanReadableException(com.facebook.buck.util.HumanReadableException) CommandThreadFactory(com.facebook.buck.log.CommandThreadFactory) TimeUnit(java.util.concurrent.TimeUnit) BufferedSource(okio.BufferedSource) OkHttpClient(okhttp3.OkHttpClient) Optional(java.util.Optional) ConnectionPool(okhttp3.ConnectionPool) AsyncCloseable(com.facebook.buck.util.AsyncCloseable) RandomizedTrial(com.facebook.buck.randomizedtrial.RandomizedTrial) DirCacheExperimentEvent(com.facebook.buck.event.DirCacheExperimentEvent) ListeningExecutorService(com.google.common.util.concurrent.ListeningExecutorService) OkHttpClient(okhttp3.OkHttpClient) CommandThreadFactory(com.facebook.buck.log.CommandThreadFactory) Dispatcher(okhttp3.Dispatcher) RetryingHttpService(com.facebook.buck.slb.RetryingHttpService) URI(java.net.URI) Response(okhttp3.Response) RetryingHttpService(com.facebook.buck.slb.RetryingHttpService) HttpService(com.facebook.buck.slb.HttpService) DefaultClock(com.facebook.buck.timing.DefaultClock) LoadBalancedService(com.facebook.buck.slb.LoadBalancedService) SingleUriService(com.facebook.buck.slb.SingleUriService) HttpLoadBalancer(com.facebook.buck.slb.HttpLoadBalancer)

Example 2 with Dispatcher

use of okhttp3.Dispatcher in project zipkin by openzipkin.

the class TraceZipkinElasticsearchHttpStorageAutoConfiguration method elasticsearchOkHttpClientBuilder.

@Bean
@Qualifier("zipkinElasticsearchHttp")
@ConditionalOnMissingBean
OkHttpClient.Builder elasticsearchOkHttpClientBuilder() {
    // have to indirect to unwind a circular dependency
    Interceptor tracingInterceptor = new Interceptor() {

        Interceptor delegate = BraveTracingInterceptor.builder(brave).serverName("elasticsearch").build();

        @Override
        public Response intercept(Chain chain) throws IOException {
            // Only join traces, don't start them. This prevents LocalCollector's thread from amplifying.
            if (brave.serverSpanThreadBinder().getCurrentServerSpan() != null && brave.serverSpanThreadBinder().getCurrentServerSpan().getSpan() != null) {
                return delegate.intercept(chain);
            }
            return chain.proceed(chain.request());
        }
    };
    BraveExecutorService tracePropagatingExecutor = BraveExecutorService.wrap(new Dispatcher().executorService(), brave);
    OkHttpClient.Builder builder = new OkHttpClient.Builder();
    builder.addInterceptor(tracingInterceptor);
    builder.addNetworkInterceptor(tracingInterceptor);
    builder.dispatcher(new Dispatcher(tracePropagatingExecutor));
    return builder;
}
Also used : BraveExecutorService(com.github.kristofa.brave.BraveExecutorService) OkHttpClient(okhttp3.OkHttpClient) Dispatcher(okhttp3.Dispatcher) Interceptor(okhttp3.Interceptor) BraveTracingInterceptor(com.github.kristofa.brave.okhttp.BraveTracingInterceptor) ConditionalOnMissingBean(org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean) Qualifier(org.springframework.beans.factory.annotation.Qualifier) ConditionalOnBean(org.springframework.boot.autoconfigure.condition.ConditionalOnBean) ConditionalOnMissingBean(org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean) Bean(org.springframework.context.annotation.Bean)

Example 3 with Dispatcher

use of okhttp3.Dispatcher in project Tusky by Vavassor.

the class BaseActivity method createMastodonAPI.

protected void createMastodonAPI() {
    mastodonApiDispatcher = new Dispatcher();
    Gson gson = new GsonBuilder().registerTypeAdapter(Spanned.class, new SpannedTypeAdapter()).registerTypeAdapter(StringWithEmoji.class, new StringWithEmojiTypeAdapter()).create();
    OkHttpClient okHttpClient = OkHttpUtils.getCompatibleClientBuilder().addInterceptor(new Interceptor() {

        @Override
        public Response intercept(Chain chain) throws IOException {
            Request originalRequest = chain.request();
            Request.Builder builder = originalRequest.newBuilder();
            String accessToken = getAccessToken();
            if (accessToken != null) {
                builder.header("Authorization", String.format("Bearer %s", accessToken));
            }
            Request newRequest = builder.build();
            return chain.proceed(newRequest);
        }
    }).dispatcher(mastodonApiDispatcher).build();
    Retrofit retrofit = new Retrofit.Builder().baseUrl(getBaseUrl()).client(okHttpClient).addConverterFactory(GsonConverterFactory.create(gson)).build();
    mastodonAPI = retrofit.create(MastodonAPI.class);
}
Also used : OkHttpClient(okhttp3.OkHttpClient) GsonBuilder(com.google.gson.GsonBuilder) GsonBuilder(com.google.gson.GsonBuilder) SpannedTypeAdapter(com.keylesspalace.tusky.json.SpannedTypeAdapter) Request(okhttp3.Request) Gson(com.google.gson.Gson) IOException(java.io.IOException) Dispatcher(okhttp3.Dispatcher) Response(okhttp3.Response) Retrofit(retrofit2.Retrofit) StringWithEmojiTypeAdapter(com.keylesspalace.tusky.json.StringWithEmojiTypeAdapter) MastodonAPI(com.keylesspalace.tusky.network.MastodonAPI) Interceptor(okhttp3.Interceptor) StringWithEmoji(com.keylesspalace.tusky.json.StringWithEmoji)

Example 4 with Dispatcher

use of okhttp3.Dispatcher in project amhttp by Eddieyuan123.

the class RequestManager method upload.

public <T> void upload(Context context, String url, CacheControl cacheControl, HashMap<String, String> headers, File file, String fileName, HashMap<String, String> params, Object tag, final OnUploadListener<T> listener) {
    try {
        IRequestBodyFactory requestBodyFactory = new RequestBodyFactoryImpl();
        RequestBody requestBody = requestBodyFactory.buildRequestBody(file, fileName, params);
        final Request request = new Request.Builder().cacheControl(cacheControl == null ? CacheControl.FORCE_NETWORK : cacheControl).headers(Headers.of(headers)).tag(tag == null ? context.hashCode() : tag).url(url).post(new ProgressRequestBody(requestBody, new ProgressRequestListener() {

            @Override
            public void onRequestProgress(final long bytesWritten, final long contentLength, final boolean done) {
                if (listener != null) {
                    Dispatcher dispatcher = Dispatcher.getDispatcher(Looper.getMainLooper());
                    dispatcher.setRequestListener(listener);
                    Message message = Message.obtain();
                    message.what = MessageConstant.MESSAGE_UPLOAD_PROGRESS;
                    Bundle bundle = new Bundle();
                    bundle.putLong("bytesWritten", bytesWritten);
                    bundle.putLong("contentLength", contentLength);
                    bundle.putBoolean("done", done);
                    message.obj = bundle;
                    dispatcher.sendMessage(message);
                }
            }
        })).build();
        RequestUtils.enqueue(mOkHttpClient, request, listener);
    } catch (Exception e) {
        e.printStackTrace();
    }
}
Also used : Message(android.os.Message) Bundle(android.os.Bundle) Request(okhttp3.Request) ProgressRequestListener(io.chelizi.amokhttp.upload.ProgressRequestListener) ProgressRequestBody(io.chelizi.amokhttp.upload.ProgressRequestBody) ProgressRequestBody(io.chelizi.amokhttp.upload.ProgressRequestBody) RequestBody(okhttp3.RequestBody)

Example 5 with Dispatcher

use of okhttp3.Dispatcher in project amhttp by Eddieyuan123.

the class OnAddListener method parseNetworkResponse.

@Override
public void parseNetworkResponse(Response response, FileCard fileCard) throws Throwable {
    ResponseBody responseBody = response.body();
    if (responseBody == null) {
        throw new NullPointerException("response body is null");
    } else {
        String responseStr = responseBody.string();
        Type type = ClassUtils.getType(OnAddListener.this.getClass());
        T bean = null;
        if (type != null) {
            if (TextUtils.equals(type.toString(), "class java.lang.String"))
                bean = (T) responseStr;
            else
                bean = new Gson().fromJson(responseStr, type);
        }
        final T finalBean = bean;
        Dispatcher dispatcher = Dispatcher.getDispatcher(Looper.getMainLooper());
        dispatcher.dispatchToUIThread(new Runnable() {

            @Override
            public void run() {
                onResponseSuccess(finalBean);
            }
        });
    }
}
Also used : Type(java.lang.reflect.Type) Gson(com.google.gson.Gson) Dispatcher(io.chelizi.amokhttp.Dispatcher) ResponseBody(okhttp3.ResponseBody)

Aggregations

Dispatcher (okhttp3.Dispatcher)40 OkHttpClient (okhttp3.OkHttpClient)34 MockResponse (okhttp3.mockwebserver.MockResponse)27 RecordedRequest (okhttp3.mockwebserver.RecordedRequest)25 Dispatcher (okhttp3.mockwebserver.Dispatcher)24 IOException (java.io.IOException)17 Before (org.junit.Before)14 Test (org.junit.Test)13 Call (okhttp3.Call)11 MockWebServer (okhttp3.mockwebserver.MockWebServer)11 LruNormalizedCacheFactory (com.apollographql.apollo.cache.normalized.lru.LruNormalizedCacheFactory)10 ArrayList (java.util.ArrayList)8 Request (okhttp3.Request)8 Response (okhttp3.Response)8 ResponseBody (okhttp3.ResponseBody)7 Gson (com.google.gson.Gson)5 Interceptor (okhttp3.Interceptor)5 CloudantClient (com.cloudant.client.api.CloudantClient)4 RefineTest (com.google.refine.RefineTest)4 DataExtensionConfig (com.google.refine.model.recon.ReconciledDataExtensionJob.DataExtensionConfig)4