gpt4 book ai didi

java - 每 100 次调用后 HttpClient 为 "reinitialized"

转载 作者:行者123 更新时间:2023-12-01 09:31:26 26 4
gpt4 key购买 nike

我有 Groovy 脚本,它必须向我的 API 发出大量调用。我对所有调用都使用一个 http 客户端 - for best performance

这是我的代码:

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
}

为我打印的代码:

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

据我所知,httpClient 在第一次调用期间初始化了某些内容,并且在每 100 次调用后必须重新初始化相同的内容。

编辑:我的代码调用 Amazon API 网关。当我将方法更改为 GET 并调用 Google 时,未重现此问题。

是否可以避免这种重新初始化?

最佳答案

目前您无法避免这种重新初始化成本。

API 网关在 100 个请求后关闭连接(它将返回 Connection: close 作为响应 header 之一)。客户端必须重新建立 TCP 连接并执行完整的 TLS 握手。

我来自 API Gateway,我会看看我们是否可以对此做些什么。不过,我不能保证任何事情。

更新:现在这应该不是什么问题了。数量已增加至1000。

关于java - 每 100 次调用后 HttpClient 为 "reinitialized",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39368633/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com