Example #1
0
        public async Task ReturnAsync(ProxyEntry proxy, HttpStatusCode statusCode)
        {
            if (proxy == null)
            {
                return;
            }

            if (_dict.TryGetValue(proxy.Uri, out var p))
            {
                if (statusCode.IsSuccessStatusCode())
                {
                    p.FailedNum = 0;
                }
                else
                {
                    p.FailedNum += 1;
                }

                if (p.FailedNum > 6)
                {
                    _dict.TryRemove(p.Uri, out _);
                    return;
                }

                if (_proxyValidator != null && p.FailedNum % 3 == 0 && await _proxyValidator.IsAvailable(proxy))
                {
                    _dict.TryRemove(p.Uri, out _);
                    return;
                }

                _queue.TryDequeue(out p);
            }
        }
Example #2
0
        public async Task <bool> IsAvailable(ProxyEntry proxy)
        {
            if (proxy == null)
            {
                return(false);
            }

            var httpClient = _httpClientFactory.CreateClient($"PROXY_{proxy.Uri}");

            httpClient.Timeout = new TimeSpan(0, 0, 3);
            try
            {
                var msg      = new HttpRequestMessage(HttpMethod.Head, _options.ProxyTestUrl);
                var response = await httpClient.SendAsync(msg);

                return(response.IsSuccessStatusCode);
            }
            catch (Exception)
            {
                _logger.LogWarning($"Proxy {proxy.Uri} is not available");
                return(false);
            }
        }
Example #3
0
 public ReturnProxyTask(IProxyService pool, ProxyEntry httpProxy)
 {
     _pool      = pool;
     _httpProxy = httpProxy;
 }