public HttpClientEntry GetHttpClient(string hash)
        {
            if (string.IsNullOrWhiteSpace(hash))
            {
                hash = string.Empty;
            }
            _getHttpClientCount.Inc();

            if (_getHttpClientCount.Value % 100 == 0)
            {
                CleanupPool();
            }

            if (_pool.ContainsKey(hash))
            {
                _pool[hash].ActiveTime = DateTime.Now;
                return(_pool[hash]);
            }
            else
            {
                var item = new HttpClientEntry();
                _pool.Add(hash, item);
                return(item);
            }
        }
 private void PrepareHttpClient(HttpClientEntry httpClientEntry)
 {
     httpClientEntry.Init(AllowAutoRedirect, () =>
     {
         if (!Equals(httpClientEntry.Client.Timeout.TotalSeconds, _timeout))
         {
             httpClientEntry.Client.Timeout = new TimeSpan(0, 0, (int)_timeout);
         }
     }, CopyCookieContainer);
 }
 internal void PrepareHttpClient(HttpClientEntry httpClientEntry, IWebProxy proxy)
 {
     httpClientEntry.Init(AllowAutoRedirect, () =>
     {
         if (!Equals(httpClientEntry.Client.Timeout.TotalSeconds, _timeout))
         {
             httpClientEntry.Client.Timeout = new TimeSpan(0, 0, (int)_timeout / 1000);
         }
         httpClientEntry.Handler.Proxy = proxy;
     }, CopyCookieContainer);
 }
        private void CleanupPool()
        {
            List <string> needRemoveEntries = new List <string>();
            var           now = DateTime.Now;

            foreach (var pair in _pool)
            {
                if ((now - pair.Value.ActiveTime).TotalSeconds > 240)
                {
                    needRemoveEntries.Add(pair.Key);
                }
            }

            foreach (var key in needRemoveEntries)
            {
                HttpClientEntry item = _pool[key];
                if (_pool.Remove(key))
                {
                    item.Client.Dispose();
                }
            }
        }