具有长时间运行的同步请求的 ASP.NET 应用程序的注意事项
在 Windows Server 2008 64 位、IIS 7.0 和 .NET 4.0 下,如果 ASP.NET 应用程序(使用 ASP.NET 线程池、同步请求处理)长时间运行(> 30 分钟)。 Web 应用程序没有页面,主要目的是以块(~5 MB)读取大文件(> 1 GB)并将它们传输到客户端。代码:
while (reading)
{
Response.OutputStream.Write(buffer, 0, buffer.Length);
Response.Flush();
}
单一生产者 - 实施单一消费者模式,因此对于每个请求都有两个线程。我在这里不使用任务库,但请告诉我在这种情况下它是否比传统的线程创建有优势。使用 HTTP 处理程序 (.ashx) 代替 (.aspx) 页面。压力测试下CPU利用率不是问题,但对于单个工作进程,210个并发客户端后,新连接会遇到超时。由于我不使用会话状态,因此这是通过网络园艺解决的。我不确定我是否遗漏了任何大问题,但请让我知道您认为还应该考虑哪些其他因素?
例如,由于“连接超时”,IIS 可能会关闭长时间运行的 TCP 连接,因为正常的 ASP.NET 页面处理时间不到 5 分钟,因此我应该增加该值。
我很欣赏您的想法。
Under windows server 2008 64bit, IIS 7.0 and .NET 4.0 if an ASP.NET application (using ASP.NET thread pool, synchronous request processing) is long running (> 30 minutes). Web application has no page and main purpose is reading huge files ( > 1 GB) in chunks (~5 MB) and transfer them to the clients. Code:
while (reading)
{
Response.OutputStream.Write(buffer, 0, buffer.Length);
Response.Flush();
}
Single producer - single consumer pattern implemented so for each request there are two threads. I don't use task library here but please let me know if it has advantage over traditional thread creation in this scenario. HTTP Handler (.ashx) is used instead of a (.aspx) page. Under stress test CPU utilization is not a problem but with a single worker process, after 210 concurrent clients, new connections encounter time-out. This is solved by web gardening since I don't use session state. I'm not sure if there's any big issue I've missed but please let me know what other considerations should be taken in your opinion ?
for example maybe IIS closes long running TCP connections due to a "connection timeout" since normal ASP.NET pages are processed in less than 5 minutes, so I should increase the value.
I appreciate your Ideas.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
就我个人而言,我会为这种类型的处理寻找不同的机制。 HTTP 请求/Web 应用程序不是为此类事情而设计的,并且稳定性将非常困难,当您使用此类模型时,您面临许多可能导致重大问题的风险。
我会将该处理移至后端进程,这样您就可以在 asp.net 运行时之外,这样您就可以更好地控制启动/关闭等。
Personally, I would be looking at a different mechanism for this type of processing. HTTP Requests/Web Applications are NOT designed for this type of thing, and stability is going to be VERY hard, you have a number of risks that could cause you major issues as you are working with this type of model.
I would move that processing off to a backend process, so that you are OUTSIDE of the asp.net runtime, that way you have more control over start/shutdown, etc.
首先,从来没有。绝不。绝不!在线程池线程中执行任何需要超过几秒的处理。它们的数量有限,并且系统将它们用于许多用途。这是自找麻烦。
其次,虽然处理程序是一个好主意,但您对“动态生成”的含义有点模糊。您的意思是您正在动态加密文件并且此加密可能需要 30 分钟吗?或者您的意思是从数据库中提取数据并组装文件?或者下载需要 30 分钟才能下载?
编辑:
正如我所说,不要使用线程池来进行长时间运行的操作。创建您自己的线程,或者如果您使用 .NET 4,请使用任务并将其指定为长时间运行。
First, Never. NEVER. NEVER! do any processing that takes more than a few seconds in a thread pool thread. There are a limited number of them, and they're used by the system for many things. This is asking for trouble.
Second, while the handler is a good idea, you're a little vague on what you mean by "generate on the fly" Do you mean you are encrypting a file on the fly and this encryption can take 30 minutes? Or do you mean you're pulling data from a database and assembling a file? Or that the download takes 30 minutes to download?
Edit:
As I said, don't use a thread pool for anything long running. Create your own thread, or if you're using .NET 4 use a Task and specify it as long running.
长时间运行的进程不应该以这种方式实现。将其传递给您设置的服务。
如果您确实希望客户端的页面挂起,请考虑从 AJAX 与不会阻塞 IO 线程的事物(例如 node.js)进行交互。
由于线程的使用,向许多客户端推送通知不是 ASP.NET 可以处理的,因此我的node.js。如果您的负载较低,您还有其他选择。
Long running processes should not be implemented this way. Pass this off to a service that you set up.
IF you do want to have a page hang for a client, consider interfacing from AJAX to something that does not block on IO threads - like node.js.
Push notifications to many clients is not something ASP.NET can handle due to thread usage, hence my node.js. If your load is low, you have other options.
使用 Web-Gardening 可以提高应用程序的稳定性。
关闭缓存,因为您没有 aspx 页面
如果没有性能分析,很难提供更多建议。你使用VS内置并找到瓶颈。
Use Web-Gardening for more stability of your application.
Turn-off caching since you don't have aspx pages
It's hard to advise more without performance analysis. You the VS built-in and find the bottlenecks.
Web 1.0 处理长时间运行的进程的方法是在服务器上生成它们并立即返回。让衍生的服务更新数据库的进度,并且站点上的页面可以查询进度。
该技术最常见的用法是获得包裹递送。在我的包出现之前,您无法保持 HTTP 连接打开,因此它只是为您提供了一种查询进度的方法。后台进程负责协调获取物品、包装物品、将其运送到 UPS 卡车等所需的所有步骤。整个过程中,每个步骤都记录在数据库中。从概念上来说,是一样的。
根据问题编辑进行编辑:只需立即返回结果页面,并在生成的线程或进程中在服务器上生成二进制文件。使用 Ajax 检查文件是否已准备好,准备好后提供指向该文件的链接。
The Web 1.0 way of dealing with long running processes is to spawn them off on the server and return immediately. Have the spawned off service update a database with progress and pages on the site can query for progress.
The most common usage of this technique is getting a package delivery. You can't hold the HTTP connection open until my package shows up, so it just gives you a way to query for progress. The background process deals with orchestrating all of the steps it takes for getting the item, wrapping it up, getting it onto a UPS truck, etc. All along the way, each step is recorded in the database. Conceptually, it's the same.
Edit based on Question Edit: Just return a result page immediately, and generate the binary on the server in a spawned thread or process. Use Ajax to check to see if the file is ready and when it is, provide a link to it.