Skip to content

Commit 4d2da7a

Browse files
authored
Implement GetServer(RedisKey, ...) (#2936)
* - implement GetServer API from #2932, and tests - implement memoization on GetServer when asyncState is null - fixup NotImplementedException in dummy places * release notes and shipped * remove params overload * remove params overload from muxer impl * test stability: allow both embstr or raw for string encoding * remove pragma * Additional intellisense comments to clarify usage.
1 parent b03e5f7 commit 4d2da7a

File tree

10 files changed

+528
-307
lines changed

10 files changed

+528
-307
lines changed

docs/ReleaseNotes.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ Current package versions:
1313
- Add `Condition.SortedSet[Not]ContainsStarting` condition for transactions ([#2638 by ArnoKoll](https://github.com/StackExchange/StackExchange.Redis/pull/2638))
1414
- Add support for XPENDING Idle time filter ([#2822 by david-brink-talogy](https://github.com/StackExchange/StackExchange.Redis/pull/2822))
1515
- Improve `double` formatting performance on net8+ ([#2928 by mgravell](https://github.com/StackExchange/StackExchange.Redis/pull/2928))
16+
- Add `GetServer(RedisKey, ...)` API ([#2936 by mgravell](https://github.com/StackExchange/StackExchange.Redis/pull/2936))
1617
- Fix error constructing `StreamAdd` message ([#2941 by mgravell](https://github.com/StackExchange/StackExchange.Redis/pull/2941))
1718

1819
## 2.8.58

src/StackExchange.Redis/ConnectionMultiplexer.cs

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -210,7 +210,7 @@ internal async Task MakePrimaryAsync(ServerEndPoint server, ReplicationChangeOpt
210210
{
211211
throw ExceptionFactory.AdminModeNotEnabled(RawConfig.IncludeDetailInExceptions, cmd, null, server);
212212
}
213-
var srv = new RedisServer(this, server, null);
213+
var srv = server.GetRedisServer(null);
214214
if (!srv.IsConnected)
215215
{
216216
throw ExceptionFactory.NoConnectionAvailable(this, null, server, GetServerSnapshot(), command: cmd);
@@ -1229,7 +1229,21 @@ public IServer GetServer(EndPoint? endpoint, object? asyncState = null)
12291229
throw new NotSupportedException($"The server API is not available via {RawConfig.Proxy}");
12301230
}
12311231
var server = servers[endpoint] as ServerEndPoint ?? throw new ArgumentException("The specified endpoint is not defined", nameof(endpoint));
1232-
return new RedisServer(this, server, asyncState);
1232+
return server.GetRedisServer(asyncState);
1233+
}
1234+
1235+
/// <inheritdoc cref="IConnectionMultiplexer.GetServer(RedisKey, object, CommandFlags)"/>
1236+
#pragma warning disable RS0026
1237+
public IServer GetServer(RedisKey key, object? asyncState = null, CommandFlags flags = CommandFlags.None)
1238+
#pragma warning restore RS0026
1239+
{
1240+
// We'll spoof the GET command for this; we're not supporting ad-hoc access to the pub/sub channel, because: bad things.
1241+
// Any read-only-replica vs writable-primary concerns should be managed by the caller via "flags"; the default is PreferPrimary.
1242+
// Note that ServerSelectionStrategy treats "null" (default) keys as NoSlot, aka Any.
1243+
return (SelectServer(RedisCommand.GET, flags, key) ?? Throw()).GetRedisServer(asyncState);
1244+
1245+
[DoesNotReturn]
1246+
static ServerEndPoint Throw() => throw new InvalidOperationException("It was not possible to resolve a connection to the server owning the specified key");
12331247
}
12341248

12351249
/// <summary>
@@ -1241,7 +1255,7 @@ public IServer[] GetServers()
12411255
var result = new IServer[snapshot.Length];
12421256
for (var i = 0; i < snapshot.Length; i++)
12431257
{
1244-
result[i] = new RedisServer(this, snapshot[i], null);
1258+
result[i] = snapshot[i].GetRedisServer(null);
12451259
}
12461260
return result;
12471261
}

0 commit comments

Comments
 (0)