简体   繁体   English

在IIS 7中使用TPL-处置任务/线程/请求

[英]Using TPL in IIS 7 - Disposing Tasks/Threads/Requests

Forgive the title, I am not quite sure how to word what I am looking for, or if I even know what I am looking for. 原谅标题,我不太确定该如何表达所要查找的内容,或者甚至不知道所要查找的内容。

I have a service that is running that takes a potentially large amount of requests concurrently. 我有一个正在运行的服务,该服务同时需要大量的请求。 Additionally, within the service, I must make a certain amount (usually 10-15) requests to external services, which I am running in parallel.This code looks like the following 此外,在服务中,我必须对并行运行的外部服务发出一定数量的请求(通常为10-15),此代码如下所示

        Task<IResponse>[] tasks = new Task<IResponse>[Adapters.Count];
        for(int i = 0; i < Adapters.Count;i++)
        {
            IAdapter adapter = Adapters[i];

            Func<IResponse> makeExternalHttpRequest= () => adapter.MakeExternalHttpRequest(element, mappings);
            tasks[i] = Task.Factory.StartNew<IResponse>(() =>
                {
                    try
                    {
                        var result = makeExternalHttpRequest();
                        if (!token.IsCancellationRequested)
                        {
                            return result;
                        }
                    }catch(Exception exception)
                    {

                    }

                    return null;

                }, token);
        };
        var timeout = ...some timeout value
        Task.WaitAll(tasks,timeout, token);
        tokenSource.Cancel();

        for (int i = 0; i < tasks.Length; i++)
        {
            if (tasks[i].Result != null)
            {
                if (tasks[i].IsCompleted)
                {
                    results.Add(tasks[i].Result);
                }
            }
        }

Everything seems to be working as I expected for the past 6-7 months, and then yesterday, the server went down and I was sent the following from our system admin. 在过去的6到7个月中,一切似乎都按照我的预期进行,然后昨天,服务器出现故障,系统管理员向我发送了以下消息。

在此处输入图片说明

If you notice the highlighted area, it is quite large for the time elapsed. 如果您注意到突出显示的区域,则对于经过的时间来说,它是很大的。

I am guessing this contributed to why the server went down, but we are still looking into what happened. 我猜这是造成服务器故障的原因,但我们仍在调查发生了什么。

Any idea on what is going on, and what my next steps should be? 对发生的事情以及下一步应该做什么有任何想法吗?

I really think your next steps should be to gather more information. 我真的认为您的下一步应该是收集更多信息。 The way the code is written, your request is going to return right away after the timeout expires (or when all tasks have completed, whichever comes first). 编写代码的方式是,超时到期后(或所有任务完成后,以先到者为准),您的请求将立即返回。 The other tasks are going to run to completion on their own, in the background, and assuming that the makeExternalHttpRequest() method times out eventually, they will all spin down in their own time. 其他任务将在后台自行完成,并假设makeExternalHttpRequest()方法最终超时,它们将在自己的时间内减速。

Your cancellation token is not doing anything useful, as the code is going to block on the line above it. 您的取消令牌没有做任何有用的事情,因为代码将在其上方的行中阻塞。

What I would do is figure out which of the external requests is taking the longest, and maybe log a warning when that happens. 我要做的是找出哪个外部请求花费的时间最长,并且可能在发生这种情况时记录一条警告。 Then at least you will have better information as to what is happening. 然后,至少您将对正在发生的事情有更好的了解。 See this pesudo-code: 看到这个伪代码:

(somewhere up above, create a logger and a System.Diagnostics.Stopwatch (在上方的某个位置,创建一个记录器和一个System.Diagnostics.Stopwatch

              try
                {
                    var result = makeExternalHttpRequest();
                    if(stopwatch.Elapsed > SomeUnreasonableTimespan) 
                          logger.Warn("Task exceeded reasonable execution period." + TaskData);

                    if (!token.IsCancellationRequested)
                    {
                        return result;
                    }
                }catch(Exception exception)
                {

                }

                return null;

As a general comment, I find this piece of information most useful: 作为一般性评论,我发现以下信息最有用:

Everything seems to be working as I expected for the past 6-7 months, and then yesterday, the server went down and I was sent the following from our system admin. 在过去的6到7个月中,一切似乎都按照我的预期进行,然后昨天,服务器出现故障,系统管理员向我发送了以下消息。

This means that the problem is probably not related to your code, but related to something else. 这意味着问题可能与您的代码无关,而与其他问题有关。 When I troubleshoot, I tend to ignore things that haven't changed and focus on things that have. 当我进行故障排除时,我倾向于忽略那些没有改变的事情,而将注意力集中在那些已经改变的事情上。 Servers are going to crash from time to time. 服务器将不时崩溃。 The thing you need to worry about is are they meeting your requirements most of the time? 您需要担心的是,它们大多数时候都满足您的要求吗? If so, then I wouldn't worry about this code too much. 如果是这样,那么我将不必担心此代码。

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

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