简体   繁体   English

如何限制Web API中的并发请求数?

[英]How to limit the number of concurrent requests in web api?

How to limit by code the number of concurrent requests to web application, say to 3 requests ? 如何通过代码限制对Web应用程序的并发请求数,例如3个请求? Am I suppose to put each servlet class into a thread and create a global counter (by creating new class)? 我是否想将每个servlet类放入一个线程中并创建一个全局计数器(通过创建新类)?

How to limit by code the number of concurrent requests to web application, say to 3 requests ? 如何通过代码限制对Web应用程序的并发请求数,例如3个请求? Am I suppose to put each servlet class into a thread and create a global counter (by creating new class)? 我是否想将每个servlet类放入一个线程中并创建一个全局计数器(通过创建新类)?

You typically rely in the web container to limit the number of concurrent requests; 通常,您依靠Web容器来限制并发请求的数量。 eg by setting limit on the number of worker threads or connections ... in the web container configurations. 例如,通过在Web容器配置中设置工作线程或连接数的限制...。

Apparently, if a Tomcat server gets more requests than it can handle, it will send generic 503 responses. 显然,如果Tomcat服务器收到的请求超出其处理能力,它将发送通用503响应。 For more information: 欲获得更多信息:

But how can i display to the users that the wed reached its limit (like 3 requests)? 但是我如何向用户显示结婚已达到其限制(例如3个请求)?

If you want to limit specific request types and display specific responses to the user, then you probably will need to implement this within each servlet, using a counter, etcetera. 如果要限制特定的请求类型并向用户显示特定的响应,则可能需要使用计数器等在每个Servlet中实现此功能。

But the problem with trying to do "nice" things when the server is overloaded is that doing the nice things tends to increase the load. 但是,当服务器超负荷时尝试做“好”的事情的问题是,做一件好事往往会增加负载。 This is particularly important: 这尤其重要:

  • when your server is grossly inadequate for the actual load from (real) users, or 当您的服务器严重不足以(实际)用户实际加载时,或
  • when someone is DoS'ing you, either accidentally or deliberately. 当某人无意或故意地对您进行DoS攻击时。

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

相关问题 面试任务:使用令牌限制并发请求数 - Interview task: Limit number of concurrent requests with tokens 如何使用 Mono & Flux 限制并发 http 请求 - How to limit concurrent http requests with Mono & Flux 如何增加 Spring WebClient 并发请求限制? - How to increase Spring WebClient concurrent requests limit? 如何设置servlet中并发请求数的限制? - How to set limit to the number of concurrent request in servlet? Spring boot限制控制器中特定API的并发调用次数 - Spring boot Limit number of concurrent invocations of a specific API in a controller 如何一次限制所有并发方法的线程数 - How to limit number of threads all concurrent methods at once 集群式无状态Web应用程序-如何处理并发请求? - Clustered, Stateless web application - how to handle concurrent requests? Hibernate中并发事务的最大数量是否有限制? - Is there a limit for maximum number of concurrent transactions in Hibernate? Tomcat:如何限制整个应用程序中的线程数(不是请求)? - Tomcat: how to limit number of threads (not requests) in the entire application? 如何限制每秒的Web请求以避免垃圾邮件和拒绝服务 - How to Limit web requests per second to avoid spamming and denial of service
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM