1

我正在尝试使用 Scala Spray-client 库请求持久的 HTTP 连接,这样客户端必须建立一次连接,然后它可以在同一个连接上进行多次调用。

我无法在 Spray 文档页面上找到任何相关信息

4

1 回答 1

1

我认为默认情况下,喷雾客户端将尽可能重用连接。

默认情况下,60 秒内未使用的连接将被客户端终止。

喷雾的参考配置:

# The time after which an idle connection will be automatically closed.
# Set to `infinite` to completely disable idle timeouts.
spray.client.idle-timeout = infinite

如果我们禁用空闲超时,我们的连接将永远保持活动状态,除非远程服务器设置为在超时后终止空闲连接(可能是这样,但喷雾客户端应该确保我们总是可以获得新连接,遵守下面的限制)。

我们可以将主机连接器池的大小限制为 1 以获得所需的行为(每个主机一个连接):

# The maximum number of parallel connections that an `HttpHostConnector`
# is allowed to establish to a host. Must be greater than zero.
spray.can.host-connector.max-connections = 1

这意味着如果我们在第一个请求收到响应之前发送第二个请求,则在第一个请求完成之前不会发送第二个请求。要在单个连接上发送多个请求而不等待第一个请求完成,我们可以启用流水线

# If this setting is enabled, the `HttpHostConnector` pipelines requests
# across connections, otherwise only one single request can be "open"
# on a particular HTTP connection.
spray.can.host-connector.pipelining = off
于 2015-12-03T13:19:27.273 回答