实现提供了完整的弹幕功能,包括多行显示、不同颜色、头像支持、防重叠、椭圆形边框、透明度渐变、点击事件、字体样式设置、暂停/继续功能、过滤功能等。还包含了批处理队列处理逻辑、速率限制、错误处理和性能监控等功能。
This commit is contained in:
45
YwxAppWpfDanMu/Utils/RateLimiter.cs
Normal file
45
YwxAppWpfDanMu/Utils/RateLimiter.cs
Normal file
@@ -0,0 +1,45 @@
|
||||
using System;
|
||||
using System.Diagnostics;
|
||||
|
||||
namespace YwxAppWpfDanMu.Utils
|
||||
{
|
||||
public class RateLimiter
|
||||
{
|
||||
private readonly int _maxActionsPerSecond;
|
||||
private readonly Stopwatch _stopwatch;
|
||||
private int _actionCount;
|
||||
private long _lastResetTicks;
|
||||
|
||||
public RateLimiter(int maxActionsPerSecond)
|
||||
{
|
||||
if (maxActionsPerSecond <= 0)
|
||||
throw new ArgumentOutOfRangeException(nameof(maxActionsPerSecond));
|
||||
|
||||
_maxActionsPerSecond = maxActionsPerSecond;
|
||||
_stopwatch = Stopwatch.StartNew();
|
||||
_lastResetTicks = _stopwatch.ElapsedTicks;
|
||||
}
|
||||
|
||||
public bool TryAcquire()
|
||||
{
|
||||
long elapsedTicks = _stopwatch.ElapsedTicks;
|
||||
long ticksPerSecond = Stopwatch.Frequency;
|
||||
|
||||
// 检查是否需要重置计数器
|
||||
if (elapsedTicks - _lastResetTicks >= ticksPerSecond)
|
||||
{
|
||||
_actionCount = 0;
|
||||
_lastResetTicks = elapsedTicks;
|
||||
}
|
||||
|
||||
// 检查是否超过限制
|
||||
if (_actionCount >= _maxActionsPerSecond)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
_actionCount++;
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user