예제 #1
0
        /// <summary>
        /// SendAsync override method.
        /// Recursive call.
        /// </summary>
        /// <param name="request">The request given.</param>
        /// <param name="cancellationToken">The cancelation token.</param>
        /// <returns>The Http response.</returns>
        protected override async Task <HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
        {
            if (request == null)
            {
                throw new ArgumentNullException(nameof(request));
            }

            Uri         originalUri = request.RequestUri;
            DnsEndPoint host        = await TargetSelector.SelectHostAsync(ServiceDescription);

            if (host == null)
            {
                Logger?.LogInformation("No Dns Host Found");
                return(await base.SendAsync(request, cancellationToken));
            }

            request.RequestUri = ReplaceHost(request.RequestUri, host);
            Logger?.LogInformation("Request uri : {requestRequestUri}", request.RequestUri);
            var response = await base.SendAsync(request, cancellationToken);

            if (response == null)
            {
                return(response);
            }

            Logger?.LogTrace("Response status code : {response.StatusCode}", response.StatusCode);
            if (QuarantinePolicy.ShouldQuarantine(response))
            {
                Logger?.LogWarning("Host {host} (from original host {original_host}) is send in quarantine", host, originalUri.Host);
                await TargetSelector.BlacklistHostForAsync(host, QuarantinePolicy.QuarantineDuration);

                request.RequestUri = originalUri;
                return(await SendAsync(request, cancellationToken));
            }

            return(response);
        }