Example usage for com.squareup.okhttp Dispatcher getMaxRequests

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

Introduction

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

Prototype

public synchronized int getMaxRequests() 

Source Link

Usage

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

License:Apache License

private ElasticSearchOkHttpClientImpl createClient(MapWrap config) {

    boolean isPrefix;
    String index;/*w  ww.j  a  v a  2s . c o  m*/
    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;
}