Skip to content

Expose HashSlot(RedisChannel) on IConnectionMultiplexer - #3366

Open
aradalvand wants to merge 1 commit into
seredis:mainfrom
aradalvand:feat/hash-slot-of-channel
Open

aradalvand wants to merge 1 commit into
seredis:mainfrom
aradalvand:feat/hash-slot-of-channel

Conversation

@aradalvand

@aradalvand aradalvand commented Nov 19, 2026 •

Copy link
Copy Markdown

Fixes #3365.

There was no public way to ask which hash slot a RedisChannel is routed by. The usual workaround is HashSlot(new RedisKey(channel)), which hashes the channel's name as a key. It gets two cases wrong: a configured ChannelPrefix, and single-key keyspace channels, which are routed by their key. This exposes the existing ServerSelectionStrategy.HashSlot(in RedisChannel), which is what routing actually uses and handles both.

Changes

  • New IConnectionMultiplexer.HashSlot(RedisChannel), implemented by ConnectionMultiplexer (delegating to ServerSelectionStrategy.HashSlot(in RedisChannel)). Also forwarded by MultiGroupMultiplexer and the test fixture's NonDisposingConnection.
  • [OverloadResolutionPriority(1)] on the existing HashSlot(RedisKey), on both the interface and ConnectionMultiplexer. string and byte[] convert implicitly to both RedisKey and RedisChannel; the channel conversions are [Obsolete], but that doesn't remove them from overload resolution. Without the attribute, HashSlot("key") becomes ambiguous (CS0121). I checked this: removing it breaks existing calls in ClusterTests and HotKeysTests. This follows the precedent of RedisChannel.KeySpacePrefix, including the framework shim for older TFMs.
  • PublicAPI.Unshipped.txt entries.

Things worth a reviewer's eye

  • A new interface member. This adds a member to IConnectionMultiplexer, which is a break for anyone implementing it outside the library. Happy to change the shape (e.g. an extension method) if you'd rather avoid that.
  • The priority attribute on an existing member. Adding it doesn't change any signature, so it isn't a binary break. Compilers that don't understand the attribute would still see the ambiguity for string/byte[] arguments.
  • Non-sharded, non-key-routed channels. Like the internal method, this returns the slot of the (prefixed) name even for channels that aren't routed by slot. As with HashSlot(RedisKey), it returns -1 when not connected to a cluster.

Testing

  • New ChannelHashSlotUnitTests, against the in-process server in cluster mode:
    • a sharded channel has the same slot as the same-named key;
    • with a ChannelPrefix, its slot is the prefixed name's;
    • a KeySpaceSingleKey channel has its key's slot;
    • standalone returns -1;
    • string and byte[] arguments still bind to the key overload, both through ConnectionMultiplexer and through the interface.
  • Builds for all of StackExchange.Redis's TFMs with no warnings.
  • Existing tests: all *UnitTests classes pass (1225, including the 5 new ones) on net10.0.

Checklist

  • I fully and freely contribute this code in accordance with the project license (and am legally able to do so)
  • I take responsibility for this contribution's quality and correctness, including any portions produced with AI assistance (see CONTRIBUTING.md).

There was no public way to ask which hash slot a channel is routed by. The
usual workaround, hashing the channel's name as a key, ignores
ConfigurationOptions.ChannelPrefix, and the key-based routing of single-key
keyspace channels. This exposes the existing internal
ServerSelectionStrategy.HashSlot(in RedisChannel), which accounts for both.

Strings and byte arrays convert implicitly to both RedisKey and RedisChannel,
so the existing HashSlot(RedisKey) gets [OverloadResolutionPriority(1)] to
keep calls like HashSlot("key") compiling, and binding to it.

Fixes seredis#3365.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01XUNs4qcAQaGCRpkwmr8LnY

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Expose HashSlot(RedisChannel) on IConnectionMultiplexer

1 participant