Skip to content
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

Closed
wants to merge 3 commits into from

Conversation

rjongeneelen
Copy link
Contributor

…es #297

@dnfclas
Copy link

dnfclas commented Aug 25, 2017

@ExtRemo75,
Thanks for having already signed the Contribution License Agreement. Your agreement was validated by .NET Foundation. We will now review your pull request.
Thanks,
.NET Foundation Pull Request Bot

@rjongeneelen
Copy link
Contributor Author

rjongeneelen commented Aug 25, 2017

  • Used interfaces throughout PolicyWrap logic, this required splitting some methods as well whenusing IsPolicy
  • Extended ISync/IAsync interfaces with access to Predicates + made those delegates public
  • Rewrote PolicyWrap static Wrap/WrapAsync methods to stop making use of Policy.Wrap() methods, since this would require making PolicyWrap public as well, which seems unnecessary and unwanted. It also removes PolicyWrap-specific logic from the Policy class
  • Changed static Success/Failure methods of PolicyResult to public, to enable instantiation from a derived policy

@@ -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)
Copy link

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?

Copy link
Contributor Author

@rjongeneelen rjongeneelen Sep 14, 2017

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.

: 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)
Copy link

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

Copy link
Contributor Author

@rjongeneelen rjongeneelen Sep 14, 2017

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();
Copy link

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>

Copy link
Contributor Author

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();
Copy link

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()

Copy link
Contributor Author

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();
Copy link

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()

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See above

@rjongeneelen
Copy link
Contributor Author

rjongeneelen commented Sep 14, 2017

@reisenberger Not sure why the build fails, looks like a generic build error, rather than related to my changes?

@reisenberger
Copy link
Member

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 PolicyWrap config code was on my list to tidy: please feel free!

(Apologies that vacation followed by tight commitments have kept me from these PRs just lately ...)

@reisenberger
Copy link
Member

Cross-ref build issue

@reisenberger
Copy link
Member

@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!)

@reisenberger
Copy link
Member

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 PolicyWrap config syntax, which also found its way into #370.

#370 also addressed some of the useful comments on further cleanups for PolicyWrap syntax arising in discussion from @udlose and @rjongeneelen , thanks y'all!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants