如何有效地重用HttpClient连接?

时间:2023-01-22 21:30:40

I am doing HTTP POSTs very frequently (>= 1/sec) to an API endpoint and I want to make sure I'm doing it efficiently. My goal is to succeed or fail as soon as possible, especially since I have separate code to retry failed POSTs. There is a nice page of HttpClient performance tips, but I'm not sure if exhaustively implementing them all will have real benefits. Here is my code right now:

我非常频繁地对API端点执行HTTP post (>= 1/sec),我希望确保有效地执行它。我的目标是尽快成功或失败,特别是因为我有单独的代码来重试失败的帖子。有一个不错的HttpClient性能技巧页面,但我不确定是否彻底实现它们都有真正的好处。这是我现在的密码:

public class Poster {
  private String url;
  // re-use our request
  private HttpClient client;
  // re-use our method
  private PostMethod method;

  public Poster(String url) {
    this.url = url;

    // Set up the request for reuse.
    HttpClientParams clientParams = new HttpClientParams();
    clientParams.setSoTimeout(1000);  // 1 second timeout.
    this.client = new HttpClient(clientParams);
    // don't check for stale connections, since we want to be as fast as possible?
    // this.client.getParams().setParameter("http.connection.stalecheck", false);

    this.method = new PostMethod(this.url);
    // custom RetryHandler to prevent retry attempts
    HttpMethodRetryHandler myretryhandler = new HttpMethodRetryHandler() {
      public boolean retryMethod(final HttpMethod method, final IOException exception, int executionCount) {
        // For now, never retry
        return false;
      }
    };

    this.method.getParams().setParameter(HttpMethodParams.RETRY_HANDLER, myretryhandler);
  }

  protected boolean sendData(SensorData data) {
    NameValuePair[] payload = {
      // ...
    };
    method.setRequestBody(payload);

    // Execute it and get the results.
    try {
      // Execute the POST method.
      client.executeMethod(method);
    } catch (IOException e) {
      // unable to POST, deal with consequences here
      method.releaseConnection();
      return false;
    }

    // don't release so that it can be reused?
    method.releaseConnection();

    return method.getStatusCode() == HttpStatus.SC_OK;
  }
}

Would it make sense to disable the check for stale connections? Should I be looking at using the MultiThreadedConnectionManager? Of course, actual benchmarking would help but I wanted to check if my code is on the right track first.

禁用检查过期连接是否有意义?我应该使用MultiThreadedConnectionManager吗?当然,实际的基准测试会有所帮助,但我想先检查一下我的代码是否在正确的轨道上。

1 个解决方案

#1


5  

Much of the performance hit of http connections is establishing the socket connection. You can avoid this by using 'keep-alive' http connections. To do this, it's best to use HTTP 1.1 and make sure that "Content-Length: xx" is always set in requests and responses, "Connecction: close" is correctly set when appropriate and is properly acted upon when received.

http连接的大部分性能影响是建立套接字连接。可以通过使用“keep-alive”http连接来避免这种情况。要做到这一点,最好使用HTTP 1.1,并确保在请求和响应中始终设置“Content-Length: xx”,“Connecction: close”在适当时正确设置,并在接收时正确执行。

#1


5  

Much of the performance hit of http connections is establishing the socket connection. You can avoid this by using 'keep-alive' http connections. To do this, it's best to use HTTP 1.1 and make sure that "Content-Length: xx" is always set in requests and responses, "Connecction: close" is correctly set when appropriate and is properly acted upon when received.

http连接的大部分性能影响是建立套接字连接。可以通过使用“keep-alive”http连接来避免这种情况。要做到这一点,最好使用HTTP 1.1,并确保在请求和响应中始终设置“Content-Length: xx”,“Connecction: close”在适当时正确设置,并在接收时正确执行。