How to limit data transfer rate using HttpHandler

I am programming a file transfer handler with a rate limiting function whose speed depends on the user level. How to manage / calculate the baud rate in HttpHandler ?.

Some asp.net resources tell me that using Thread.Sleep blocks the asp.net thread pool.

+1


source to share


2 answers


It is generally a bad idea to "Sleep any thread" from ASP.NET because these threads might otherwise be used to serve requests from the pool. If it were said 10 threads in the pool, sleeping 10 threads that would handle downloads would cause all other requests to pile up in the queue until the download is complete.

You are probably best served by creating an IHttpAsyncHandler instead of an IHttpHandler, as stated in:



http://msdn.microsoft.com/en-us/library/ms227433.aspx

You can use a timer to periodically pump x bytes of data to the client (but be sure to periodically pool for a closed connection using IsClientConnected or some).

+2


source


You can try using timers and a timer callback for this. The idea would be to have a timer (or maybe two) that starts when your handler can run and for how long. Every time the "go" timer expires, it starts a thread that writes your data back until the "stop" timer expires (or the same timer expires again), then that thread ends what it is doing, whether the household is doing the next thread, resets the "go" timer and exits. Your main threads set up an initial timer, data to transmit, then call the timer and exits. Presumably, you will need to keep the response handle somewhere so you can access it again. By changing the length of time that the handler should wait / execute,you can control how many resources it uses.



0


source







All Articles