Skip to content

Commit 032d309

Browse files
committed
API review: rename ServiceEndPointCollectionSource to IServiceEndPointBuilder
1 parent 6b09407 commit 032d309

File tree

19 files changed

+166
-157
lines changed

19 files changed

+166
-157
lines changed
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the MIT license.
3+
4+
using Microsoft.AspNetCore.Http.Features;
5+
using Microsoft.Extensions.Primitives;
6+
7+
namespace Microsoft.Extensions.ServiceDiscovery;
8+
9+
/// <summary>
10+
/// Builder to create a <see cref="ServiceEndPointSource"/> instances.
11+
/// </summary>
12+
public interface IServiceEndPointBuilder
13+
{
14+
/// <summary>
15+
/// Gets the endpoints.
16+
/// </summary>
17+
IList<ServiceEndPoint> EndPoints { get; }
18+
19+
/// <summary>
20+
/// Gets the feature collection.
21+
/// </summary>
22+
IFeatureCollection Features { get; }
23+
24+
/// <summary>
25+
/// Adds a change token to the resulting <see cref="ServiceEndPointSource"/>.
26+
/// </summary>
27+
/// <param name="changeToken">The change token.</param>
28+
void AddChangeToken(IChangeToken changeToken);
29+
}

src/Microsoft.Extensions.ServiceDiscovery.Abstractions/IServiceEndPointProvider.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,5 +14,5 @@ public interface IServiceEndPointProvider : IAsyncDisposable
1414
/// <param name="endPoints">The endpoint collection, which resolved endpoints will be added to.</param>
1515
/// <param name="cancellationToken">The token to monitor for cancellation requests.</param>
1616
/// <returns>The resolution status.</returns>
17-
ValueTask ResolveAsync(ServiceEndPointCollectionSource endPoints, CancellationToken cancellationToken);
17+
ValueTask ResolveAsync(IServiceEndPointBuilder endPoints, CancellationToken cancellationToken);
1818
}

src/Microsoft.Extensions.ServiceDiscovery.Abstractions/IServiceEndPointSelector.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,10 @@ public interface IServiceEndPointSelector
1212
/// Sets the collection of endpoints which this instance will select from.
1313
/// </summary>
1414
/// <param name="endPoints">The collection of endpoints to select from.</param>
15-
void SetEndPoints(ServiceEndPointCollection endPoints);
15+
void SetEndPoints(ServiceEndPointSource endPoints);
1616

1717
/// <summary>
18-
/// Selects an endpoints from the collection provided by the most recent call to <see cref="SetEndPoints(ServiceEndPointCollection)"/>.
18+
/// Selects an endpoints from the collection provided by the most recent call to <see cref="SetEndPoints(ServiceEndPointSource)"/>.
1919
/// </summary>
2020
/// <param name="context">The context.</param>
2121
/// <returns>An endpoint.</returns>
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,28 @@
11
// Licensed to the .NET Foundation under one or more agreements.
22
// The .NET Foundation licenses this file to you under the MIT license.
33

4-
using System.Collections;
54
using System.Diagnostics;
65
using Microsoft.AspNetCore.Http.Features;
76
using Microsoft.Extensions.Primitives;
87

98
namespace Microsoft.Extensions.ServiceDiscovery;
109

1110
/// <summary>
12-
/// Represents an immutable collection of service endpoints.
11+
/// Represents a collection of service endpoints.
1312
/// </summary>
1413
[DebuggerDisplay("{ToString(),nq}")]
1514
[DebuggerTypeProxy(typeof(ServiceEndPointCollectionDebuggerView))]
16-
public sealed class ServiceEndPointCollection : IReadOnlyList<ServiceEndPoint>
15+
public sealed class ServiceEndPointSource
1716
{
1817
private readonly List<ServiceEndPoint>? _endpoints;
1918

2019
/// <summary>
21-
/// Initializes a new <see cref="ServiceEndPointCollection"/> instance.
20+
/// Initializes a new <see cref="ServiceEndPointSource"/> instance.
2221
/// </summary>
2322
/// <param name="endpoints">The endpoints.</param>
2423
/// <param name="changeToken">The change token.</param>
2524
/// <param name="features">The feature collection.</param>
26-
public ServiceEndPointCollection(List<ServiceEndPoint>? endpoints, IChangeToken changeToken, IFeatureCollection features)
25+
public ServiceEndPointSource(List<ServiceEndPoint>? endpoints, IChangeToken changeToken, IFeatureCollection features)
2726
{
2827
ArgumentNullException.ThrowIfNull(changeToken);
2928

@@ -32,8 +31,10 @@ public ServiceEndPointCollection(List<ServiceEndPoint>? endpoints, IChangeToken
3231
ChangeToken = changeToken;
3332
}
3433

35-
/// <inheritdoc/>
36-
public ServiceEndPoint this[int index] => _endpoints?[index] ?? throw new ArgumentOutOfRangeException(nameof(index));
34+
/// <summary>
35+
/// Gets the endpoints.
36+
/// </summary>
37+
public IReadOnlyList<ServiceEndPoint> EndPoints => _endpoints ?? (IReadOnlyList<ServiceEndPoint>)[];
3738

3839
/// <summary>
3940
/// Gets the change token which indicates when this collection should be refreshed.
@@ -45,15 +46,6 @@ public ServiceEndPointCollection(List<ServiceEndPoint>? endpoints, IChangeToken
4546
/// </summary>
4647
public IFeatureCollection Features { get; }
4748

48-
/// <inheritdoc/>
49-
public int Count => _endpoints?.Count ?? 0;
50-
51-
/// <inheritdoc/>
52-
public IEnumerator<ServiceEndPoint> GetEnumerator() => _endpoints?.GetEnumerator() ?? Enumerable.Empty<ServiceEndPoint>().GetEnumerator();
53-
54-
/// <inheritdoc/>
55-
IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();
56-
5749
/// <inheritdoc/>
5850
public override string ToString()
5951
{
@@ -65,13 +57,13 @@ public override string ToString()
6557
return $"[{string.Join(", ", eps)}]";
6658
}
6759

68-
private sealed class ServiceEndPointCollectionDebuggerView(ServiceEndPointCollection value)
60+
private sealed class ServiceEndPointCollectionDebuggerView(ServiceEndPointSource value)
6961
{
7062
public IChangeToken ChangeToken => value.ChangeToken;
7163

7264
public IFeatureCollection Features => value.Features;
7365

7466
[DebuggerBrowsable(DebuggerBrowsableState.RootHidden)]
75-
public ServiceEndPoint[] EndPoints => value.ToArray();
67+
public ServiceEndPoint[] EndPoints => value.EndPoints.ToArray();
7668
}
7769
}

src/Microsoft.Extensions.ServiceDiscovery.Dns/DnsServiceEndPointResolverBase.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ protected DnsServiceEndPointResolverBase(
5858
protected CancellationToken ShutdownToken => _disposeCancellation.Token;
5959

6060
/// <inheritdoc/>
61-
public async ValueTask ResolveAsync(ServiceEndPointCollectionSource endPoints, CancellationToken cancellationToken)
61+
public async ValueTask ResolveAsync(IServiceEndPointBuilder endPoints, CancellationToken cancellationToken)
6262
{
6363
// Only add endpoints to the collection if a previous provider (eg, a configuration override) did not add them.
6464
if (endPoints.EndPoints.Count != 0)

src/Microsoft.Extensions.ServiceDiscovery.Yarp/ServiceDiscoveryDestinationResolver.cs

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -54,32 +54,32 @@ public async ValueTask<ResolvedDestinationCollection> ResolveDestinationsAsync(I
5454
var originalHost = originalConfig.Host is { Length: > 0 } h ? h : originalUri.Authority;
5555
var serviceName = originalUri.GetLeftPart(UriPartial.Authority);
5656

57-
var endPoints = await resolver.GetEndPointsAsync(serviceName, cancellationToken).ConfigureAwait(false);
58-
var results = new List<(string Name, DestinationConfig Config)>(endPoints.Count);
57+
var result = await resolver.GetEndPointsAsync(serviceName, cancellationToken).ConfigureAwait(false);
58+
var results = new List<(string Name, DestinationConfig Config)>(result.EndPoints.Count);
5959
var uriBuilder = new UriBuilder(originalUri);
6060
var healthUri = originalConfig.Health is { Length: > 0 } health ? new Uri(health) : null;
6161
var healthUriBuilder = healthUri is { } ? new UriBuilder(healthUri) : null;
62-
foreach (var endPoint in endPoints)
62+
foreach (var endPoint in result.EndPoints)
6363
{
6464
var addressString = endPoint.GetEndPointString();
65-
Uri result;
65+
Uri uri;
6666
if (!addressString.Contains("://"))
6767
{
68-
result = new Uri($"https://{addressString}");
68+
uri = new Uri($"https://{addressString}");
6969
}
7070
else
7171
{
72-
result = new Uri(addressString);
72+
uri = new Uri(addressString);
7373
}
7474

75-
uriBuilder.Host = result.Host;
76-
uriBuilder.Port = result.Port;
75+
uriBuilder.Host = uri.Host;
76+
uriBuilder.Port = uri.Port;
7777
var resolvedAddress = uriBuilder.Uri.ToString();
7878
var healthAddress = originalConfig.Health;
7979
if (healthUriBuilder is not null)
8080
{
81-
healthUriBuilder.Host = result.Host;
82-
healthUriBuilder.Port = result.Port;
81+
healthUriBuilder.Host = uri.Host;
82+
healthUriBuilder.Port = uri.Port;
8383
healthAddress = healthUriBuilder.Uri.ToString();
8484
}
8585

@@ -88,6 +88,6 @@ public async ValueTask<ResolvedDestinationCollection> ResolveDestinationsAsync(I
8888
results.Add((name, config));
8989
}
9090

91-
return (results, endPoints.ChangeToken);
91+
return (results, result.ChangeToken);
9292
}
9393
}

src/Microsoft.Extensions.ServiceDiscovery/Configuration/ConfigurationServiceEndPointResolver.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ public ConfigurationServiceEndPointResolver(
4747
public ValueTask DisposeAsync() => default;
4848

4949
/// <inheritdoc/>
50-
public ValueTask ResolveAsync(ServiceEndPointCollectionSource endPoints, CancellationToken cancellationToken)
50+
public ValueTask ResolveAsync(IServiceEndPointBuilder endPoints, CancellationToken cancellationToken)
5151
{
5252
// Only add resolved to the collection if a previous provider (eg, an override) did not add them.
5353
if (endPoints.EndPoints.Count != 0)

src/Microsoft.Extensions.ServiceDiscovery/Http/HttpServiceEndPointResolver.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,7 @@ public ResolverEntry(ServiceEndPointWatcher resolver, IServiceEndPointSelector s
173173
{
174174
if (result.ResolvedSuccessfully)
175175
{
176-
_selector.SetEndPoints(result.EndPoints);
176+
_selector.SetEndPoints(result.EndPointSource);
177177
}
178178
};
179179
}

src/Microsoft.Extensions.ServiceDiscovery/Internal/ServiceEndPointResolverResult.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,9 @@ namespace Microsoft.Extensions.ServiceDiscovery.Internal;
88
/// <summary>
99
/// Represents the result of service endpoint resolution.
1010
/// </summary>
11-
/// <param name="endPoints">The endpoint collection.</param>
11+
/// <param name="endPointSource">The endpoint collection.</param>
1212
/// <param name="exception">The exception which occurred during resolution.</param>
13-
internal sealed class ServiceEndPointResolverResult(ServiceEndPointCollection? endPoints, Exception? exception)
13+
internal sealed class ServiceEndPointResolverResult(ServiceEndPointSource? endPointSource, Exception? exception)
1414
{
1515
/// <summary>
1616
/// Gets the exception which occurred during resolution.
@@ -20,11 +20,11 @@ internal sealed class ServiceEndPointResolverResult(ServiceEndPointCollection? e
2020
/// <summary>
2121
/// Gets a value indicating whether resolution completed successfully.
2222
/// </summary>
23-
[MemberNotNullWhen(true, nameof(EndPoints))]
23+
[MemberNotNullWhen(true, nameof(EndPointSource))]
2424
public bool ResolvedSuccessfully => Exception is null;
2525

2626
/// <summary>
2727
/// Gets the endpoints.
2828
/// </summary>
29-
public ServiceEndPointCollection? EndPoints { get; } = endPoints;
29+
public ServiceEndPointSource? EndPointSource { get; } = endPointSource;
3030
}

src/Microsoft.Extensions.ServiceDiscovery/LoadBalancing/RoundRobinServiceEndPointSelector.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,12 @@ namespace Microsoft.Extensions.ServiceDiscovery.LoadBalancing;
99
internal sealed class RoundRobinServiceEndPointSelector : IServiceEndPointSelector
1010
{
1111
private uint _next;
12-
private ServiceEndPointCollection? _endPoints;
12+
private IReadOnlyList<ServiceEndPoint>? _endPoints;
1313

1414
/// <inheritdoc/>
15-
public void SetEndPoints(ServiceEndPointCollection endPoints)
15+
public void SetEndPoints(ServiceEndPointSource endPoints)
1616
{
17-
_endPoints = endPoints;
17+
_endPoints = endPoints.EndPoints;
1818
}
1919

2020
/// <inheritdoc/>

0 commit comments

Comments
 (0)