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 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(); } } }