设置喷雾连接限制

Setting spray connection limits

我们目前正在将 tomcat 应用程序移植到 spray/scala 应用程序。

我们的旧应用程序有这样的设置:

<Connector port="8082" protocol="HTTP/1.1"
        maxThreads="1000"
        maxConnections="10000"
        processorCache="500"
        connectionTimeout="20000"
        URIEncoding="UTF-8"
        redirectPort="8443" />

在新的应用程序中,我们想在 spray 中设置相当于 maxThreads/maxConnections 的值。

我看到了喷雾 reference.conf 文件(如下),想知道这些设置是否确实是要更改的正确设置?
或者我应该配置执行 runRoute 的 actor?
还是两者都有?

我确实发现低 "max-connections = 4" 默认值有点奇怪,如果那确实是服务器连接限制。

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

# The maximum number of times an `HttpHostConnector` attempts to repeat
# failed requests (if the request can be safely retried) before
# giving up and returning an error.
max-retries = 5

# Configures redirection following.
# If set to zero redirection responses will not be followed, i.e. they'll be returned to the user as is.
# If set to a value > zero redirection responses will be followed up to the given number of times.
# If the redirection chain is longer than the configured value the first redirection response that is
# is not followed anymore is returned to the user as is.
max-redirects = 0

# If this setting is enabled, the `HttpHostConnector` pipelines requests
# across connections, otherwise only one single request can be "open"
# on a particular HTTP connection.
pipelining = off

# The time after which an idle `HttpHostConnector` (without open
# connections) will automatically terminate itself.
# Set to `infinite` to completely disable idle timeouts.
idle-timeout = 30 s

# Modify to tweak client settings for this host-connector only.
client = ${spray.can.client}

}

您所指的设置是 spray-client 设置,而不是 spray-server 设置。

host-connector {    max-connections = 4 }

这意味着如果您想编写一个连接到某些外部 HTTP 服务器的喷射应用程序,您将无法同时创建超过 4 个到特定主机的连接。

在 Spray 中没有直接等同于 maxThreads/maxConnections。因为 Spray 是建立在 Akka Actors 之上的。 Akka Actors 使用 Dispatchers 和 Execution Contexts 来处理消息。 Spray for Http Listener(处理 HTTP 请求的参与者)的默认调度程序是:

listener-dispatcher = "akka.actor.default-dispatcher"

您可以阅读有关默认调度程序的信息 here

在 Spray 中没有直接指定 maxThreads 的方法,因为默认的 akka 调度程序是基于 top fork-join-executor 的尽可能利用多核架构。

要指定 maxConnections,您必须修改 HttpListener 的源代码:

case x: Tcp.Bound if(maxConnections()) ⇒ //handle max connections use case