Restricting bandwidth for download stream

H

hoenes1

Hi all,

I have an asp.net application from which registered users can download
files. What I want to implement is a bandwidth restriction for certain
users. What I DON'T want to do is restrict the bandwidth on the IIS
side. It really should be handled in the code. Here is a code snippet
showing how downloads are currently handled (standard implementation I
guess):

// fs: FileStream
while (offset < size && Response.IsClientConnected)
{
if (size-offset < bufSize)
{
bufSize = size-offset;
buf = new byte[bufSize];
}
fs.Read(buf, 0, (int)bufSize);
Response.OutputStream.Write(buf, 0, (int)bufSize);
if (Response.IsClientConnected)
Response.Flush();
else
break;
offset += bufSize;
}

everything works fine, but I can't find a way to decrease the speed of
the download (except perhaps adding Sleep() or decreasing bufsize which
would both be a *dirty* solution). Anyone has an idea?

Thanks in advance

Hannes
 
B

Bruce Barker

first be sure buffering is turned off. loop writing chunks with sleeps as
you guessed.

a bit about about flow. when the download request hits iis, it uses a thread
and pipe to talk to asp.net worker process. asp.net dedicates an io thread
to handle the pipe, and starts a worker thread to process the request.

if you add delays, all these threads will be tied up, and the number of
concurrent downloads will drop. while you can bump up the threads pools, it
is still not a very high number (200 concurrent downloads per server is
about the max).

you may find that limiting bandwidth is not very useful. you might look at
limiting the number of downloads in a given time rather than slowing down a
download.

-- bruce (sqlwork.com)
 
H

hoenes1

First, thank you for the time you spent with your answer.
Actually, the purpose of the bandwith restriction is not to disburden
the server (which has enough firepower to serve all concurrent
requests), but to restrict the bandwidth usage of certain users (e. g.
who don't pay for the service or any other restriction criteria). Is
Sleep() really the only possibility to restrict streaming bandwidth? I
can hardly imagine that. And if so, does adding Sleep() after
stream.Write() actually slow down all concurrent downloads (given that
every download has its own thread)?
Thanks again in advance for your input.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,053
Latest member
BrodieSola

Latest Threads

Top