如何动态设置 MassTransit 的速率限制?

huangapple go评论66阅读模式
英文:

How to set MassTransit rate limit dynamically?

问题

The translation of the code part you provided is as follows:

如何使用IPipeRouter动态设置速率限制

目前,我使用以下的UseRateLimit配置方法。但我需要动态更改速率限制。

cfg.ReceiveEndpoint("SomeEvent", x =>
{
    x.UseRateLimit(1, TimeSpan.FromSeconds(1));

    x.ConfigureConsumer<SomeEventConsumer>(context);
});

我看到UseRateLimit方法有一个类型为IPipeRouter的router参数。参数的解释是:

用于动态调整速率限制的控制管道。

但我找不到关于它的示例。

英文:

How can use the IPipeRouter to dynamically set the rate limit

Currently, I use UseRateLimit configuration method as following. But I need to change the rate limit dynamically.

                cfg.ReceiveEndpoint(&quot;SomeEvent&quot;, x =&gt;
                {
                    x.UseRateLimit(1, TimeSpan.FromSeconds(1));

                    x.ConfigureConsumer&lt;SomeEventConsumer&gt;(context);
                }); 

I see that UseRateLimit method has router parameter with type IPipeRouter.The explanation of the parameter is

> The control pipe used to adjust the rate limit dynamically.
>

But I couldn't find an example on it.

答案1

得分: 0

没有记录/支持的动态调整速率限制的方式。

有一些传统的方法允许进行此类更改,但它们的使用相对复杂(这也是为什么不支持的原因)。

var router = new PipeRouter();

cfg.UseRateLimit(100, TimeSpan.FromSeconds(1), router);

保存该管道路由器由您来决定,但在代码的其他地方,您可以调用:

await router.SetRateLimit(10);

然后可以根据您的需要进行上下调整。

再次强调,这并不受支持,但可能适用于您的情况。

英文:

There is no documented/supported way to dynamically adjust the rate limit.

There are legacy methods that allowed such a change, but the use of them is complicated (and thus why it isn't supported).

var router = new PipeRouter();

cfg.UseRateLimit(100, TimeSpan.FromSeconds(1), router);

Saving that pipe router is up to you, but somewhere else in the code, you could call:

await router.SetRateLimit(10);

And it can be adjusted up or down to suit your purpose.

> Again, this is not supported but could work for you.

huangapple
  • 本文由 发表于 2023年5月26日 16:36:30
  • 转载请务必保留本文链接:https://go.coder-hub.com/76339082.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定