public async Task InvokeAsync()

in src/dotnet/AzureAI.Proxy/ReverseProxy/RetryMiddleware.cs [20:50]


    public async Task InvokeAsync(HttpContext context)
    {
        context.Request.EnableBuffering();

        var shouldRetry = true;
        var retryCount = 0;

        while (shouldRetry)
        {
            var reverseProxyFeature = context.GetReverseProxyFeature();
            var destination = PickOneDestination(reverseProxyFeature);

            reverseProxyFeature.AvailableDestinations = new List<DestinationState>(destination);

            if (retryCount > 0)
            {
                //If this is a retry, we must reset the request body to initial position and clear the current response
                context.Request.Body.Position = 0;
                reverseProxyFeature.ProxiedDestination = null;
                context.Response.Clear();
            }

            await _next(context);

            var statusCode = context.Response.StatusCode;
            var atLeastOneBackendHealthy = GetNumberHealthyEndpoints(context) > 0;
            retryCount++;

            shouldRetry = (statusCode is 429 or >= 500) && atLeastOneBackendHealthy;
        }
    }