<commit_before>package com.ibm.stocator.fs.swift.http;

import java.io.IOException;
import java.io.InterruptedIOException;
import java.net.SocketTimeoutException;
import java.net.UnknownHostException;

import javax.net.ssl.SSLException;

import org.apache.http.HttpEntityEnclosingRequest;
import org.apache.http.HttpRequest;
import org.apache.http.NoHttpResponseException;
import org.apache.http.client.HttpRequestRetryHandler;
import org.apache.http.client.protocol.HttpClientContext;
import org.apache.http.config.SocketConfig;
import org.apache.http.conn.ConnectTimeoutException;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;
import org.apache.http.protocol.HttpContext;

public class SwiftConnectionManager {
  private final PoolingHttpClientConnectionManager connectionPool;

  public SwiftConnectionManager() {
    connectionPool = new PoolingHttpClientConnectionManager();
    connectionPool.setDefaultMaxPerRoute(25);
    connectionPool.setMaxTotal(50);
    SocketConfig socketConfig = SocketConfig.custom()
        .setSoKeepAlive(false).setSoTimeout(10000).build();
    connectionPool.setDefaultSocketConfig(socketConfig);
  }

  private HttpRequestRetryHandler getRetryHandler() {

    HttpRequestRetryHandler myRetryHandler = new HttpRequestRetryHandler() {

      public boolean retryRequest(IOException exception, int executionCount, HttpContext context) {
        System.out.println("Execution count " + executionCount);
        if (executionCount >= 100) {
          // Do not retry if over max retry count
          System.out.println(executionCount);
          return false;
        }
        if (exception instanceof NoHttpResponseException) {
          System.out.println("NoHttpResponseException exception");
          return true;
        }
        if (exception instanceof UnknownHostException) {
          // Unknown host
          System.out.println("11");
          return true;
        }
        if (exception instanceof ConnectTimeoutException) {
          // Connection refused
          System.out.println("12");
          return true;
        }
        if (exception instanceof SocketTimeoutException) {
          // Connection refused
          System.out.println("java.net.SocketTimeoutException");
          return true;
        }
        if (exception.getClass() == SocketTimeoutException.class) {
          System.out.println("java.net.SocketTimeoutException - 1");
          return true;
        }
        if (exception.getClass().isInstance(SocketTimeoutException.class)) {
          System.out.println("java.net.SocketTimeoutException - 2");
          return true;
        }
        if (exception instanceof InterruptedIOException) {
          // Timeout
          System.out.println("1");
          return true;
        }
        if (exception instanceof SSLException) {
          // SSL handshake exception
          System.out.println("13");
          return true;
        }
        System.out.println("weird...");
        HttpClientContext clientContext = HttpClientContext.adapt(context);
        HttpRequest request = clientContext.getRequest();
        boolean idempotent = !(request instanceof HttpEntityEnclosingRequest);
        if (idempotent) {
          // Retry if the request is considered idempotent
          return true;
        }
        System.out.println("return false");
        return false;
      }
    };
    return myRetryHandler;
  }

  public CloseableHttpClient createHttpConnection() {
    /*
     * RequestConfig rConfig = RequestConfig.custom() .setConnectTimeout(5000)
     * .setConnectionRequestTimeout(5000) .setSocketTimeout(5000).build();
     */

    CloseableHttpClient httpclient = HttpClients.custom()
        .setRetryHandler(getRetryHandler())
        .setConnectionManager(connectionPool)
        // .setDefaultRequestConfig(rConfig)
        .build();
    return httpclient;
  }
}<commit_msg>finalizing: using pool connection manager for JOSS and SwiftDirectClient<commit_after>package com.ibm.stocator.fs.swift.http;

import java.io.IOException;
import java.io.InterruptedIOException;
import java.net.SocketTimeoutException;
import java.net.UnknownHostException;

import javax.net.ssl.SSLException;

import org.apache.http.HttpEntityEnclosingRequest;
import org.apache.http.HttpRequest;
import org.apache.http.NoHttpResponseException;
import org.apache.http.client.HttpRequestRetryHandler;
import org.apache.http.client.protocol.HttpClientContext;
import org.apache.http.config.SocketConfig;
import org.apache.http.conn.ConnectTimeoutException;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;
import org.apache.http.protocol.HttpContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * Connection manager for Swift API object store
 * Use pooling connection pool manager and custom retry handler
 * The same pooling manager is used by other components and not only by JOSS
 */
public class SwiftConnectionManager {
  /*
   * Logger
   */
  private static final Logger LOG = LoggerFactory.getLogger(SwiftConnectionManager.class);
  /*
   * Connection pool
   */
  private final PoolingHttpClientConnectionManager connectionPool;

  /**
   * Default constructor
   */
  public SwiftConnectionManager() {
    connectionPool = new PoolingHttpClientConnectionManager();
    connectionPool.setDefaultMaxPerRoute(25);
    connectionPool.setMaxTotal(50);
    SocketConfig socketConfig = SocketConfig.custom()
        .setSoKeepAlive(false).setSoTimeout(10000).build();
    connectionPool.setDefaultSocketConfig(socketConfig);
  }

  /**
   * Creates custom retry handler to be used if HTTP exception happens
   *
   * @return retry handler
   */
  private HttpRequestRetryHandler getRetryHandler() {

    HttpRequestRetryHandler myRetryHandler = new HttpRequestRetryHandler() {

      public boolean retryRequest(IOException exception, int executionCount, HttpContext context) {
        if (executionCount >= 100) {
          // Do not retry if over max retry count
          LOG.debug("Execution count {} is bigger then threashold. Stop" ,executionCount);
          return false;
        }
        if (exception instanceof NoHttpResponseException) {
          LOG.debug("NoHttpResponseException exception. Retry count {}", executionCount);
          return true;
        }
        if (exception instanceof UnknownHostException) {
          LOG.debug("UnknownHostException. Retry count {}", executionCount);
          return true;
        }
        if (exception instanceof ConnectTimeoutException) {
          LOG.debug("ConnectTimeoutException. Retry count {}", executionCount);
          return true;
        }
        if (exception instanceof SocketTimeoutException
            || exception.getClass() == SocketTimeoutException.class
            || exception.getClass().isInstance(SocketTimeoutException.class)) {
          // Connection refused
          LOG.debug("socketTimeoutException Retry count {}", executionCount);
          return true;
        }
        if (exception instanceof InterruptedIOException) {
          // Timeout
          LOG.debug("InterruptedIOException Retry count {}", executionCount);
          return true;
        }
        if (exception instanceof SSLException) {
          LOG.debug("SSLException Retry count {}", executionCount);
          return true;
        }
        HttpClientContext clientContext = HttpClientContext.adapt(context);
        HttpRequest request = clientContext.getRequest();
        boolean idempotent = !(request instanceof HttpEntityEnclosingRequest);
        if (idempotent) {
          LOG.debug("HttpEntityEnclosingRequest. Retry count {}", executionCount);
          return true;
        }
        LOG.debug("Retry stopped. Retry count {}", executionCount);
        return false;
      }
    };
    return myRetryHandler;
  }

  /**
   * Creates HTTP connection based on the connection pool
   *
   * @return http client
   */
  public CloseableHttpClient createHttpConnection() {
    /*
     * RequestConfig rConfig = RequestConfig.custom() .setConnectTimeout(5000)
     * .setConnectionRequestTimeout(5000) .setSocketTimeout(5000).build();
     */

    CloseableHttpClient httpclient = HttpClients.custom()
        .setRetryHandler(getRetryHandler())
        .setConnectionManager(connectionPool)
        // .setDefaultRequestConfig(rConfig)
        .build();
    return httpclient;
  }
}
{'fraction_non_alphanumeric': 0.09066604672788563, 'fraction_numerical': 0.006858072765314425, 'mean_word_length': 3.1585306911551476, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, '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': '4265938', 'n_tokens_mistral': 2209, 'n_tokens_neox': 2156, 'n_words': 478}