Example usage for com.squareup.okhttp Dispatcher setMaxRequests

List of usage examples for com.squareup.okhttp Dispatcher setMaxRequests

Introduction

In this page you can find the example usage for com.squareup.okhttp Dispatcher setMaxRequests.

Prototype

public synchronized void setMaxRequests(int maxRequests) 

Source Link

Document

Set the maximum number of requests to execute concurrently.

Usage

From source file:com.frostwire.http.HttpClient.java

License:Open Source License

private static OkHttpClient buildClient(Params params) {
    OkHttpClient c = Loader.DEFAULT_CLIENT.clone();
    ExecutorService pool = params.pool != null ? params.pool : c.getDispatcher().getExecutorService();

    Dispatcher d = new Dispatcher(pool);
    d.setMaxRequests(params.maxRequests);
    d.setMaxRequestsPerHost(params.maxRequestsPerHost);
    c.setDispatcher(d);/*from  w  w w  . j av  a 2 s  .c  o m*/

    return c;
}

From source file:lumbermill.internal.elasticsearch.ElasticsearchClientFactory.java

License:Apache License

private ElasticSearchOkHttpClientImpl createClient(MapWrap config) {

    boolean isPrefix;
    String index;/*w  ww.j  av a 2 s . c om*/
    String url = config.asString("url");
    if (config.exists("index_prefix")) {
        isPrefix = true;
        index = config.asString("index_prefix");
    } else {
        isPrefix = false;
        index = config.asString("index");
    }

    String cacheKey = cacheKey(url, index, isPrefix);
    if (cachedClients.containsKey(cacheKey)) {
        LOGGER.trace("Using cached Elasticsearch client");
        return cachedClients.get(cacheKey);
    }
    LOGGER.trace("Creating new Elasticsearch client");
    final ElasticSearchOkHttpClientImpl es = new ElasticSearchOkHttpClientImpl(url, index,
            config.asString("type"), isPrefix);

    if (config.exists("document_id")) {
        es.withDocumentId(config.asString("document_id"));
    }

    if (config.exists("signer")) {
        es.withSigner(config.getObject("signer"));
    }

    if (config.exists("basic_auth")) {
        if (config.exists("signer")) {
            LOGGER.warn("A client cannot have both signed (AWS) and basic auth. Disabling basic auth");
        } else {
            String auth = config.asString("basic_auth");
            if (!auth.contains(":")) {
                throw new IllegalArgumentException(
                        "Invalid basic_auth value, expected 'user:passwd' but was " + auth);
            }
            if (auth.length() > 1) {
                String[] split = auth.split(":");
                es.withBasicAuth(split[0], split[1]);
            }
        }
    }

    if (config.exists("timestamp_field")) {
        es.withTimestampField(config.asString("timestamp_field"));
    }

    if (config.exists("retry")) {
        MapWrap retryConfig = MapWrap.of(config.getObject("retry")).assertExists("policy");
        es.withRetryTimer(Observables.timer(retryConfig), retryConfig.asInt("attempts", DEFAULT_ATTEMPTS));
    }

    if (config.exists("dispatcher")) {
        LOGGER.info("Configuring http dispatcher");
        MapWrap dispatchConfig = MapWrap.of(config.getObject("dispatcher"));
        Dispatcher dispatcher = dispatchConfig.exists("threadpool")
                ? new Dispatcher(dispatchConfig.getObject("threadpool"))
                : new Dispatcher();
        dispatcher.setMaxRequests(dispatchConfig.exists("max_concurrent_requests")
                ? dispatchConfig.asInt("max_concurrent_requests")
                : dispatcher.getMaxRequests());
        dispatcher.setMaxRequestsPerHost(dispatchConfig.exists("max_concurrent_requests")
                ? dispatchConfig.asInt("max_concurrent_requests")
                : dispatcher.getMaxRequestsPerHost());
        es.withDispatcher(dispatcher);
    }

    cachedClients.put(cacheKey, es);

    return es;
}