Skip to content

Implement GetServer(RedisKey, ...) #2936

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

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions docs/ReleaseNotes.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ Current package versions:
- Add `Condition.SortedSet[Not]ContainsStarting` condition for transactions ([#2638 by ArnoKoll](https://github.com/StackExchange/StackExchange.Redis/pull/2638))
- Add support for XPENDING Idle time filter ([#2822 by david-brink-talogy](https://github.com/StackExchange/StackExchange.Redis/pull/2822))
- Improve `double` formatting performance on net8+ ([#2928 by mgravell](https://github.com/StackExchange/StackExchange.Redis/pull/2928))
- Add `GetServer(RedisKey, ...)` API ([#2936 by mgravell](https://github.com/StackExchange/StackExchange.Redis/pull/2936))

## 2.8.58

Expand Down
20 changes: 17 additions & 3 deletions src/StackExchange.Redis/ConnectionMultiplexer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,7 @@ internal async Task MakePrimaryAsync(ServerEndPoint server, ReplicationChangeOpt
{
throw ExceptionFactory.AdminModeNotEnabled(RawConfig.IncludeDetailInExceptions, cmd, null, server);
}
var srv = new RedisServer(this, server, null);
var srv = server.GetRedisServer(null);
if (!srv.IsConnected)
{
throw ExceptionFactory.NoConnectionAvailable(this, null, server, GetServerSnapshot(), command: cmd);
Expand Down Expand Up @@ -1229,7 +1229,21 @@ public IServer GetServer(EndPoint? endpoint, object? asyncState = null)
throw new NotSupportedException($"The server API is not available via {RawConfig.Proxy}");
}
var server = servers[endpoint] as ServerEndPoint ?? throw new ArgumentException("The specified endpoint is not defined", nameof(endpoint));
return new RedisServer(this, server, asyncState);
return server.GetRedisServer(asyncState);
}

/// <inheritdoc cref="IConnectionMultiplexer.GetServer(RedisKey, object, CommandFlags)"/>
#pragma warning disable RS0026
public IServer GetServer(RedisKey key, object? asyncState = null, CommandFlags flags = CommandFlags.None)
#pragma warning restore RS0026
{
// We'll spoof the GET command for this; we're not supporting ad-hoc access to the pub/sub channel, because: bad things.
// Any read-only-replica vs writable-primary concerns should be managed by the caller via "flags"; the default is PreferPrimary.
// Note that ServerSelectionStrategy treats "null" (default) keys as NoSlot, aka Any.
return (SelectServer(RedisCommand.GET, flags, key) ?? Throw()).GetRedisServer(asyncState);

[DoesNotReturn]
static ServerEndPoint Throw() => throw new InvalidOperationException("It was not possible to resolve a connection to the server owning the specified key");
}

/// <summary>
Expand All @@ -1241,7 +1255,7 @@ public IServer[] GetServers()
var result = new IServer[snapshot.Length];
for (var i = 0; i < snapshot.Length; i++)
{
result[i] = new RedisServer(this, snapshot[i], null);
result[i] = snapshot[i].GetRedisServer(null);
}
return result;
}
Expand Down
Loading
Loading