internal void OnDispose(DisposeRecycleHandler disposeRecycleHandler) { if (System.Threading.Interlocked.Decrement(ref this._Usage) <= 0) { // Create Timer lock (this) { if (Volatile.Read(ref this._Timer) == null) { var timer = NonCapturingTimer.Create(this.OnTimer, null, TimeSpan.FromMinutes(2), TimeSpan.FromMinutes(1)); if (System.Threading.Interlocked.CompareExchange(ref this._Timer, timer, null) is null) { // OK } else { timer.Dispose(); } } } } }
public HttpClient CreateHttpClient() { System.Threading.Interlocked.Exchange(ref this._Timer, null)?.Dispose(); var handler = this._ReuseRecycleHandler; if (handler == null) { lock (this) { handler = Volatile.Read(ref this._ReuseRecycleHandler); if (handler == null) { handler = this.CreateReuseRecycleHandler(); this._ReuseRecycleHandler = handler; } } } // using ref count instead of weak references System.Threading.Interlocked.Increment(ref this._Usage); try { // var trigger = new DisposeRecycleHandler(handler, this); var httpClient = new HttpClient(trigger, true); if (!string.IsNullOrEmpty(this._Configuration.BaseAddress)) { httpClient.BaseAddress = new Uri(this._Configuration.BaseAddress); } foreach (var action in this._Configuration.HttpClientConfigurations) { if (action != null) { action(httpClient, this._Configuration); } } return(httpClient); } catch { System.Threading.Interlocked.Decrement(ref this._Usage); throw; } }