Thread pooling for web connections

Source: Internet
Author: User

[Main reference: http://msdn.microsoft.com/msdnmag/issues/04/12/netmatters/]

In this case, the client app uses httpwebrequest to send a service request to the server. In. net 1. in X, if you have too many concurrent HTTP webrequests, you will find that. net begins to throw such an exception: there were not enough free threads in the threadpool object to complete the operation. why?

The answer is always found in the source code. If you look at the implementation in the Shared Source CLI (by the way, we recommend that you download an sscli ,. net is not open-source, but many implementations in sscli are faithfully reflected. net), you will find that, in fact. x. in net framework, httpwebrequest is actually asynchronous:

 

Public override webresponse getresponse ()

{
....
Iasyncresult asyncresult = begingetresponse (null, null );
....
Return endgetresponse (asyncresult );
}

 

Begingetresponse is actually a work item to the queue in the thread pool (friends who have used the thread pool should be familiar with the queueuserworkitem API ). Why does this cause exceptions?

Deadlock (deadlock ).

Let's assume that there is only one thread in the thread pool. When you call getresponse for the first time, the only thread in the thread pool is used. getresponse calls begingetresponse queue and sends a work item to send the request, then, call endgetresponse to wait for the result to be returned. Unfortunately, begingetresponse is always executed, because the only thread has been used by getresponse, And it (endgetresponse) is waiting for the begingetresponse to return, deadlock.

In reality, the thread pool obviously cannot have only one thread. However, this situation will still happen. For example, if there are 10 threads in the thread pool and you enter 10 work items in a row, a deadlock will occur.

In 1.x, the. NET privilege is to throw an exception. At the end of begingetresponse, before the queue user work item, the program will check system. net. Connection. isthreadpoollow. If it is true, it will run out of invalidopeartionexception.

However, if you look at the above implementation of sscli, you will find that begingetresponse/endgetresponse is not used in getresponse at all (haha, in fact, I started watching it at the beginning. net source code, the results found that there is no begingetresponse/endgetresponse at all, so I suspect that the original author is not in a mess, and later I see the article later to understand), because in. in NET 2.0, getresponse is actually using synchronous call, so the deadlock problem does not exist.

In 1.x. net, how do we manually intervene to prevent deadlocks.

The most obvious answer is to use semaphore (I will not talk about the concept of semaphore, but this is mandatory for the university operating system course ).

But what's worse ,. net 1. X does not even connect to semaphore ...... (Khan, I realized that I had read Jeff prosise's programming Microsoft a long time ago. net core reference didn't find that the word semaphore was not mentioned in the multithreading chapter ).

You can only use wrap for Win32 APIs:

 

public sealed class Semaphore : WaitHandle{    public Semaphore() : this(1, 1) {}    public Semaphore(int initialCount, int maximumCount)    {        if (initialCount < 0 || initialCount > maximumCount)            throw new ArgumentOutOfRangeException("initialCount");        if (maximumCount < 1)            throw new ArgumentOutOfRangeException("maximumCount");        IntPtr h = CreateSemaphore(            IntPtr.Zero, initialCount, maximumCount, null);        if (h == WaitHandle.InvalidHandle || h == IntPtr.Zero)            throw new Win32Exception();        Handle = h;    }    public void ReleaseOne()    {        int previousCount;        if (!ReleaseSemaphore(Handle, 1, out previousCount))            throw new Win32Exception();    }    [DllImport("kernel32.dll", SetLastError=true)]    private static extern IntPtr CreateSemaphore(        IntPtr lpSemaphoreAttributes, int lInitialCount,        int lMaximumCount, string lpName);    [DllImport("kernel32.dll", SetLastError=true)]    private static extern bool ReleaseSemaphore(        IntPtr hSemaphore, int lReleaseCount, out int lpPreviousCount);}

 

The code for how to use semaphore to control the number of concurrent threads will not be pasted. The typical producer/consumer problem, or you can refer to the code snippet in the original article.

========================================================== ======================================

Some of my comments:

Web Connection pooling is almost required for any large web app. In 2.0, how does. Net control the number of concurrent threads for synchronization? Is there any other variable that can be configured for Web connection?

The answer is yes. If you are interested, you can take a look at servicepoint, servicepointmanager, and system.net configuration element. Maybe I will introduce this content next time.

 

In a web application that requires high security-concern and high flexibility, I keep the following in mind:

1. sacriice performance for design simplicity.

2. sacriice functionality for your mace.

3. If a functionality is really desired and a must-have, at least provide a fallback so that user can get their result in a not intuitive but fast fashion.

Complex Design is prone to errors and more prone to security vulnerabilities. When you think that your security mechanisms are indestructible, countless hackers are starting to get started. What is the best security? I think it is safest to encrypt all sensitive data with only public key cryptography (OK, I am over-simplifying here, but I hope you get my point ).

About performance, every extra second your service takes, you lose 10% more users. I 'd rather have a simple but available and fast system, and then release alpha and beta tests. Instead of designing a bunch of caching mechanisms, XSLT translation, throttling, and blahblah, it looks pretty good, in fact, it is too slow to be tolerated. Later, it was very costly to modify the bloated system.

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.