Skip to content

Commit 8608532

Browse files
committed
refactor API and improve eventing
1 parent 9974aca commit 8608532

6 files changed

Lines changed: 685 additions & 118 deletions

File tree

src/StackExchange.Redis/ConfigurationOptions.cs

Lines changed: 1 addition & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -117,8 +117,7 @@ internal const string
117117
Tunnel = "tunnel",
118118
SetClientLibrary = "setlib",
119119
Protocol = "protocol",
120-
HighIntegrity = "highIntegrity",
121-
Weight = "weight";
120+
HighIntegrity = "highIntegrity";
122121

123122
private static readonly Dictionary<string, string> normalizedOptions = new[]
124123
{
@@ -150,7 +149,6 @@ internal const string
150149
CheckCertificateRevocation,
151150
Protocol,
152151
HighIntegrity,
153-
Weight,
154152
}.ToDictionary(x => x, StringComparer.OrdinalIgnoreCase);
155153

156154
public static string TryNormalize(string value)
@@ -851,7 +849,6 @@ public static ConfigurationOptions Parse(string configuration, bool ignoreUnknow
851849
heartbeatInterval = heartbeatInterval,
852850
heartbeatConsistencyChecks = heartbeatConsistencyChecks,
853851
highIntegrity = highIntegrity,
854-
Weight = Weight,
855852
};
856853

857854
/// <summary>
@@ -934,11 +931,6 @@ public string ToString(bool includePassword)
934931
Append(sb, OptionKeys.SetClientLibrary, setClientLibrary);
935932
Append(sb, OptionKeys.HighIntegrity, highIntegrity);
936933
Append(sb, OptionKeys.Protocol, FormatProtocol(Protocol));
937-
var weight = Weight;
938-
if (!float.IsNaN(weight))
939-
{
940-
Append(sb, OptionKeys.Weight, weight);
941-
}
942934
if (Tunnel is { IsInbuilt: true } tunnel)
943935
{
944936
Append(sb, OptionKeys.Tunnel, tunnel.ToString());
@@ -995,7 +987,6 @@ private void Clear()
995987
ChannelPrefix = default;
996988
SocketManager = null;
997989
Tunnel = null;
998-
Weight = float.NaN;
999990
}
1000991

1001992
object ICloneable.Clone() => Clone();
@@ -1107,9 +1098,6 @@ private ConfigurationOptions DoParse(string configuration, bool ignoreUnknown)
11071098
case OptionKeys.HighIntegrity:
11081099
HighIntegrity = OptionKeys.ParseBoolean(key, value);
11091100
break;
1110-
case OptionKeys.Weight:
1111-
Weight = OptionKeys.ParseSingle(key, value);
1112-
break;
11131101
case OptionKeys.Tunnel:
11141102
if (value.IsNullOrWhiteSpace())
11151103
{
@@ -1186,11 +1174,6 @@ private ConfigurationOptions DoParse(string configuration, bool ignoreUnknown)
11861174
/// </summary>
11871175
public RedisProtocol? Protocol { get; set; }
11881176

1189-
/// <summary>
1190-
/// Specify the preference of this connection group relative to others.
1191-
/// </summary>
1192-
public float Weight { get; set; } = float.NaN;
1193-
11941177
internal bool TryResp3()
11951178
{
11961179
// note: deliberately leaving the IsAvailable duplicated to use short-circuit
Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
using System;
2+
using System.IO;
3+
using System.Text;
4+
using System.Threading.Tasks;
5+
6+
// ReSharper disable once CheckNamespace
7+
namespace StackExchange.Redis;
8+
9+
/// <summary>
10+
/// A group of connections to redis servers, that manages connections to multiple
11+
/// servers, routing traffic based on the availability of the servers and their
12+
/// relative <see cref="ConnectionGroupMember.Weight"/>.
13+
/// </summary>
14+
public interface IConnectionGroup : IConnectionMultiplexer
15+
{
16+
/// <summary>
17+
/// A change occured to one of the connection groups.
18+
/// </summary>
19+
event EventHandler<GroupConnectionChangedEventArgs>? ConnectionChanged;
20+
21+
/// <summary>
22+
/// Adds a new member to the group.
23+
/// </summary>
24+
Task AddAsync(ConnectionGroupMember group, TextWriter? log = null);
25+
26+
/// <summary>
27+
/// Removes a member from the group.
28+
/// </summary>
29+
bool Remove(ConnectionGroupMember group);
30+
31+
/// <summary>
32+
/// Get the members of the group.
33+
/// </summary>
34+
ReadOnlySpan<ConnectionGroupMember> GetMembers();
35+
}
36+
37+
/// <summary>
38+
/// Represents a change to a connection group.
39+
/// </summary>
40+
public class GroupConnectionChangedEventArgs(GroupConnectionChangedEventArgs.ChangeType type, ConnectionGroupMember group, ConnectionGroupMember? previousGroup = null) : EventArgs, ICompletable
41+
{
42+
/// <summary>
43+
/// The group relating to the change. For <see cref="ChangeType.ActiveChanged"/>, this is the new group.
44+
/// </summary>
45+
public ConnectionGroupMember Group => group;
46+
47+
/// <summary>
48+
/// The previous group relating to the change, if applicable.
49+
/// </summary>
50+
public ConnectionGroupMember? PreviousGroup => previousGroup;
51+
52+
/// <summary>
53+
/// The type of change that occurred.
54+
/// </summary>
55+
public ChangeType Type => type;
56+
57+
private EventHandler<GroupConnectionChangedEventArgs>? _handler;
58+
private object? _sender;
59+
60+
/// <summary>
61+
/// The type of change that occurred.
62+
/// </summary>
63+
public enum ChangeType
64+
{
65+
/// <summary>
66+
/// Unused.
67+
/// </summary>
68+
Unknown = 0,
69+
70+
/// <summary>
71+
/// A new connection group was added.
72+
/// </summary>
73+
Added = 1,
74+
75+
/// <summary>
76+
/// A connection group was removed.
77+
/// </summary>
78+
Removed = 2,
79+
80+
/// <summary>
81+
/// A connection group became disconnected.
82+
/// </summary>
83+
Disconnected = 3,
84+
85+
/// <summary>
86+
/// A connection group became reconnected.
87+
/// </summary>
88+
Reconnected = 4,
89+
90+
/// <summary>
91+
/// The active connection group changed, changing how traffic is routed.
92+
/// </summary>
93+
ActiveChanged = 5,
94+
}
95+
96+
internal void CompleteAsWorker(EventHandler<GroupConnectionChangedEventArgs> handler, object sender)
97+
{
98+
_handler = handler;
99+
_sender = sender;
100+
ConnectionMultiplexer.CompleteAsWorker(this);
101+
}
102+
103+
void ICompletable.AppendStormLog(StringBuilder sb) { }
104+
105+
bool ICompletable.TryComplete(bool isAsync) => ConnectionMultiplexer.TryCompleteHandler(_handler, _sender!, this, isAsync);
106+
}

0 commit comments

Comments
 (0)