gpt4 book ai didi

c# - Interlocked.Increment 字典值

转载 作者:太空狗 更新时间:2023-10-29 22:15:49 24 4
gpt4 key购买 nike

我正在尝试使用字典来记录 Web 服务上每个 API 路径的当前请求计数,并增加和减少当前计数我认为使用 Interlocked.Increment() 因为它增加了计数并同时读取了计数。

但是下面的代码给出了一个错误,说 ref argument is not classified as a variable,我猜这是因为 dict[key] 不是一个变量?

var dict = new Dictionary<string, int>();
dict.Add("a", 1);
int i = Interlocked.Increment(ref dict["a"]);

我知道 Interlocked.Increment() 不能应用于属性,但没想到通过键访问字典会遇到同样的问题。

解决这个问题的最佳方法是什么?

编辑:这里有一些关于此的更多详细信息。

我正在尝试编写一些代码来限制 Web 服务的每个 API 路径上的 API 调用数量,所以我有两个字典,一个是指定每个 API 路径上允许多少并发调用者的策略字典,另一个是第二个计数器字典记录每个 API 路径上当前有多少调用者处于事件状态。

在 Web 服务执行任何传入请求之前,它会检查上述两个字典来决定请求是应该继续还是直接返回 HTTP 429(请求过多)响应。

这是代码的摘录,它首先检查是否有匹配的策略,如果有,然后检查是否违反了允许的最大请求数。

public override async Task Invoke(IOwinContext context)
{
var url = context.Request.Path.ToString();
var policy = _policies.FirstOrDefault(x => x.EndpointPath == url);

if (policy != null)
{
try
{
if (Interlocked.Increment(ref _currentRequests[policy]) > policy.MaxConcurrentConnection)
{
context.Response.StatusCode = 429;
var message = string.Format(
"Max API concurrent calls quota exceeded, please try again later. Maximum admitted: {0}",
policy.MaxConcurrentConnection);
context.Response.Write(message);
context.Response.ReasonPhrase = "Too Many Requests";
}
else
{
await Next.Invoke(context);
}
}
finally
{
Interlocked.Decrement(ref _currentRequests[policy]);
}
}
else
{
await Next.Invoke(context);
}
}

最佳答案

在字典中存储一个可变堆对象:

ConcurrentDictionary<..., StrongBox<int>> dict = ...;
Interlocked.Increment(ref dict[...].Value);

StrongBox.Value 是一个可变字段。

关于c# - Interlocked.Increment 字典值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33775875/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com