Skip to content

Commit db9a001

Browse files
committed
Refactor transport to use endpoint instead of BalancerAddress in logging
1 parent 6d23e73 commit db9a001

File tree

9 files changed

+120
-111
lines changed

9 files changed

+120
-111
lines changed

src/Grpc.Net.Client/Balancer/BalancerAddress.cs

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ namespace Grpc.Net.Client.Balancer;
3030
/// </summary>
3131
public sealed class BalancerAddress
3232
{
33+
// Internal so address attributes can be compared without allocating an empty collection.
3334
internal BalancerAttributes? _attributes;
3435

3536
/// <summary>
@@ -48,7 +49,7 @@ public BalancerAddress(DnsEndPoint endPoint)
4849
/// <param name="host">The host.</param>
4950
/// <param name="port">The port.</param>
5051
[DebuggerStepThrough]
51-
public BalancerAddress(string host, int port) : this(new DnsEndPoint(host, port))
52+
public BalancerAddress(string host, int port) : this(new BalancerEndPoint(host, port))
5253
{
5354
}
5455

@@ -69,5 +70,21 @@ public override string ToString()
6970
{
7071
return $"{EndPoint.Host}:{EndPoint.Port}";
7172
}
73+
74+
private sealed class BalancerEndPoint : DnsEndPoint
75+
{
76+
private string? _cachedToString;
77+
78+
public BalancerEndPoint(string host, int port) : base(host, port)
79+
{
80+
}
81+
82+
public override string ToString()
83+
{
84+
// Improve ToString performance when logging by caching ToString.
85+
// Don't include DnsEndPoint address family.
86+
return _cachedToString ??= $"{Host}:{Port}";
87+
}
88+
}
7289
}
7390
#endif

src/Grpc.Net.Client/Balancer/Internal/BalancerHttpHandler.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ internal async ValueTask<Stream> OnConnect(SocketsHttpConnectionContext context,
9494
}
9595

9696
Debug.Assert(context.DnsEndPoint.Equals(currentAddress.EndPoint), "Context endpoint should equal address endpoint.");
97-
return await subchannel.Transport.GetStreamAsync(currentAddress, cancellationToken).ConfigureAwait(false);
97+
return await subchannel.Transport.GetStreamAsync(currentAddress.EndPoint, cancellationToken).ConfigureAwait(false);
9898
}
9999
#endif
100100

src/Grpc.Net.Client/Balancer/Internal/ISubchannelTransport.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#region Copyright notice and license
1+
#region Copyright notice and license
22

33
// Copyright 2019 The gRPC Authors
44
//
@@ -33,7 +33,7 @@ internal interface ISubchannelTransport : IDisposable
3333
TimeSpan? ConnectTimeout { get; }
3434
TransportStatus TransportStatus { get; }
3535

36-
ValueTask<Stream> GetStreamAsync(BalancerAddress address, CancellationToken cancellationToken);
36+
ValueTask<Stream> GetStreamAsync(DnsEndPoint endPoint, CancellationToken cancellationToken);
3737
ValueTask<ConnectResult> TryConnectAsync(ConnectContext context);
3838

3939
void Disconnect();

src/Grpc.Net.Client/Balancer/Internal/PassiveSubchannelTransport.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ public void Dispose()
7171
_currentEndPoint = null;
7272
}
7373

74-
public ValueTask<Stream> GetStreamAsync(BalancerAddress address, CancellationToken cancellationToken)
74+
public ValueTask<Stream> GetStreamAsync(DnsEndPoint endPoint, CancellationToken cancellationToken)
7575
{
7676
throw new NotSupportedException();
7777
}

src/Grpc.Net.Client/Balancer/Internal/SocketConnectivitySubchannelTransport.cs

Lines changed: 87 additions & 95 deletions
Large diffs are not rendered by default.

test/FunctionalTests/Balancer/ConnectionTests.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -346,7 +346,7 @@ await TestHelpers.AssertIsTrueRetryAsync(() =>
346346
}, "Wait for connections to start.");
347347
foreach (var t in activeStreams)
348348
{
349-
Assert.AreEqual(new DnsEndPoint("127.0.0.1", 50051), t.Address.EndPoint);
349+
Assert.AreEqual(new DnsEndPoint("127.0.0.1", 50051), t.EndPoint);
350350
}
351351

352352
// Act
@@ -367,7 +367,7 @@ await TestHelpers.AssertIsTrueRetryAsync(() =>
367367
activeStreams = transport.GetActiveStreams();
368368
return activeStreams.Count == 11;
369369
}, "Wait for connections to start.");
370-
Assert.AreEqual(new DnsEndPoint("127.0.0.1", 50051), activeStreams[activeStreams.Count - 1].Address.EndPoint);
370+
Assert.AreEqual(new DnsEndPoint("127.0.0.1", 50051), activeStreams[activeStreams.Count - 1].EndPoint);
371371

372372
tcs.SetResult(null);
373373

@@ -407,7 +407,7 @@ await TestHelpers.AssertIsTrueRetryAsync(() =>
407407

408408
activeStreams = transport.GetActiveStreams();
409409
Assert.AreEqual(1, activeStreams.Count);
410-
Assert.AreEqual(new DnsEndPoint("127.0.0.1", 50052), activeStreams[0].Address.EndPoint);
410+
Assert.AreEqual(new DnsEndPoint("127.0.0.1", 50052), activeStreams[0].EndPoint);
411411
}
412412

413413
#if NET7_0_OR_GREATER

test/FunctionalTests/Balancer/PickFirstBalancerTests.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -371,7 +371,7 @@ async Task<HelloReply> UnaryMethod(HelloRequest request, ServerCallContext conte
371371
Assert.GreaterOrEqual(activeStreams.Count, 2);
372372
foreach (var stream in activeStreams)
373373
{
374-
Assert.AreEqual(new DnsEndPoint("127.0.0.1", 50051), stream.Address.EndPoint);
374+
Assert.AreEqual(new DnsEndPoint("127.0.0.1", 50051), stream.EndPoint);
375375
}
376376

377377
tcs.SetResult(null);
@@ -385,7 +385,7 @@ async Task<HelloReply> UnaryMethod(HelloRequest request, ServerCallContext conte
385385
await TestHelpers.AssertIsTrueRetryAsync(() =>
386386
{
387387
activeStreams = transport.GetActiveStreams();
388-
Logger.LogInformation($"Current active stream addresses: {string.Join(", ", activeStreams.Select(s => s.Address))}");
388+
Logger.LogInformation($"Current active stream addresses: {string.Join(", ", activeStreams.Select(s => s.EndPoint))}");
389389
return activeStreams.Count == 0;
390390
}, "Active streams removed.", Logger).DefaultTimeout();
391391

@@ -395,7 +395,7 @@ await TestHelpers.AssertIsTrueRetryAsync(() =>
395395

396396
activeStreams = transport.GetActiveStreams();
397397
Assert.AreEqual(1, activeStreams.Count);
398-
Assert.AreEqual(new DnsEndPoint("127.0.0.1", 50052), activeStreams[0].Address.EndPoint);
398+
Assert.AreEqual(new DnsEndPoint("127.0.0.1", 50052), activeStreams[0].EndPoint);
399399
}
400400

401401
[Test]

test/FunctionalTests/Balancer/RoundRobinBalancerTests.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -381,8 +381,8 @@ Task<HelloReply> UnaryMethod(HelloRequest request, ServerCallContext context)
381381

382382
var activeStreams = ((SocketConnectivitySubchannelTransport)disposedSubchannel.Transport).GetActiveStreams();
383383
Assert.AreEqual(1, activeStreams.Count);
384-
Assert.AreEqual("127.0.0.1", activeStreams[0].Address.EndPoint.Host);
385-
Assert.AreEqual(50051, activeStreams[0].Address.EndPoint.Port);
384+
Assert.AreEqual("127.0.0.1", activeStreams[0].EndPoint.Host);
385+
Assert.AreEqual(50051, activeStreams[0].EndPoint.Port);
386386

387387
// Wait until connected to new endpoint
388388
Subchannel? newSubchannel = null;
@@ -415,8 +415,8 @@ await TestHelpers.AssertIsTrueRetryAsync(() =>
415415
// New subchannel stream created with request.
416416
activeStreams = ((SocketConnectivitySubchannelTransport)newSubchannel.Transport).GetActiveStreams();
417417
Assert.AreEqual(1, activeStreams.Count);
418-
Assert.AreEqual("127.0.0.1", activeStreams[0].Address.EndPoint.Host);
419-
Assert.AreEqual(50052, activeStreams[0].Address.EndPoint.Port);
418+
Assert.AreEqual("127.0.0.1", activeStreams[0].EndPoint.Host);
419+
Assert.AreEqual(50052, activeStreams[0].EndPoint.Port);
420420
Assert.IsNull(((SocketConnectivitySubchannelTransport)disposedSubchannel.Transport)._initialSocket);
421421
}
422422

test/Grpc.Net.Client.Tests/Infrastructure/Balancer/TestSubChannelTransport.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ public void Dispose()
6262
{
6363
}
6464

65-
public ValueTask<Stream> GetStreamAsync(BalancerAddress address, CancellationToken cancellationToken)
65+
public ValueTask<Stream> GetStreamAsync(DnsEndPoint endPoint, CancellationToken cancellationToken)
6666
{
6767
return new ValueTask<Stream>(new MemoryStream());
6868
}

0 commit comments

Comments
 (0)