From 6c821e57e4b501b7963181c0964a3b5080613c85 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=99=BE=E5=B0=8F=E5=83=A7?= Date: Tue, 3 Dec 2024 12:14:44 +0800 Subject: [PATCH] =?UTF-8?q?=F0=9F=98=8A=20=E6=94=B9=E8=BF=9B=20=E5=B8=A6?= =?UTF-8?q?=E5=BA=94=E7=94=A8=E9=80=9F=E7=8E=87=E9=99=90=E5=88=B6=E7=9A=84?= =?UTF-8?q?=E6=B5=81=EF=BC=8C=E5=B0=BD=E5=8F=AF=E8=83=BD=E5=87=8F=E5=B0=91?= =?UTF-8?q?=E8=AF=AF=E5=B7=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../HttpRemote/Models/RateLimitedStream.cs | 26 ++++++++++--------- .../HttpRemote/Models/RateLimitedStream.cs | 26 ++++++++++--------- 2 files changed, 28 insertions(+), 24 deletions(-) diff --git a/framework/Furion.Pure/V5_Experience/HttpRemote/Models/RateLimitedStream.cs b/framework/Furion.Pure/V5_Experience/HttpRemote/Models/RateLimitedStream.cs index e2a6d4cd11..90523eb7b2 100644 --- a/framework/Furion.Pure/V5_Experience/HttpRemote/Models/RateLimitedStream.cs +++ b/framework/Furion.Pure/V5_Experience/HttpRemote/Models/RateLimitedStream.cs @@ -23,6 +23,8 @@ // 请访问 https://gitee.com/dotnetchina/Furion 获取更多关于 Furion 项目的许可证和版权信息。 // ------------------------------------------------------------------------ +using System.Diagnostics; + namespace Furion.HttpRemote; /// @@ -39,14 +41,14 @@ public sealed class RateLimitedStream : Stream internal readonly Stream _innerStream; /// - /// 流开始读取的时间点 + /// 用于精确计时的 实例 /// - internal readonly DateTime _startTime; + internal readonly Stopwatch _stopwatch = new(); /// - /// 到目前为止已读取的总字节数 + /// 到目前为止已读取或写入的总字节数 /// - internal long _totalBytesRead; + internal long _totalBytesProcessed; /// /// @@ -70,8 +72,8 @@ public sealed class RateLimitedStream : Stream _innerStream = innerStream; _bytesPerSecond = bytesPerSecond; - // 记录当前时间作为开始时间 - _startTime = DateTime.UtcNow; + // 启动 Stopwatch 来开始计时 + _stopwatch.Start(); } /// @@ -140,17 +142,17 @@ public sealed class RateLimitedStream : Stream /// /// 根据设定的速率限制调整读写操作的速度 /// - /// 本次操作将处理的字节数。 - internal async Task ApplyRateLimitAsync(int bytesToRead) + /// 本次操作将处理的字节数 + internal async Task ApplyRateLimitAsync(int bytesToProcess) { // 自开始以来经过的时间(秒) - var elapsedSeconds = (DateTime.UtcNow - _startTime).TotalSeconds; + var elapsedSeconds = _stopwatch.ElapsedMilliseconds / 1000.0; // 根据速率预期应读取的字节数 var totalBytesExpected = elapsedSeconds * _bytesPerSecond; // 计算实际与预期之差 - var bytesOverLimit = _totalBytesRead + bytesToRead - totalBytesExpected; + var bytesOverLimit = _totalBytesProcessed + bytesToProcess - totalBytesExpected; if (bytesOverLimit > 0) { @@ -160,7 +162,7 @@ public sealed class RateLimitedStream : Stream await Task.Delay(delayMilliseconds).ConfigureAwait(false); } - // 更新已读取的总字节数 - _totalBytesRead += bytesToRead; + // 更新已处理的总字节数 + _totalBytesProcessed += bytesToProcess; } } \ No newline at end of file diff --git a/framework/Furion/V5_Experience/HttpRemote/Models/RateLimitedStream.cs b/framework/Furion/V5_Experience/HttpRemote/Models/RateLimitedStream.cs index e2a6d4cd11..90523eb7b2 100644 --- a/framework/Furion/V5_Experience/HttpRemote/Models/RateLimitedStream.cs +++ b/framework/Furion/V5_Experience/HttpRemote/Models/RateLimitedStream.cs @@ -23,6 +23,8 @@ // 请访问 https://gitee.com/dotnetchina/Furion 获取更多关于 Furion 项目的许可证和版权信息。 // ------------------------------------------------------------------------ +using System.Diagnostics; + namespace Furion.HttpRemote; /// @@ -39,14 +41,14 @@ public sealed class RateLimitedStream : Stream internal readonly Stream _innerStream; /// - /// 流开始读取的时间点 + /// 用于精确计时的 实例 /// - internal readonly DateTime _startTime; + internal readonly Stopwatch _stopwatch = new(); /// - /// 到目前为止已读取的总字节数 + /// 到目前为止已读取或写入的总字节数 /// - internal long _totalBytesRead; + internal long _totalBytesProcessed; /// /// @@ -70,8 +72,8 @@ public sealed class RateLimitedStream : Stream _innerStream = innerStream; _bytesPerSecond = bytesPerSecond; - // 记录当前时间作为开始时间 - _startTime = DateTime.UtcNow; + // 启动 Stopwatch 来开始计时 + _stopwatch.Start(); } /// @@ -140,17 +142,17 @@ public sealed class RateLimitedStream : Stream /// /// 根据设定的速率限制调整读写操作的速度 /// - /// 本次操作将处理的字节数。 - internal async Task ApplyRateLimitAsync(int bytesToRead) + /// 本次操作将处理的字节数 + internal async Task ApplyRateLimitAsync(int bytesToProcess) { // 自开始以来经过的时间(秒) - var elapsedSeconds = (DateTime.UtcNow - _startTime).TotalSeconds; + var elapsedSeconds = _stopwatch.ElapsedMilliseconds / 1000.0; // 根据速率预期应读取的字节数 var totalBytesExpected = elapsedSeconds * _bytesPerSecond; // 计算实际与预期之差 - var bytesOverLimit = _totalBytesRead + bytesToRead - totalBytesExpected; + var bytesOverLimit = _totalBytesProcessed + bytesToProcess - totalBytesExpected; if (bytesOverLimit > 0) { @@ -160,7 +162,7 @@ public sealed class RateLimitedStream : Stream await Task.Delay(delayMilliseconds).ConfigureAwait(false); } - // 更新已读取的总字节数 - _totalBytesRead += bytesToRead; + // 更新已处理的总字节数 + _totalBytesProcessed += bytesToProcess; } } \ No newline at end of file -- Gitee