从路由创建时,流量节流不起作用

Throttling of Flow not working when created from Route

考虑包含所有 HTTP 服务的路由

val routes:Route = ...

我希望限制请求的数量,所以我使用 Route.handleFlow(routes) 来创建流程并调用具有有限持续时间的限制方法。

最后,我使用

创建了 HTTP 绑定
Http().bindAndHandle(flowObjectAfterThrottling, hostname, port)

当从循环中触发 HTTP 请求时,akka 不遵守节流。

一种可能性是 "fired from a loop" 的 http 请求可能正在使用单独的连接。每个传入连接都以适当的速率进行限制,但总吞吐量高于预期。

改用配置

您不需要编写软件来为您的 Route 设置限制速率。

如果您只关心磁盘或 RAM 等资源的消耗,那么您可以删除速率逻辑并改用 akka configuration settings

# The maximum number of concurrently accepted connections when using the
# `Http().bindAndHandle` methods.
max-connections = 1024

# The maximum number of requests that are accepted (and dispatched to
# the application) on one single connection before the first request
# has to be completed.
pipelining-limit = 16

这不提供设置最大频率的能力,但它至少允许指定最大并发使用量,这通常足以保护资源。