package com.cisdi.data.DHV.gateway.config;

import com.google.common.collect.Maps;
import okhttp3.ConnectionPool;
import okhttp3.OkHttpClient;

import java.util.Map;
import java.util.concurrent.TimeUnit;

/**
 * @author jianghc
 * @function:
 * @date 2022年05月07日 10:26
 */
public class YFHttpClient {

    private static Map<Integer, OkHttpClient> map = Maps.newConcurrentMap();


    public static OkHttpClient get(Integer timeout) {

        OkHttpClient okHttpClient = map.get(timeout);

        if(okHttpClient != null) {
            return okHttpClient;
        }

        OkHttpClient.Builder builder = new OkHttpClient.Builder();
        builder.connectTimeout(timeout, TimeUnit.MILLISECONDS)
                .readTimeout(timeout, TimeUnit.MILLISECONDS)
                .writeTimeout(timeout,TimeUnit.MILLISECONDS)
                .connectionPool(new ConnectionPool(15, 1, TimeUnit.MINUTES))
                .retryOnConnectionFailure(true);

        okHttpClient = builder.build();

        map.put(timeout, okHttpClient);

        return okHttpClient;
    }

}
