Esempio n. 1
0
        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);
            }
        }
Esempio n. 2
0
 internal void PrepareHttpClient(HttpClientEntry httpClientEntry)
 {
     httpClientEntry.Init(AllowAutoRedirect, () =>
     {
         if (!Equals(httpClientEntry.Client.Timeout.TotalSeconds, _timeout))
         {
             httpClientEntry.Client.Timeout = new TimeSpan(0, 0, (int)_timeout / 1000);
         }
     }, CopyCookieContainer);
 }
Esempio n. 3
0
        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();
                }
            }
        }