-
-
Notifications
You must be signed in to change notification settings - Fork 362
feat(ThrottleDispatcher): refine code improve readability #6379
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
Reviewer's GuideRefines the ThrottleDispatcher implementation by removing redundant synchronization fields, unifying and simplifying throttling logic with async/await and timestamp updates, streamlining the synchronous Throttle method’s flow, and updating unit tests to align with the new behavior. Class diagram for updated ThrottleDispatcherclassDiagram
class ThrottleDispatcher {
- DateTime? _invokeTime
+ ThrottleDispatcher(ThrottleOptions options)
+ bool ShouldWait()
+ void Throttle(Action action, CancellationToken token = default)
- async Task InternalThrottleAsync(Func<Task> function, CancellationToken cancellationToken = default)
}
class ThrottleOptions {
+ TimeSpan Interval
+ bool DelayAfterExecution
+ bool ResetIntervalOnException
}
ThrottleDispatcher --> ThrottleOptions
File-Level Changes
Assessment against linked issues
Possibly linked issues
Tips and commandsInteracting with Sourcery
Customizing Your ExperienceAccess your dashboard to:
Getting Help
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hey @ArgoZhang - I've reviewed your changes and they look great!
Prompt for AI Agents
Please address the comments from this code review:
## Individual Comments
### Comment 1
<location> `src/BootstrapBlazor/Services/ThrottleDispatcher.cs:19` </location>
<code_context>
/// </summary>
/// <returns></returns>
- protected virtual bool ShouldWait() => _busy || _invokeTime.HasValue && (DateTime.UtcNow - _invokeTime.Value) < options.Interval;
+ protected virtual bool ShouldWait() => _invokeTime.HasValue && (DateTime.UtcNow - _invokeTime.Value) < options.Interval;
/// <summary>
</code_context>
<issue_to_address>
Removal of the _busy flag may introduce concurrency issues.
Without the _busy flag and lock, concurrent calls may bypass throttling. If thread safety is required, add appropriate synchronization to avoid race conditions.
</issue_to_address>
### Comment 2
<location> `src/BootstrapBlazor/Services/ThrottleDispatcher.cs:63` </location>
<code_context>
}
- lock (_locker)
+ _invokeTime = DateTime.UtcNow;
+
+ try
{
- if (ShouldWait())
+ await function();
+ if (options.DelayAfterExecution)
{
- return LastTask;
+ _invokeTime = DateTime.UtcNow;
}
-
</code_context>
<issue_to_address>
Setting _invokeTime before and after function execution may not be necessary.
Clarify whether throttling should be based on the start or end of execution, and update _invokeTime in only one place to reflect that.
</issue_to_address>
### Comment 3
<location> `test/UnitTest/Services/ThrottleTest.cs:91` </location>
<code_context>
+ Assert.Equal(1, count);
- Assert.ThrowsAny<InvalidOperationException>(() => dispatcher.Throttle(() => throw new InvalidOperationException()));
+ dispatcher.Throttle(() => throw new InvalidOperationException());
// 发生错误后可以立即执行下一次任务,不限流
</code_context>
<issue_to_address>
Missing assertion for expected exception in sync Throttle after async exception.
Please reintroduce an assertion (e.g., Assert.Throws) to verify that the sync Throttle call throws as expected, or clarify if this change is intentional.
</issue_to_address>
<suggested_fix>
<<<<<<< SEARCH
dispatcher.Throttle(() => throw new InvalidOperationException());
// 发生错误后可以立即执行下一次任务,不限流
=======
Assert.ThrowsAny<InvalidOperationException>(() => dispatcher.Throttle(() => throw new InvalidOperationException()));
// 发生错误后可以立即执行下一次任务,不限流
>>>>>>> REPLACE
</suggested_fix>
### Comment 4
<location> `test/UnitTest/Services/ThrottleTest.cs:120` </location>
<code_context>
cts = new CancellationTokenSource(100);
- await Assert.ThrowsAnyAsync<OperationCanceledException>(() => dispatcher.ThrottleAsync(async () =>
+ await dispatcher.ThrottleAsync(async () =>
{
await Task.Delay(300);
- }, cts.Token));
+ }, cts.Token);
}
</code_context>
<issue_to_address>
Missing assertion for OperationCanceledException in ThrottleAsync with cancellation.
Restoring the assertion ensures that cancellation is properly handled and exceptions are surfaced as expected.
</issue_to_address>Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.
Link issues
fixes #6378
Summary By Copilot
Regression?
Risk
Verification
Packaging changes reviewed?
☑️ Self Check before Merge
Summary by Sourcery
Refine ThrottleDispatcher to simplify its implementation and improve readability by removing unnecessary locks and flags, unify sync/async paths with async/await, and update tests to align with corrected reset-on-exception and cancellation behaviors
Bug Fixes:
Enhancements:
Tests: