Search code examples
javaamazon-web-servicesgroovyaws-api-gatewayapache-commons-httpclient

HttpClient is "reinitialized" after each 100 calls


I have Groovy script that has to post a lot of calls to my API. I am using one http client for all calls - for best performance

There my code:

httpClient = new HttpClient() 
def start = new Date().getTime()
def i = 0

for (item in items) {
  post(item)
  def spend = new Date().getTime()- start // usual call takes 100-300 miliseconds
  start = new Date().getTime()

  if(spend>1000){
  logger.debug i
  }
  ++i
}

def post(item){
  def httpMethod = new PostMethod(endpoint)
  httpMethod.setRequestHeader(new Header("Content-Type", "application/json"))
  httpMethod.setRequestHeader(new Header("Host", AWS.Host))
  httpMethod.setRequestHeader(new Header("x-amz-date", amzDate))
  httpMethod.setRequestHeader(new Header("Authorization", authorizationHeader))
  def requestEntity = new StringRequestEntity(item, "application/json", "UTF-8")
  httpMethod.setRequestEntity(requestEntity)
  def statusCode = httpClient.executeMethod(httpMethod)
  httpMethod.releaseConnection()
  return statusCode >= 200 && statusCode < 300
}

This code printed for me:

DEBUG: 0 : 1504 
DEBUG: 100 : 1389
DEBUG: 200 : 1177
DEBUG: 400 : 1200
DEBUG: 500 : 1058
...

As I get correct httpClient is initialize something during the first call and has to reinitialize same stuff after each 100th call.

EDIT: My code calls Amazon API gateway. When I change method to GET and call Google - this issues wasn't reproduced.

Is it possible to avoid this reinitialization?


Solution

  • Its currently not possible for you to avoid this re-initialization cost.

    API gateway is closing the connection after 100 requests (It'll return Connection: close as one of the response headers). The client has to re-establish the TCP connection and perform a full TLS handshake.

    I am from API Gateway, I'll look at if we can do anything about this. Though, I cannot promise anything.

    UPDATE: This should be less of an issue now. The number has been increased to 1000.