-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Allow HttpClient instances to be refreshed periodically #4673
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
ffc9dd8
Fix check for agressive connectionlimit
Mpdreamz 1cacf37
remove unused reference
Mpdreamz 1455f78
Detect if CurlHandler exists
russcam fb40e0d
initial commit
Mpdreamz c88fc62
add tests and configuration for DnsRefreshTimeout
Mpdreamz 22b1f6f
failing readonly test
Mpdreamz 67af583
Apply suggestions from code review
Mpdreamz b579a86
link back to dotnet/runtime source
Mpdreamz File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
1,253 changes: 636 additions & 617 deletions
1,253
src/Elasticsearch.Net/Configuration/ConnectionConfiguration.cs
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
84 changes: 84 additions & 0 deletions
84
src/Elasticsearch.Net/Connection/HandlerTracking/ActiveHandlerTrackingEntry.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
// See the LICENSE file in the project root for more information. | ||
|
||
#if DOTNETCORE | ||
using System; | ||
using System.Diagnostics; | ||
using System.Threading; | ||
|
||
namespace Elasticsearch.Net | ||
{ | ||
/// <summary> | ||
/// Thread-safety: We treat this class as immutable except for the timer. Creating a new object | ||
/// for the 'expiry' pool simplifies the threading requirements significantly. | ||
/// <para>https://github.com/dotnet/runtime/blob/master/src/libraries/Microsoft.Extensions.Http/src/ActiveHandlerTrackingEntry.cs</para> | ||
/// </summary> | ||
internal class ActiveHandlerTrackingEntry | ||
{ | ||
private static readonly TimerCallback TimerCallback = (s) => ((ActiveHandlerTrackingEntry)s).Timer_Tick(); | ||
private readonly object _lock; | ||
private bool _timerInitialized; | ||
private Timer _timer; | ||
private TimerCallback _callback; | ||
|
||
public ActiveHandlerTrackingEntry( | ||
int key, | ||
LifetimeTrackingHttpMessageHandler handler, | ||
TimeSpan lifetime) | ||
{ | ||
Key = key; | ||
Handler = handler; | ||
Lifetime = lifetime; | ||
|
||
_lock = new object(); | ||
} | ||
|
||
public LifetimeTrackingHttpMessageHandler Handler { get; private set; } | ||
|
||
public TimeSpan Lifetime { get; } | ||
|
||
public int Key { get; } | ||
|
||
public void StartExpiryTimer(TimerCallback callback) | ||
{ | ||
if (Lifetime == Timeout.InfiniteTimeSpan) return; | ||
|
||
if (Volatile.Read(ref _timerInitialized)) return; | ||
|
||
StartExpiryTimerSlow(callback); | ||
} | ||
|
||
private void StartExpiryTimerSlow(TimerCallback callback) | ||
{ | ||
Debug.Assert(Lifetime != Timeout.InfiniteTimeSpan); | ||
|
||
lock (_lock) | ||
{ | ||
if (Volatile.Read(ref _timerInitialized)) | ||
return; | ||
|
||
_callback = callback; | ||
_timer = NonCapturingTimer.Create(TimerCallback, this, Lifetime, Timeout.InfiniteTimeSpan); | ||
_timerInitialized = true; | ||
} | ||
} | ||
|
||
private void Timer_Tick() | ||
{ | ||
Debug.Assert(_callback != null); | ||
Debug.Assert(_timer != null); | ||
|
||
lock (_lock) | ||
{ | ||
if (_timer == null) return; | ||
|
||
_timer.Dispose(); | ||
_timer = null; | ||
|
||
_callback(this); | ||
} | ||
} | ||
} | ||
} | ||
#endif |
36 changes: 36 additions & 0 deletions
36
src/Elasticsearch.Net/Connection/HandlerTracking/ExpiredHandlerTrackingEntry.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
// See the LICENSE file in the project root for more information. | ||
Mpdreamz marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
#if DOTNETCORE | ||
using System; | ||
using System.Net.Http; | ||
|
||
namespace Elasticsearch.Net | ||
{ | ||
/// <summary> | ||
/// Thread-safety: This class is immutable | ||
/// <para>https://github.com/dotnet/runtime/blob/master/src/libraries/Microsoft.Extensions.Http/src/ExpiredHandlerTrackingEntry.cs</para> | ||
/// </summary> | ||
internal class ExpiredHandlerTrackingEntry | ||
{ | ||
private readonly WeakReference _livenessTracker; | ||
|
||
// IMPORTANT: don't cache a reference to `other` or `other.Handler` here. | ||
// We need to allow it to be GC'ed. | ||
public ExpiredHandlerTrackingEntry(ActiveHandlerTrackingEntry other) | ||
{ | ||
Key = other.Key; | ||
|
||
_livenessTracker = new WeakReference(other.Handler); | ||
InnerHandler = other.Handler.InnerHandler; | ||
} | ||
|
||
public bool CanDispose => !_livenessTracker.IsAlive; | ||
|
||
public HttpMessageHandler InnerHandler { get; } | ||
|
||
public int Key { get; } | ||
} | ||
} | ||
#endif |
27 changes: 27 additions & 0 deletions
27
src/Elasticsearch.Net/Connection/HandlerTracking/LifetimeTrackingHttpMessageHandler.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
// See the LICENSE file in the project root for more information. | ||
Mpdreamz marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
#if DOTNETCORE | ||
using System.Net.Http; | ||
|
||
namespace Elasticsearch.Net | ||
{ | ||
/// <summary> | ||
/// This a marker used to check if the underlying handler should be disposed. HttpClients | ||
/// share a reference to an instance of this class, and when it goes out of scope the inner handler | ||
/// is eligible to be disposed. | ||
/// <para>https://github.com/dotnet/runtime/blob/master/src/libraries/Microsoft.Extensions.Http/src/LifetimeTrackingHttpMessageHandler.cs</para> | ||
/// </summary> | ||
internal class LifetimeTrackingHttpMessageHandler : DelegatingHandler | ||
{ | ||
public LifetimeTrackingHttpMessageHandler(HttpMessageHandler innerHandler) | ||
: base(innerHandler) { } | ||
|
||
protected override void Dispose(bool disposing) | ||
{ | ||
// The lifetime of this is tracked separately by ActiveHandlerTrackingEntry | ||
} | ||
} | ||
} | ||
#endif |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.