<commit_before>package com.reizes.shiva2.http;

import java.io.Closeable;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.Map;
import java.util.concurrent.atomic.AtomicBoolean;

import org.apache.http.Consts;
import org.apache.http.HttpEntity;
import org.apache.http.HttpHost;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpUriRequest;
import org.apache.http.config.SocketConfig;
import org.apache.http.entity.BufferedHttpEntity;
import org.apache.http.entity.FileEntity;
import org.apache.http.entity.InputStreamEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;

import com.google.gson.Gson;
import com.reizes.shiva2.core.ExceptionListener;
import com.reizes.shiva2.http.inner.Utils;

import lombok.Getter;
import lombok.Setter;

public class ThreadedRestClient implements Closeable {
	@Getter
	@Setter
	private int maxTotal = 100;
	@Getter
	@Setter
	private int maxPerRoute = 20;
	@Getter
	@Setter
	private int timeout = 5000;
	private Gson gson = new Gson();
	private static ThreadedRestClient instance = null;
	private AtomicBoolean closed = new AtomicBoolean(true);
	
	private CloseableHttpClient httpclient;
	@Getter
	@Setter
	private ExceptionListener exceptionListener;
	PoolingHttpClientConnectionManager connManager;
	
	private ThreadedRestClient() {
		httpclient = initHttpClient();
	}
	
	public synchronized static ThreadedRestClient getInstance() {
		if (instance==null) {
			instance = new ThreadedRestClient();
		}
		
		return instance;
	}

	protected synchronized CloseableHttpClient initHttpClient() {
		connManager = new PoolingHttpClientConnectionManager();
		connManager.setMaxTotal(maxTotal);
		connManager.setDefaultMaxPerRoute(maxPerRoute);
		connManager.setDefaultSocketConfig(SocketConfig.custom().setSoTimeout(timeout).build());
		CloseableHttpClient client = HttpClients.custom().setConnectionManager(connManager).build();
		closed.set(true);
		return client;
	}

	public void requestAsync(URI uri, Method method, String requestUri, Map<String, String> headers, HttpEntity requestEntity, HttpRequestCallback callback) throws IOException {
		if (!closed.get()) {
			HttpHost target = new HttpHost(uri.getHost(), uri.getPort(), uri.getScheme());
			HttpUriRequest request = Utils.buildRequest(method, requestUri, headers, requestEntity);
			HttpClientThread thread = new HttpClientThread(httpclient, target, request, callback);
			thread.setExceptionListener(exceptionListener);
			thread.start();
		}
	}
	
	public void requestAsync(String uri, Method method, String requestUri, Map<String, String> headers, HttpEntity requestEntity, HttpRequestCallback callback) throws IOException, URISyntaxException {
		requestAsync(new URI(uri), method, requestUri, headers, requestEntity, callback);
	}

	@Override
	public synchronized void close() throws IOException {
		if (!closed.get()) {
			closed.set(true);
			httpclient.close();
			connManager.shutdown();
		}<commit_msg>fix ThreadedRestClient.isClosed bug<commit_after>package com.reizes.shiva2.http;

import java.io.Closeable;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.Map;
import java.util.concurrent.atomic.AtomicBoolean;

import org.apache.http.Consts;
import org.apache.http.HttpEntity;
import org.apache.http.HttpHost;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpUriRequest;
import org.apache.http.config.SocketConfig;
import org.apache.http.entity.BufferedHttpEntity;
import org.apache.http.entity.FileEntity;
import org.apache.http.entity.InputStreamEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;

import com.google.gson.Gson;
import com.reizes.shiva2.core.ExceptionListener;
import com.reizes.shiva2.http.inner.Utils;

import lombok.Getter;
import lombok.Setter;

public class ThreadedRestClient implements Closeable {
	@Getter
	@Setter
	private int maxTotal = 100;
	@Getter
	@Setter
	private int maxPerRoute = 20;
	@Getter
	@Setter
	private int timeout = 5000;
	private Gson gson = new Gson();
	private static ThreadedRestClient instance = null;
	private AtomicBoolean closed = new AtomicBoolean(true);
	
	private CloseableHttpClient httpclient;
	@Getter
	@Setter
	private ExceptionListener exceptionListener;
	PoolingHttpClientConnectionManager connManager;
	
	private ThreadedRestClient() {
		httpclient = initHttpClient();
	}
	
	public synchronized static ThreadedRestClient getInstance() {
		if (instance==null) {
			instance = new ThreadedRestClient();
		}
		
		return instance;
	}

	protected synchronized CloseableHttpClient initHttpClient() {
		connManager = new PoolingHttpClientConnectionManager();
		connManager.setMaxTotal(maxTotal);
		connManager.setDefaultMaxPerRoute(maxPerRoute);
		connManager.setDefaultSocketConfig(SocketConfig.custom().setSoTimeout(timeout).build());
		CloseableHttpClient client = HttpClients.custom().setConnectionManager(connManager).build();
		closed.set(false);
		return client;
	}

	public void requestAsync(URI uri, Method method, String requestUri, Map<String, String> headers, HttpEntity requestEntity, HttpRequestCallback callback) throws IOException {
		if (!closed.get()) {
			HttpHost target = new HttpHost(uri.getHost(), uri.getPort(), uri.getScheme());
			HttpUriRequest request = Utils.buildRequest(method, requestUri, headers, requestEntity);
			HttpClientThread thread = new HttpClientThread(httpclient, target, request, callback);
			thread.setExceptionListener(exceptionListener);
			thread.start();
		}
	}
	
	public void requestAsync(String uri, Method method, String requestUri, Map<String, String> headers, HttpEntity requestEntity, HttpRequestCallback callback) throws IOException, URISyntaxException {
		requestAsync(new URI(uri), method, requestUri, headers, requestEntity, callback);
	}

	@Override
	public synchronized void close() throws IOException {
		if (!closed.get()) {
			closed.set(true);
			httpclient.close();
			connManager.shutdown();
		}
{'fraction_non_alphanumeric': 0.09882537196554425, 'fraction_numerical': 0.003758809710258418, 'mean_word_length': 6.033039647577093, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '261133', 'n_tokens_mistral': 1952, 'n_tokens_neox': 1829, 'n_words': 420}