示例#1
0
        internal static async Task <bool> SharedRetryForExceptionAsync(Runtime.IExecutionContext executionContext, Exception exception,
                                                                       Func <Runtime.IExecutionContext, Exception, bool?> retryForExceptionSync,
                                                                       Func <Runtime.IExecutionContext, Exception, bool> baseRetryForException)
        {
            var syncResult = retryForExceptionSync(executionContext, exception);

            if (syncResult.HasValue)
            {
                return(syncResult.Value);
            }
            else
            {
                var         serviceException = exception as AmazonServiceException;
                string      correctedRegion  = null;
                AmazonS3Uri s3BucketUri;
                if (AmazonS3Uri.TryParseAmazonS3Uri(executionContext.RequestContext.Request.Endpoint, out s3BucketUri))
                {
                    var credentials = executionContext.RequestContext.ImmutableCredentials;
                    correctedRegion = await BucketRegionDetector.DetectMismatchWithHeadBucketFallbackAsync(s3BucketUri, serviceException, credentials).ConfigureAwait(false);
                }

                if (correctedRegion == null)
                {
                    return(baseRetryForException(executionContext, exception));
                }
                else
                {
                    // change authentication region of request and signal the handler to sign again with the new region
                    executionContext.RequestContext.Request.AuthenticationRegion = correctedRegion;
                    executionContext.RequestContext.IsSigned = false;
                    return(true);
                }
            }
        }
        /// <summary>
        /// Return true if the request should be retried. Implements additional checks
        /// specific to S3 on top of the checks in DefaultRetryPolicy.
        /// </summary>
        /// <param name="executionContext">Request context containing the state of the request.</param>
        /// <param name="exception">The exception thrown by the previous request.</param>
        /// <returns>Return true if the request should be retried.</returns>
        public override bool RetryForException(Runtime.IExecutionContext executionContext, Exception exception)
        {
            var syncResult = RetryForExceptionSync(executionContext, exception);

            if (syncResult.HasValue)
            {
                return(syncResult.Value);
            }
            else
            {
                var         serviceException = exception as AmazonServiceException;
                string      correctedRegion  = null;
                AmazonS3Uri s3BucketUri;
                if (AmazonS3Uri.TryParseAmazonS3Uri(executionContext.RequestContext.Request.Endpoint, out s3BucketUri))
                {
                    var credentials = executionContext.RequestContext.ImmutableCredentials;
                    if (credentials != null)
                    {
                        correctedRegion = BucketRegionDetector.DetectMismatchWithHeadBucketFallback(s3BucketUri, serviceException, credentials);
                    }
                }

                if (correctedRegion == null)
                {
                    return(base.RetryForException(executionContext, exception));
                }
                else
                {
                    // change authentication region of request and signal the handler to sign again with the new region
                    executionContext.RequestContext.Request.AuthenticationRegion = correctedRegion;
                    executionContext.RequestContext.IsSigned = false;
                    return(true);
                }
            }
        }