使用Twilio Java Helper库发送Twilio API请求时,响应时间太长

时间:2019-07-10 15:12:11

标签: twilio twilio-taskrouter

我们将Twilio Task Router用于我们的Web项目(在Tomcat 9上运行的Java 8应用程序)中的呼叫路由功能。我们正在使用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分钟的延迟。根据我们的分析,此跟踪来自Twilio SDK一部分的Apache HttpClient库。 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毫秒。因此,我们假设在SDK而不是Twilio服务器中发生了10分钟的延迟。请帮助我们解决此问题。预先感谢。

| 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

1 个答案:

答案 0 :(得分:1)

这里是Twilio开发人员的传播者。

根据您的评论,您在15分钟内发出了大约25,000个API请求。大约每秒27个请求。 Twilio本身有100个并发连接限制,但是Twilio Java库有20个HTTP连接对象池。如果您要一遍向库抛出所有25,000个请求,则将从使用池的前20个请求开始并进行处理。每个请求释放连接后,将发出下一个请求。最终,这意味着25,000个请求中的最后一个将不得不等待之前的24,999个请求发生,从而导致您延迟10分钟。

如果连接池的大小不足以容纳您,则可以向库提供您自己的HTTPClient。您可以看到NetworkHTTPClient的现有实现,这可能是基于您自己的实现的好模型。 connection pool limits are implemented here。请注意,将连接池设置为大于100将导致API错误,从而导致并发使用次数超出允许范围。如果您需要考虑更高的API限制,我建议您talk to Twilio sales了解您的需求。

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

让我知道是否有帮助。