02 Apr 2007 |
| |||
Dit is een crosspost van codeproject. Zie het orgineel inclusief source code vind je op CodeProject zelf: CodeProject: Bandwidth throttling.
IntroductionHosting a website can be fun. But when you offer big downloads or streaming media you do not want a visitor to eat up the full bandwith. Or maybe you want to offer a premium account from where users can download without limitation and a free account where users cannot download faster than 50kb/s. Here is where throttling comes in. Bandwidth throttling helps provide quality of service (QoS) by limiting network congestion and server crashes. For example, you make sure a connection does not get more then an X number of bytes per second. The purpose of this article is to show how to use bandwidth throttling with a small helper class. Using the codeWithin the source code you will find a class named Stream throttledStream = new ThrottledStream(Response.OutputStream, 1024); Now everything you do with the Because the The old code can look like this: Stream sourceStream; Stream destinationStream; try { sourceStream = new FileStream(@"c:\myfile.bin", FileMode.Open, FileAccess.Read, FileShare.Read); destinationStream = new NetworkStream(mySocket, false); byte[] buffer = new byte[1024]; int readCount = sourceStream.Read(buffer, 0, BufferSize); while (readCount > 0) { destinationStream.Write(buffer, 0, readCount); readCount = sourceStream.Read(buffer, 0, BufferSize); } } finally { if (destinationStream != null) { destinationStream.Close(); } if (sourceStream != null) { sourceStream.Close(); } } Now we can easily add throttling support to this process. We only need to change the initialization: ... Stream originalDestinationStream = new NetworkStream(mySocket, false); destinationStream = new ThrottledStream(originalDestinationStream, 51200); ... By adding only one line of code this full process is throttled to 50 kb/s (51200 b/s). Now we go even a step further and will add throttling that is based on a membership: ... long bps; switch( user.Membership ) { case MembershipLevel.Bronze: bps = 51200; break; case MembershipLevel.Silver: bps = 102400; break; case MembershipLevel.Gold: bps = 153600; break; case MembershipLevel.Platina: bps = ThrottledStream.Infinite; break; } Stream originalDestinationStream = new NetworkStream(mySocket, false); destinationStream = new ThrottledStream(originalDestinationStream, bps); ... Here we have a situation where a Bronze membership will give you 50kb/s, Silver 100kb/s, Gold 150kb/s and Platina infinitive (no throttling). Points of InterestBandwidth throttling can improve the QoS of your server and allows you to control the bandwidth for a specified connection. The helper class named
Set as favorite
Bookmark
Email This
Trackback(0)
Commentaar (0)
![]() Schrijf commentaar
|
Bedankt voor je schitterende presentatie. Ik heb niets dan positieve geluiden gehoord.
Peter Criellaard
Microsoft
Eindelijk een presentatie waarmee ik ook iets mee kan in de praktijk!
Dennis Schuuring
Max BV
Bedankt voor de presentatie, het was voor mij 1 van de betere momenten van die dag!
Henri Koppen
Arcenus