-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
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
Refine PolicyWrap configuration syntax to take parameters as interfac… #302
Conversation
@ExtRemo75, |
|
@@ -21,12 +21,12 @@ internal PolicyWrap(Action<Action<Context, CancellationToken>, Context, Cancella | |||
/// <typeparam name="TResult">The return type of delegates which may be executed through the policy.</typeparam> | |||
public partial class PolicyWrap<TResult> : Policy<TResult>, IPolicyWrap<TResult> | |||
{ | |||
internal PolicyWrap(Func<Func<Context, CancellationToken, TResult>, Context, CancellationToken, TResult> policyAction, Policy outer, IsPolicy inner) | |||
internal PolicyWrap(Func<Func<Context, CancellationToken, TResult>, Context, CancellationToken, TResult> policyAction, ISyncPolicy outer, ISyncPolicy<TResult> inner) | |||
: base(policyAction, outer.ExceptionPredicates, PredicateHelper<TResult>.EmptyResultPredicates) |
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.
Shouldn't both of these be ISyncPolicy?
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.
Just like in PolicyWrapAsync.cs, there should be 3 variants:
1: ..., ISyncPolicy outer, ISyncPolicy<TResult> inner)
2: ..., ISyncPolicy<TResult> outer, ISyncPolicy inner)
3: ..., ISyncPolicy<TResult> outer, ISyncPolicy<TResult> inner)
As you mentioned in your next review-comment below, I accidentally forgot to split up 2 and 3, I have pushed the changes to commit b3cc1a7.
src/Polly.Shared/Wrap/PolicyWrap.cs
Outdated
: base(policyAction, outer.ExceptionPredicates, PredicateHelper<TResult>.EmptyResultPredicates) | ||
{ | ||
} | ||
|
||
internal PolicyWrap(Func<Func<Context, CancellationToken, TResult>, Context, CancellationToken, TResult> policyAction, Policy<TResult> outer, IsPolicy inner) | ||
internal PolicyWrap(Func<Func<Context, CancellationToken, TResult>, Context, CancellationToken, TResult> policyAction, ISyncPolicy<TResult> outer, IsPolicy inner) |
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.
Why was the 3rd param left as type IsPolicy? Not consistent with your other changes
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.
Good observation, thanks!
Just like in PolicyWrapAsync.cs, there should be 3 variants:
1: ..., ISyncPolicy outer, ISyncPolicy<TResult> inner)
2: ..., ISyncPolicy<TResult> outer, ISyncPolicy inner)
3: ..., ISyncPolicy<TResult> outer, ISyncPolicy<TResult> inner)
I will split up the IsPolicy into 2 and 3, which also aligns it with the changes made to PolicyWrapAsync.cs.
I have pushed the changes to commit b3cc1a7.
{ | ||
switch (policies.Count()) | ||
{ | ||
case 0: | ||
case 1: | ||
throw new ArgumentException("The enumerable of policies to form the wrap must contain at least two policies.", nameof(policies)); | ||
default: | ||
IEnumerable<Policy> remainder = policies.Skip(1); | ||
return policies.First().Wrap(remainder.Count() == 1 ? remainder.Single() : Wrap(remainder.ToArray())); | ||
ISyncPolicy[] remainder = policies.Skip(1).ToArray(); |
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.
Why add the overhead of creating an array using ToArray()? Use IEnumerable<ISyncPolicy>
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.
In the old code, the enumerable already was converted into an array when passing it as argument to the policies.First().Wrap(...) method. All I did was move the ToArray to an earlier moment, to prevent both the Count() and the ToArray() to navigate through the enumerable. It should result to less overhead rather than more.
If we want to remove the ToArray, I reckon these type of performance improvements should be part of a separate pull request?
{ | ||
switch (policies.Count()) | ||
{ | ||
case 0: | ||
case 1: | ||
throw new ArgumentException("The enumerable of policies to form the wrap must contain at least two policies.", nameof(policies)); | ||
default: | ||
IEnumerable<Policy> remainder = policies.Skip(1); | ||
return policies.First().WrapAsync(remainder.Count() == 1 ? remainder.Single() : WrapAsync(remainder.ToArray())); | ||
IAsyncPolicy[] remainder = policies.Skip(1).ToArray(); |
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.
Same comment as above about ToArray()
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.
See above
{ | ||
switch (policies.Count()) | ||
{ | ||
case 0: | ||
case 1: | ||
throw new ArgumentException("The enumerable of policies to form the wrap must contain at least two policies.", nameof(policies)); | ||
default: | ||
IEnumerable<Policy<TResult>> remainder = policies.Skip(1); | ||
return policies.First().WrapAsync(remainder.Count() == 1 ? remainder.Single() : WrapAsync(remainder.ToArray())); | ||
IAsyncPolicy<TResult>[] remainder = policies.Skip(1).ToArray(); |
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.
Same comment as above about ToArray()
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.
See above
@reisenberger Not sure why the build fails, looks like a generic build error, rather than related to my changes? |
Huge thank you, @ExtRemo75 , for your contribution, and thank you @udlose, @ExtRemo75 , both, for your close attention to this code! Yes, that corner of the (Apologies that vacation followed by tight commitments have kept me from these PRs just lately ...) |
Cross-ref build issue |
@ExtRemo75 Huge thanks for this contribution (just reviewed again) I am going to pend this PR for the moment, as I am also exploring #281 as part of v6, which may make the sync/async split redundant. (Thanks also for the attention to the PolicyWrap config code!) |
Closing in favour of #370 , which already merged to the v560dev branch, because this (#302) also included parts of #299 (which not ready to merge/still under discussion). Credit added to @rjongeneelen in the readme, for doing much of the refinement on #370 also addressed some of the useful comments on further cleanups for PolicyWrap syntax arising in discussion from @udlose and @rjongeneelen , thanks y'all! |
…es #297