Twilio API 请求在使用 Twilio Java Helper Library 发送时响应时间过长

Twilio API requests taking too long to respond when sent using Twilio Java Helper Library

我们在 Web 项目中使用 Twilio Task Router 来实现呼叫路由功能(Java 8 应用程序 运行 on Tomcat 9)。我们正在使用 Twilio Java SDK (https://mvnrepository.com/artifact/com.twilio.sdk/twilio/7.36.2) 来发出 Twilio API 请求。当我们测试我们的应用程序时,发现 Twilio 对 1 小时内发送的几乎每个 API 请求的响应时间都太长了。对于分析的几个示例请求,响应时间高达 10 分钟。

下面提供了一个此类请求的日志跟踪。在此日志跟踪中,发现在执行“Connection request: [route: {s}->https://taskrouter.twilio.com:443][total kept alive: 0; route allocated: 10 of 10; total allocated: 10 of 20]”行后发生了 10 分钟的延迟。根据我们的分析,此跟踪来自 Apache HttpClient 库,它是 Twilio SDK 的一部分。下一个跟踪:10 分钟后出现的“Connection leased: [id: 3492][route: {s}->https://taskrouter.twilio.com:443][total kept alive: 0; route allocated: 10 of 10; total allocated: 10 of 20]”也来自 Apache HttpClient。根据以下日志中的 http-outgoing 跟踪,Twilio-Request-Duration 仅为 27 毫秒。因此,我们假设 10 分钟的延迟发生在 SDK 中,而不是来自 Twilio 服务器。请帮助我们解决这个问题。提前致谢。

| DEBUG | 2019-07-09 10:31:22.688 | https-jsse-nio-8443-exec-342 | TwilioCommandExecutor:38 | Executing Twilio command
| DEBUG | 2019-07-09 10:31:22.688 | https-jsse-nio-8443-exec-342 | RequestAddCookies:123 | CookieSpec selected: default
| DEBUG | 2019-07-09 10:31:23.756 | https-jsse-nio-8443-exec-342 | RequestAuthCache:77 | Auth cache not set in the context
| DEBUG | 2019-07-09 10:31:23.772 | https-jsse-nio-8443-exec-342 | PoolingHttpClientConnectionManager:265 | Connection request: [route: {s}->https://taskrouter.twilio.com:443][total kept alive: 0; route allocated: 10 of 10; total allocated: 10 of 20]
| DEBUG | 2019-07-09 10:41:39.018 | https-jsse-nio-8443-exec-342 | PoolingHttpClientConnectionManager:309 | Connection leased: [id: 3492][route: {s}->https://taskrouter.twilio.com:443][total kept alive: 0; route allocated: 10 of 10; total allocated: 10 of 20]
| DEBUG | 2019-07-09 10:41:39.020 | https-jsse-nio-8443-exec-342 | DefaultManagedHttpClientConnection:88 | http-outgoing-3492: set socket timeout to 0
| DEBUG | 2019-07-09 10:41:39.020 | https-jsse-nio-8443-exec-342 | DefaultManagedHttpClientConnection:88 | http-outgoing-3492: set socket timeout to 30500
| DEBUG | 2019-07-09 10:41:39.020 | https-jsse-nio-8443-exec-342 | MainClientExec:256 | Executing request POST /v1/Workspaces/WSd9f51d1692a76faa3079514c2a89197c/Workers/WK9cacec1e7285f98519152316bac03de8 HTTP/1.1
| DEBUG | 2019-07-09 10:41:39.021 | https-jsse-nio-8443-exec-342 | MainClientExec:267 | Proxy auth state: UNCHALLENGED
| DEBUG | 2019-07-09 10:41:39.021 | https-jsse-nio-8443-exec-342 | headers:133 | http-outgoing-3492 >> POST /v1/Workspaces/WSd9f51d1692a76faa3079514c2a89197c/Workers/WK9cacec1e7285f98519152316bac03de8 HTTP/1.1
| DEBUG | 2019-07-09 10:41:39.096 | https-jsse-nio-8443-exec-342 | wire:73 | http-outgoing-3492 << "HTTP/1.1 200 OK[\r][\n]"
| DEBUG | 2019-07-09 10:41:39.105 | https-jsse-nio-8443-exec-342 | wire:73 | http-outgoing-3492 << "Date: Tue, 09 Jul 2019 14:41:39 GMT[\r][\n]"
| DEBUG | 2019-07-09 10:41:39.106 | https-jsse-nio-8443-exec-342 | wire:73 | http-outgoing-3492 << "Content-Type: application/json[\r][\n]"
| DEBUG | 2019-07-09 10:41:39.106 | https-jsse-nio-8443-exec-342 | wire:73 | http-outgoing-3492 << "Content-Length: 1891[\r][\n]"
| DEBUG | 2019-07-09 10:41:39.106 | https-jsse-nio-8443-exec-342 | wire:73 | http-outgoing-3492 << "Connection: keep-alive[\r][\n]"
| DEBUG | 2019-07-09 10:41:39.131 | https-jsse-nio-8443-exec-342 | headers:125 | http-outgoing-3492 << Twilio-Request-Duration: 0.027
| DEBUG | 2019-07-09 10:41:39.133 | https-jsse-nio-8443-exec-342 | MainClientExec:285 | Connection can be kept alive indefinitely
| DEBUG | 2019-07-09 10:41:39.134 | https-jsse-nio-8443-exec-342 | PoolingHttpClientConnectionManager:341 | Connection [id: 3492][route: {s}->https://taskrouter.twilio.com:443] can be kept alive indefinitely
| DEBUG | 2019-07-09 10:41:39.137 | https-jsse-nio-8443-exec-342 | DefaultManagedHttpClientConnection:88 | http-outgoing-3492: set socket timeout to 0
| DEBUG | 2019-07-09 10:41:39.424 | https-jsse-nio-8443-exec-342 | PoolingHttpClientConnectionManager:348 | Connection released: [id: 3492][route: {s}->https://taskrouter.twilio.com:443][total kept alive: 1; route allocated: 10 of 10; total allocated: 10 of 20]
| DEBUG | 2019-07-09 10:41:39.424 | https-jsse-nio-8443-exec-342 | TwilioCommandExecutor:40 | Twilio command executed successfully

此处为 Twilio 开发人员布道师。

根据您的评论,您在 15 分钟内提出了大约 25,000 个 API 请求。这大约是每秒 27 个请求。 Twilio 本身有 100 个并发连接的限制,但 Twilio Java 库有一个包含 20 个 HTTP 连接对象的池。如果你在一个回合中将所有 25,000 个请求扔到库中,那么它将从使用池的前 20 个请求开始,并发出它们。一旦每个请求释放连接,就会发出下一个请求。最终这意味着 25,000 个请求中的最后一个将不得不等待之前的 24,999 个请求发生,从而造成 10 分钟的延迟。

如果连接池的大小对您来说不够大,您可以向库提供自己的 HTTPClient。你可以看看现有的实现NetworkHTTPClient which is likely a good model to base your own implementation on. The connection pool limits are implemented here. Note that making a connection pool larger than 100 will result in API errors for higher than allowed concurrent use. If you need to consider higher API limits, I recommend you talk to Twilio sales关于你的需求。

或者,您可以考虑在这么短的时间内是否需要这么多请求。如果可以对请求进行不同的批处理并在不同的时间进行调用,则可以避免这种级别的请求排队。

如果有帮助请告诉我。