6.67RateLimitedStream Rate-Limited Stream
On SaaS/PaaS application platforms, users are often billed based on resource usage (such as bandwidth and traffic). In particular, when users download or upload resources, the platform applies rate limiting. In such cases, the RateLimitedStream provided by the framework is very useful. This stream can adjust read/write speed according to a configured rate limit, making it ideal for resource control.
Using RateLimitedStream is very simple — you only need to pass the Stream object to be rate-controlled and the maximum allowed bytes per second (bytesPerSecond) through the constructor. For example:
var stream = await httpRemoteService.GetAsStreamAsync("https://furion.net/", HttpCompletionOption.ResponseHeadersRead);// Wrap the stream with RateLimitedStream and return a new stream; for subsequent operations, simply use rateLimitedStream instead of streamvar rateLimitedStream = new RateLimitedStream(stream, 1024 * 1024 * 1); // Limit the maximum read/write speed to 1MB/s// At this point, read/write operations on rateLimitedStream will be kept within 1MB/s. ✅Overall, RateLimitedStream is a powerful stream wrapper that helps developers better manage resource usage on SaaS/PaaS platforms, ensuring user operations comply with billing conditions while improving system stability and performance.