Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How useful is Response.IsClientConnected?

Tags:

asp.net

I was wondering if anyone had experience they could share using the Response.IsClientConnected property as a performance optimization for asp.net websites.

The reason I ask is that I am a bit skeptical on how effective it would be in real life scenarios. I understand the concept of checking the value before performing a large task but I just can't see how useful this would be as clients could disconnect at any point time.

like image 567
rifferte Avatar asked Aug 17 '10 19:08

rifferte


2 Answers

I think the main usage would be for optimizing the delivery of long processes. For example, if you had to generate a huge report or something, you might run the report in a separate thread and then periodically check to see if the user is still connnected. If not, you could kill this long running process so that it is not running needlessly since the user is no longer expecting a response.

This helps to prevent users from starting long processes and then making more requests over and over because they might think it is slow or something. If you were not doing this type of checking, you could tax your server due to all the requests even though all but one is valid. This scenario could be handled by allowing only one user to run one long running task, but it would also help in a multi-user environment as well to make sure you are only spending time serving up requests where the user is still connected and waiting for the response.

Note: I have never actually used this before, this is just based on my very basic understanding of what I have read.

like image 200
Kelsey Avatar answered Nov 14 '22 02:11

Kelsey


I have used this extensively in my applications and it can give you a huge saving on resources.

Try this: create a page that needs -some- time to complete and try refresh it many many times before it complete. You will see that requests are queued to be executed. Imagine a user that has a slow connection and refreshes his page many many times thinking this will fetch the page (a very common issue from what a site can die out of resources when all users are connected and for some reason it becomes slow).

Now, change it and at the start of each page load, (or sooner at page init) check if HttpContext.Current.Response.IsClientConnected and in the case that he is not connetced throw a threadabord exception. You will see, your site will respond much sooner.

Actually I check if client is connected before any heavy action on the page so as to prevent needless executions. In production environments, I have seen that especially in cases where the system becomes slow, this validation will help much.

like image 23
George Mavritsakis Avatar answered Nov 14 '22 02:11

George Mavritsakis