package com.mogo.cloud.network

import com.mogo.cloud.network.NetConstants.Companion.CONNECT_TIMEOUT
import com.mogo.cloud.network.NetConstants.Companion.READ_TIMEOUT
import com.mogo.cloud.network.NetConstants.Companion.WRITE_TIMEOUT
import com.mogo.cloud.network.SSLSocketFactoryUtils.createSSLSocketFactory
import com.mogo.cloud.network.SSLSocketFactoryUtils.createTrustAllManager
import com.mogo.cloud.network.cronet.CronetInterceptor
import com.mogo.cloud.network.interceptor.HttpHeaderInterceptor
import com.mogo.cloud.network.interceptor.HttpLoggingInterceptor
import com.mogo.cloud.network.interceptor.HttpPassportInterceptor
import okhttp3.Dispatcher
import okhttp3.OkHttpClient
import java.util.concurrent.TimeUnit


/**
 *
 * @Author:         wujifei
 * @CreateDate:     1/20/21 12:16 AM
 * @Description:    获取OkHttpClient
 */
class OkHttpFactory private constructor() {
    companion object {
        var dispatcher: Dispatcher = Dispatcher(CustomThreadPool.getThreadPool())

        val okHttpClient: OkHttpClient by lazy {
            //自定义上限，所有总数的最大上限
            dispatcher.setMaxRequests(3000000)
            //自定义上限，单个IP+端口的限制
            dispatcher.setMaxRequestsPerHost(1000000)

            OkHttpClient.Builder()
                .addInterceptor(HttpHeaderInterceptor())
                .addInterceptor(HttpPassportInterceptor())
                .addInterceptor(CronetInterceptor())
                .addInterceptor(HttpLoggingInterceptor())
                .sslSocketFactory(createSSLSocketFactory(), createTrustAllManager())
                .hostnameVerifier(SSLSocketFactoryUtils.TrustAllHostnameVerifier())
                .connectTimeout(CONNECT_TIMEOUT, TimeUnit.MILLISECONDS)
                .readTimeout(READ_TIMEOUT, TimeUnit.MILLISECONDS)
                .writeTimeout(WRITE_TIMEOUT, TimeUnit.MILLISECONDS)
                .eventListenerFactory(WeakNetworkEventFactory.FACTORY)
                .dispatcher(dispatcher)
                .build()
        }


    }
}