package org.voovan.http.client;

import org.voovan.tools.TPerformance;
import org.voovan.tools.pool.ObjectPool;

import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.Supplier;

/**
 * httpclient 连接池
 *
 * @author: helyho
 * voovan Framework.
 * WebSite: https://github.com/helyho/voovan
 * Licence: Apache v2 License
 */
public class HttpClientPool {
    ObjectPool<HttpClient> pool;
    private int minSize = TPerformance.getProcessorCount();
    private int maxSize = TPerformance.getProcessorCount();
    public HttpClientPool(String host, Integer timeout, Integer minSize, Integer maxSize) {
        pool = new ObjectPool<HttpClient>()
            .minSize(minSize).maxSize(maxSize)
            .validator(httpClient -> httpClient.isConnect())
            .supplier(()->{
                return new HttpClient(host, timeout);
            }).destory(httpClient -> {
                httpClient.close();
                return true;
            }).create();
    }

    public ObjectPool<HttpClient> getPool() {
        return pool;
    }

    public HttpClient getHttpClient(long timeout, TimeUnit timeUnit) throws TimeoutException {
        HttpClient httpClient = null;
        timeout = TimeUnit.MILLISECONDS.convert(timeout, timeUnit);
        long start = System.currentTimeMillis();
        do {
            if(System.currentTimeMillis() - start > timeout) {
                throw new TimeoutException("get httpclient timeout");
            }
            httpClient = pool.borrow();
        } while (httpClient==null);

        return httpClient;
    }

    public HttpClient getHttpClient() {
       return pool.borrow();
    }

    public void restitution(HttpClient httpClient) {
        pool.restitution(httpClient);
    }
}

@@ -106,16 +106,38 @@
 tPool;%0A%0A
+import java.util.Map;%0A
 import j
@@ -166,16 +166,16 @@
 meUnit;%0A
-
 import j
@@ -544,19 +544,27 @@
 ate 
-int minSize
+static int MIN_SIZE
  = T
@@ -612,19 +612,27 @@
 ate 
-int maxSize
+static int MAX_SIZE
  = T
@@ -660,24 +660,25 @@
 sorCount();%0A
+%0A
     public H
@@ -1095,16 +1095,16 @@
 n true;%0A
-
         
@@ -1119,32 +1119,146 @@
 reate();%0A    %7D%0A%0A
+    public HttpClientPool(String host, Integer timeout) %7B%0A        this(host, timeout, MIN_SIZE, MAX_SIZE);%0A    %7D%0A%0A
     public Objec

修复 HttpClientPool 默认池大小的 bug