Skip to content
GitLab
Projects Groups Snippets
  • /
  • Help
    • Help
    • Support
    • Community forum
    • Submit feedback
    • Contribute to GitLab
  • Sign in / Register
  • O openapi-generator
  • Project information
    • Project information
    • Activity
    • Labels
    • Members
  • Repository
    • Repository
    • Files
    • Commits
    • Branches
    • Tags
    • Contributors
    • Graph
    • Compare
  • Issues 3,476
    • Issues 3,476
    • List
    • Boards
    • Service Desk
    • Milestones
  • Merge requests 402
    • Merge requests 402
  • CI/CD
    • CI/CD
    • Pipelines
    • Jobs
    • Schedules
  • Deployments
    • Deployments
    • Environments
    • Releases
  • Packages and registries
    • Packages and registries
    • Package Registry
    • Infrastructure Registry
  • Monitor
    • Monitor
    • Incidents
  • Analytics
    • Analytics
    • Value stream
    • CI/CD
    • Repository
  • Wiki
    • Wiki
  • Snippets
    • Snippets
  • Activity
  • Graph
  • Create a new issue
  • Jobs
  • Commits
  • Issue Boards
Collapse sidebar
  • OpenAPI Tools
  • openapi-generator
  • Merge requests
  • !7495

Use Policy<IRestResponse>, AsyncPolicy<IRestResponse> in RetryConfiguration.mustache

  • Review changes

  • Download
  • Email patches
  • Plain diff
Merged Administrator requested to merge github/fork/jeffshantz/master into master Sep 23, 2020
  • Overview 0
  • Commits 2
  • Pipelines 0
  • Changes 3

Created by: jeffshantz

Summary

Use Policy<IRestResponse> and AsyncPolicy<IRestResponse> instead of RetryPolicy<IRestResponse> and AsyncRetryPolicy<IRestResponse> to allow for use of wrapped policies.

Background

In the .NET Core client generator, RetryConfiguration uses the type RetryPolicy<IRestResponse> and AsyncRetryPolicy<IRestResponse> for its properties RetryPolicy and AsyncRetryPolicy, respectively.

This is useful, as it allows one to define a retry policy:

      RetryConfiguration.RetryPolicy = 
        Policy
          .HandleResult<IRestResponse>(r => r.StatusCode == HttpStatusCode.Unauthorized)
          .Retry(retryCount: 3,
                 onRetry: ((result, retryNumber, context) =>
                            {
                              // Fetch new API token here
                              Console.WriteLine("Retrying...");
                            }));

The problem is that this does not allow us to exploit the full utility of Polly. For instance, after retrying 3 times, I might want to log a message or perform some action before giving up. In this case, it would be nice to use Policy.Wrap():

 var retryPolicy =
        Policy
          .HandleResult<IRestResponse>(r => r.StatusCode == HttpStatusCode.Unauthorized)
          .Retry(retryCount: 3,
                 onRetry: ((result, retryNumber, context) =>
                            {
                              // Fetch new API token here
                              Console.WriteLine("Retrying...");
                            }));

      var giveUpPolicy =
        Policy
          .HandleResult<IRestResponse>(r => true)
          .Fallback(fallbackValue: null, onFallback: result =>
          {
            Console.WriteLine("Giving up");
          });

      RetryConfiguration.RetryPolicy = giveUpPolicy.Wrap(retryPolicy);

Solution

This pull request simply changes the type of the RetryPolicy and AsyncRetryPolicy properties to their higher-level base classes in RetryConfiguration such that:

/// <summary>
/// Retry policy
/// </summary>
 public static RetryPolicy<IRestResponse> RetryPolicy { get; set; }

 /// <summary>
/// Async retry policy
/// </summary>
public static AsyncRetryPolicy<IRestResponse> AsyncRetryPolicy { get; set; }

becomes:

/// <summary>
/// Retry policy
/// </summary>
 public static Policy<IRestResponse> RetryPolicy { get; set; }

 /// <summary>
/// Async retry policy
/// </summary>
public static AsyncPolicy<IRestResponse> AsyncRetryPolicy { get; set; }

Then, one can still assign a RetryPolicy as usual, but one can now also use wrapped policies, as above. No other changes are required -- this worked out of the box when I made these type changes.

Assignee
Assign to
Reviewers
Request review from
Time tracking
Source branch: github/fork/jeffshantz/master