private UriCircuitBreaker GetCircuitBreaker(Uri uri) { uri = new Uri(uri.GetLeftPart(UriPartial.Path)); UriCircuitBreaker ucb; Monitor.Enter(m_pool); try { if ((m_evictionScan = (m_evictionScan + 1) % c_evictionScanFrequency) == 0) { var staleUris = from kvp in m_pool where kvp.Value.LastAttempt.Add(m_evictionStaleTime) < DateTime.UtcNow select kvp.Key; foreach (var staleUri in staleUris.ToArray()) { m_pool.Remove(staleUri); } } if (!m_pool.TryGetValue(uri, out ucb)) { m_pool.Add(uri, ucb = new UriCircuitBreaker()); } } finally { Monitor.Exit(m_pool); } return(ucb); }
protected override async Task <HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken) { UriCircuitBreaker ucb = GetCircuitBreaker(request.RequestUri); ucb.ThrowIfOpen(m_failuresToOpen, m_timeToStayOpen); // If we get here, we're closed try { HttpResponseMessage t = await base.SendAsync(request, cancellationToken); ucb.ReportAttempt(t.IsSuccessStatusCode, m_failuresToOpen); return(t); } catch (Exception /*ex*/) { ucb.ReportAttempt(false, m_failuresToOpen); throw; } }