Skip to content

Commit

Permalink
PR feedback
Browse files Browse the repository at this point in the history
  • Loading branch information
JamesNK committed Aug 24, 2023
1 parent 0e9185e commit ff270fc
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 44 deletions.
38 changes: 37 additions & 1 deletion src/Grpc.Net.Client/Balancer/BalancerAttributes.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public sealed class BalancerAttributes : IDictionary<string, object?>, IReadOnly
/// </summary>
public static readonly BalancerAttributes Empty = new BalancerAttributes(new Dictionary<string, object?>(), readOnly: true);

internal readonly Dictionary<string, object?> _attributes;
private readonly Dictionary<string, object?> _attributes;
private readonly bool _readOnly;

/// <summary>
Expand Down Expand Up @@ -171,6 +171,42 @@ private void ValidateReadOnly()
}
}

internal static bool DeepEquals(BalancerAttributes? x, BalancerAttributes? y)
{
var xValues = x?._attributes;
var yValues = y?._attributes;

if (ReferenceEquals(xValues, yValues))
{
return true;
}

if (xValues == null || yValues == null)
{
return false;
}

if (xValues.Count != yValues.Count)
{
return false;
}

foreach (var kvp in xValues)
{
if (!yValues.TryGetValue(kvp.Key, out var value))
{
return false;
}

if (!Equals(kvp.Value, value))
{
return false;
}
}

return true;
}

private string DebuggerToString()
{
return $"Count = {_attributes.Count}";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@
#endregion

#if SUPPORT_LOAD_BALANCING
using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;

namespace Grpc.Net.Client.Balancer.Internal;
Expand All @@ -44,47 +42,7 @@ public bool Equals(BalancerAddress? x, BalancerAddress? y)
return false;
}

var xAttributes = x._attributes?._attributes;
var yAttributes = y._attributes?._attributes;
if (!AttributesEqual(xAttributes, yAttributes))
{
return false;
}

return true;
}

private bool AttributesEqual(Dictionary<string, object?>? x, Dictionary<string, object?>? y)
{
if (x == null && y == null)
{
return true;
}

if (x == null || y == null)
{
return false;
}

if (x.Count != y.Count)
{
return false;
}

foreach (var kvp in x)
{
if (!y.TryGetValue(kvp.Key, out var value))
{
return false;
}

if (!Equals(kvp.Value, value))
{
return false;
}
}

return true;
return BalancerAttributes.DeepEquals(x._attributes, y._attributes);
}

public int GetHashCode([DisallowNull] BalancerAddress obj)
Expand Down
1 change: 1 addition & 0 deletions src/Grpc.Net.Client/Balancer/Subchannel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
#endregion

#if SUPPORT_LOAD_BALANCING
using System.Net;
using Grpc.Core;
using Grpc.Net.Client.Balancer.Internal;
using Microsoft.Extensions.Logging;
Expand Down

0 comments on commit ff270fc

Please sign in to comment.