简体   繁体   English

如何限制ASP.NET WebAPI中的最大并发连接数/请求/会话数

[英]How to limit maximum concurrent connections / requests / sessions in ASP.NET WebAPI

I'm looking for a mechanism to limit the number of concurrent connections to a service exposed using ASP.NET WebAPI. 我正在寻找一种机制来限制与使用ASP.NET WebAPI公开的服务的并发连接数。

Why? 为什么? Because this service is performing operations that are expensive on the hardware resources and I would like to prevent degradation under stress. 因为此服务执行的操作在硬件资源上很昂贵,所以我想防止在压力下性能下降。

More info: 更多信息:

  • I don't know how many requests will be issued per period of time. 我不知道每个时间段会发出多少个请求。
  • This service runs in its own IIS application pool and limiting the maximum connections on the parent site in IIS is not an option. 此服务在其自己的IIS应用程序池中运行,并且不能限制IIS中父站点上的最大连接数。
  • I found this suite, but the supported algorithms do not include the one that I'm interested in. 我找到了这个套件,但是受支持的算法不包含我感兴趣的算法。

I'm looking for something out of the box (something as straightforward as an IIS config setting) but I could not find exactly what I need. 我正在寻找一些开箱即用的东西(就像IIS配置设置一样简单),但是我找不到我真正需要的东西。

Any clues? 有什么线索吗?

Thanks! 谢谢!

Scaling your service would probably be a better idea than limiting the number of requests. 扩展服务可能比限制请求数量更好。 You could send the heavy processing to some background jobs and keep your API servicing requests. 您可以将繁重的处理发送给某些后台作业,并保留您的API服务请求。

But assuming the above cannot be done, you will need to use one of the throttling package available or write your own if none meets your requirements. 但是,假设以上操作无法完成,您将需要使用一个可用的限制程序包,或者如果没有一个满足您的要求,请编写您自己的限制程序包。

I suggest starting with the ThrottlingHandler from WebApiContrib You might be able to meet your needs by properly implementing the GetUserIdentifier method. 我建议从WebApiContrib的ThrottlingHandler开始,通过正确实现GetUserIdentifier方法,也许可以满足您的需求。

If not, you will need to implement your own MessageHandler and the handler mentioned would be a good starting point. 如果没有,您将需要实现自己的MessageHandler,并且提到的处理程序将是一个很好的起点。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM