-
Notifications
You must be signed in to change notification settings - Fork 819
Change subchannel BalancerAddress when attributes change #2228
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -38,20 +38,22 @@ public sealed class BalancerAttributes : IDictionary<string, object?>, IReadOnly | |
| /// <summary> | ||
| /// Gets a read-only collection of metadata attributes. | ||
| /// </summary> | ||
| public static readonly BalancerAttributes Empty = new BalancerAttributes(new ReadOnlyDictionary<string, object?>(new Dictionary<string, object?>())); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. you cite performance as the reason for this change; I would be genuinely surprized if
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The performance gain isn't Using |
||
| public static readonly BalancerAttributes Empty = new BalancerAttributes(new Dictionary<string, object?>(), readOnly: true); | ||
|
|
||
| private readonly IDictionary<string, object?> _attributes; | ||
| private readonly Dictionary<string, object?> _attributes; | ||
| private readonly bool _readOnly; | ||
|
|
||
| /// <summary> | ||
| /// Initializes a new instance of the <see cref="BalancerAttributes"/> class. | ||
| /// </summary> | ||
| public BalancerAttributes() : this(new Dictionary<string, object?>()) | ||
| public BalancerAttributes() : this(new Dictionary<string, object?>(), readOnly: false) | ||
| { | ||
| } | ||
|
|
||
| private BalancerAttributes(IDictionary<string, object?> attributes) | ||
| private BalancerAttributes(Dictionary<string, object?> attributes, bool readOnly) | ||
| { | ||
| _attributes = attributes; | ||
| _readOnly = readOnly; | ||
| } | ||
|
|
||
| object? IDictionary<string, object?>.this[string key] | ||
|
|
@@ -62,28 +64,49 @@ private BalancerAttributes(IDictionary<string, object?> attributes) | |
| } | ||
| set | ||
| { | ||
| ValidateReadOnly(); | ||
| _attributes[key] = value; | ||
| } | ||
| } | ||
|
|
||
| ICollection<string> IDictionary<string, object?>.Keys => _attributes.Keys; | ||
| ICollection<object?> IDictionary<string, object?>.Values => _attributes.Values; | ||
| int ICollection<KeyValuePair<string, object?>>.Count => _attributes.Count; | ||
| bool ICollection<KeyValuePair<string, object?>>.IsReadOnly => _attributes.IsReadOnly; | ||
| bool ICollection<KeyValuePair<string, object?>>.IsReadOnly => _readOnly || ((ICollection<KeyValuePair<string, object?>>)_attributes).IsReadOnly; | ||
| IEnumerable<string> IReadOnlyDictionary<string, object?>.Keys => _attributes.Keys; | ||
| IEnumerable<object?> IReadOnlyDictionary<string, object?>.Values => _attributes.Values; | ||
| int IReadOnlyCollection<KeyValuePair<string, object?>>.Count => _attributes.Count; | ||
| object? IReadOnlyDictionary<string, object?>.this[string key] => _attributes[key]; | ||
| void IDictionary<string, object?>.Add(string key, object? value) => _attributes.Add(key, value); | ||
| void ICollection<KeyValuePair<string, object?>>.Add(KeyValuePair<string, object?> item) => _attributes.Add(item); | ||
| void ICollection<KeyValuePair<string, object?>>.Clear() => _attributes.Clear(); | ||
| void IDictionary<string, object?>.Add(string key, object? value) | ||
| { | ||
| ValidateReadOnly(); | ||
| _attributes.Add(key, value); | ||
| } | ||
| void ICollection<KeyValuePair<string, object?>>.Add(KeyValuePair<string, object?> item) | ||
| { | ||
| ValidateReadOnly(); | ||
| ((ICollection<KeyValuePair<string, object?>>)_attributes).Add(item); | ||
| } | ||
| void ICollection<KeyValuePair<string, object?>>.Clear() | ||
| { | ||
| ValidateReadOnly(); | ||
| _attributes.Clear(); | ||
| } | ||
| bool ICollection<KeyValuePair<string, object?>>.Contains(KeyValuePair<string, object?> item) => _attributes.Contains(item); | ||
| bool IDictionary<string, object?>.ContainsKey(string key) => _attributes.ContainsKey(key); | ||
| void ICollection<KeyValuePair<string, object?>>.CopyTo(KeyValuePair<string, object?>[] array, int arrayIndex) => _attributes.CopyTo(array, arrayIndex); | ||
| void ICollection<KeyValuePair<string, object?>>.CopyTo(KeyValuePair<string, object?>[] array, int arrayIndex) => ((ICollection<KeyValuePair<string, object?>>)_attributes).CopyTo(array, arrayIndex); | ||
| IEnumerator<KeyValuePair<string, object?>> IEnumerable<KeyValuePair<string, object?>>.GetEnumerator() => _attributes.GetEnumerator(); | ||
| IEnumerator System.Collections.IEnumerable.GetEnumerator() => ((System.Collections.IEnumerable)_attributes).GetEnumerator(); | ||
| bool IDictionary<string, object?>.Remove(string key) => _attributes.Remove(key); | ||
| bool ICollection<KeyValuePair<string, object?>>.Remove(KeyValuePair<string, object?> item) => _attributes.Remove(item); | ||
| bool IDictionary<string, object?>.Remove(string key) | ||
| { | ||
| ValidateReadOnly(); | ||
| return _attributes.Remove(key); | ||
| } | ||
| bool ICollection<KeyValuePair<string, object?>>.Remove(KeyValuePair<string, object?> item) | ||
| { | ||
| ValidateReadOnly(); | ||
| return ((ICollection<KeyValuePair<string, object?>>)_attributes).Remove(item); | ||
| } | ||
| bool IDictionary<string, object?>.TryGetValue(string key, out object? value) => _attributes.TryGetValue(key, out value); | ||
| bool IReadOnlyDictionary<string, object?>.ContainsKey(string key) => _attributes.ContainsKey(key); | ||
| bool IReadOnlyDictionary<string, object?>.TryGetValue(string key, out object? value) => _attributes.TryGetValue(key, out value); | ||
|
|
@@ -121,6 +144,7 @@ public bool TryGetValue<TValue>(BalancerAttributesKey<TValue> key, [MaybeNullWhe | |
| /// <param name="value">The value.</param> | ||
| public void Set<TValue>(BalancerAttributesKey<TValue> key, TValue value) | ||
| { | ||
| ValidateReadOnly(); | ||
| _attributes[key.Key] = value; | ||
| } | ||
|
|
||
|
|
@@ -135,10 +159,55 @@ public void Set<TValue>(BalancerAttributesKey<TValue> key, TValue value) | |
| /// </returns> | ||
| public bool Remove<TValue>(BalancerAttributesKey<TValue> key) | ||
| { | ||
| ValidateReadOnly(); | ||
| return _attributes.Remove(key.Key); | ||
| } | ||
|
|
||
| internal string DebuggerToString() | ||
| private void ValidateReadOnly() | ||
| { | ||
| if (_readOnly) | ||
| { | ||
| throw new NotSupportedException("Collection is read-only."); | ||
| } | ||
| } | ||
|
|
||
| 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)) | ||
JamesNK marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| { | ||
| return false; | ||
| } | ||
|
|
||
| if (!Equals(kvp.Value, value)) | ||
| { | ||
| return false; | ||
| } | ||
| } | ||
|
|
||
| return true; | ||
| } | ||
|
|
||
| private string DebuggerToString() | ||
| { | ||
| return $"Count = {_attributes.Count}"; | ||
| } | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.