private RemoteImageRequest AttemptRetry(RemoteImageRequest failedRequest, string imageUrl, ImageOptions options) { if (!failedRequest.IsRequestCompleted) { throw new ArgumentException("The request has not completed.", nameof(failedRequest)); } if (failedRequest.Task.Result.ResultCode == ServiceResultCode.Success) { throw new ArgumentException("The request was successful, no retry needed.", nameof(failedRequest)); } if (failedRequest.Task.Result.ResultCode == ServiceResultCode.ResourceNotFound || failedRequest.RetryCounter == this.maxRetryAttempts) { return(failedRequest); } int retryCounter = failedRequest.RetryCounter; if (failedRequest.Task.Result.ResultCode != ServiceResultCode.NetworkUnavailable) { // If the network is unavailable we will keep retrying until it becomes available. // It is assumed that the underlying remote client is optimized to handle network status // so that a call to make repeated remote request is cheap. retryCounter++; } string requestKey = this.GetRequestKey(imageUrl, options); this.TryRemoveRequest(requestKey); return(this.TryStartImageRequest(imageUrl, options, retryCounter)); }
private RemoteImageRequest TryStartImageRequest(string imageUrl, ImageOptions options, int retryCounter = 0) { string requestKey = this.GetRequestKey(imageUrl, options); RemoteImageRequest requestToStart = this.imageRequests.GetOrAdd( requestKey, _ => new RemoteImageRequest( () => this.resourceDownloader.DownloadImage(imageUrl, options), result => this.DownloadComplete(requestKey, result), imageData => this.imageCache.Put(requestKey, imageData), retryCounter)); this.StartPendingDownloadsAsync(); return(requestToStart); }