Skip to content
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

Service Discovery: fix shutdown blocking indefinitely in some cases #880

Merged
merged 3 commits into from
Nov 16, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ private void CleanupResolvers()
{
lock (_lock)
{
if (_cleanupTask is { IsCompleted: true })
if (_cleanupTask is null or { IsCompleted: true })
{
_cleanupTask = CleanupResolversAsyncCore();
}
Expand Down Expand Up @@ -159,7 +159,7 @@ private sealed class ResolverEntry : IAsyncDisposable
{
private readonly ServiceEndPointResolver _resolver;
private readonly IServiceEndPointSelector _selector;
private const ulong CountMask = unchecked((ulong)-1);
private const ulong CountMask = ~(RecentUseFlag | DisposingFlag);
ReubenBond marked this conversation as resolved.
Show resolved Hide resolved
private const ulong RecentUseFlag = 1UL << 61;
private const ulong DisposingFlag = 1UL << 62;
ReubenBond marked this conversation as resolved.
Show resolved Hide resolved
private ulong _status;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ private void CleanupResolvers()
{
lock (_lock)
{
if (_cleanupTask is { IsCompleted: true })
if (_cleanupTask is null or { IsCompleted: true })
{
_cleanupTask = CleanupResolversAsyncCore();
}
Expand Down Expand Up @@ -155,7 +155,7 @@ private ResolverEntry CreateResolver(string serviceName)
private sealed class ResolverEntry(ServiceEndPointResolver resolver) : IAsyncDisposable
{
private readonly ServiceEndPointResolver _resolver = resolver;
private const ulong CountMask = unchecked((ulong)-1);
private const ulong CountMask = ~(RecentUseFlag | DisposingFlag);
private const ulong RecentUseFlag = 1UL << 61;
private const ulong DisposingFlag = 1UL << 62;
private ulong _status;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
using Microsoft.Extensions.Logging.Abstractions;
using Microsoft.Extensions.Primitives;
using Microsoft.Extensions.ServiceDiscovery.Abstractions;
using Microsoft.Extensions.ServiceDiscovery.Http;
using Xunit;

namespace Microsoft.Extensions.ServiceDiscovery.Tests;
Expand Down Expand Up @@ -135,6 +136,80 @@ public async Task ResolveServiceEndPoint()
}
}

[Fact]
public async Task ResolveServiceEndPointOneShot()
{
var cts = new[] { new CancellationTokenSource() };
var innerResolver = new FakeEndPointResolver(
resolveAsync: (collection, ct) =>
{
collection.AddChangeToken(new CancellationChangeToken(cts[0].Token));
collection.EndPoints.Add(ServiceEndPoint.Create(new IPEndPoint(IPAddress.Parse("127.1.1.1"), 8080)));

if (cts[0].Token.IsCancellationRequested)
{
cts[0] = new();
collection.EndPoints.Add(ServiceEndPoint.Create(new IPEndPoint(IPAddress.Parse("127.1.1.2"), 8888)));
}
return default;
},
disposeAsync: () => default);
var resolverProvider = new FakeEndPointResolverProvider(name => (true, innerResolver));
var services = new ServiceCollection()
.AddSingleton<IServiceEndPointResolverProvider>(resolverProvider)
.AddServiceDiscoveryCore()
.BuildServiceProvider();
var resolver = services.GetRequiredService<ServiceEndPointResolverRegistry>();

Assert.NotNull(resolver);
var initialEndPoints = await resolver.GetEndPointsAsync("http://basket", CancellationToken.None).ConfigureAwait(false);
Assert.NotNull(initialEndPoints);
var sep = Assert.Single(initialEndPoints);
var ip = Assert.IsType<IPEndPoint>(sep.EndPoint);
Assert.Equal(IPAddress.Parse("127.1.1.1"), ip.Address);
Assert.Equal(8080, ip.Port);

await services.DisposeAsync().ConfigureAwait(false);
}

[Fact]
public async Task ResolveHttpServiceEndPointOneShot()
{
var cts = new[] { new CancellationTokenSource() };
var innerResolver = new FakeEndPointResolver(
resolveAsync: (collection, ct) =>
{
collection.AddChangeToken(new CancellationChangeToken(cts[0].Token));
collection.EndPoints.Add(ServiceEndPoint.Create(new IPEndPoint(IPAddress.Parse("127.1.1.1"), 8080)));

if (cts[0].Token.IsCancellationRequested)
{
cts[0] = new();
collection.EndPoints.Add(ServiceEndPoint.Create(new IPEndPoint(IPAddress.Parse("127.1.1.2"), 8888)));
}
return default;
},
disposeAsync: () => default);
var fakeResolverProvider = new FakeEndPointResolverProvider(name => (true, innerResolver));
var services = new ServiceCollection()
.AddSingleton<IServiceEndPointResolverProvider>(fakeResolverProvider)
.AddServiceDiscoveryCore()
.BuildServiceProvider();
var selectorProvider = services.GetRequiredService<IServiceEndPointSelectorProvider>();
var resolverProvider = services.GetRequiredService<ServiceEndPointResolverFactory>();
await using var resolver = new HttpServiceEndPointResolver(resolverProvider, selectorProvider, TimeProvider.System);

Assert.NotNull(resolver);
var httpRequest = new HttpRequestMessage(HttpMethod.Get, "http://basket");
var endPoint = await resolver.GetEndpointAsync(httpRequest, CancellationToken.None).ConfigureAwait(false);
Assert.NotNull(endPoint);
var ip = Assert.IsType<IPEndPoint>(endPoint.EndPoint);
Assert.Equal(IPAddress.Parse("127.1.1.1"), ip.Address);
Assert.Equal(8080, ip.Port);

await services.DisposeAsync().ConfigureAwait(false);
}

[Fact]
public async Task ResolveServiceEndPoint_ThrowOnReload()
{
Expand Down