Skip to content

feat: Add Mode() Method to rueidis.Client for Detecting Client Mode #794

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

Merged
merged 8 commits into from
Mar 7, 2025

Conversation

CheyuWu
Copy link
Contributor

@CheyuWu CheyuWu commented Mar 6, 2025

Why do we need this?

Previously, Centrifugo required explicit Redis Cluster configuration because the Centrifuge library generates keys differently in cluster mode (using hash tags). However, this approach is inconvenient because cloud providers typically provide a Redis URL (e.g., redis://...) without explicitly indicating whether it is a standalone Redis or a Redis Cluster. This prevents users from simply reusing the endpoint when starting Centrifugo.

New Interface Definition

type ClientMode string

const (
	ClientModeCluster    ClientMode = "cluster"
	ClientModeSentinel   ClientMode = "sentinel"
	ClientModeStandalone ClientMode = "standalone"
)

type Client interface {
    ...
    Mode() ClientMode
}

Closes #790

rueidis.go Outdated
// https://github.com/valkey-io/valkey/blob/1a34a4ff7f101bb6b17a0b5e9aa3bf7d6bd29f68/src/networking.c#L4118-L4124
ModeCluster ClientMode = "cluster"
ModeSentinel ClientMode = "sentinel"
ModeStandalone ClientMode = "standalone"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should be with Client prefix:

ClientModeCluster ClientMode = "cluster"
ClientModeSentinel ClientMode = "sentinel"
ClientModeStandalone ClientMode = "standalone"

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oops, I will fix this

@CheyuWu
Copy link
Contributor Author

CheyuWu commented Mar 6, 2025

also cc @rueian

client.go Outdated
@@ -189,6 +190,10 @@ func (c *singleClient) Nodes() map[string]Client {
return map[string]Client{c.conn.Addr(): c}
}

func (c *singleClient) Mode() ClientMode {
return c.mode
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
return c.mode
return ClientModeStandalone

client.go Outdated
@@ -16,6 +16,7 @@ type singleClient struct {
cmd Builder
retry bool
DisableCache bool
mode ClientMode
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
mode ClientMode

cluster.go Outdated
@@ -57,6 +58,7 @@ func newClusterClient(opt *ClientOption, connFn connFn, retryer retryHandler) (*
retry: !opt.DisableRetry,
retryHandler: retryer,
stopCh: make(chan struct{}),
mode: ClientModeCluster,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
mode: ClientModeCluster,

cluster.go Outdated
@@ -1203,6 +1205,10 @@ func (c *clusterClient) Nodes() map[string]Client {
return _nodes
}

func (c *clusterClient) Mode() ClientMode {
return c.mode
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
return c.mode
return ClientModeCluster

sentinel.go Outdated
@@ -26,6 +26,7 @@ func newSentinelClient(opt *ClientOption, connFn connFn, retryer retryHandler) (
retry: !opt.DisableRetry,
retryHandler: retryer,
replica: opt.ReplicaOnly,
mode: ClientModeSentinel,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
mode: ClientModeSentinel,

sentinel.go Outdated
@@ -56,6 +57,7 @@ type sentinelClient struct {
cmd Builder
retry bool
replica bool
mode ClientMode
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
mode ClientMode

sentinel.go Outdated
@@ -218,6 +220,10 @@ func (c *sentinelClient) Nodes() map[string]Client {
return map[string]Client{conn.Addr(): newSingleClientWithConn(conn, c.cmd, c.retry, disableCache, c.retryHandler)}
}

func (c *sentinelClient) Mode() ClientMode {
return c.mode
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
return c.mode
return ClientModeSentinel

@CheyuWu CheyuWu force-pushed the feat/client/mode branch from 1ad61f9 to 6d9ed43 Compare March 6, 2025 16:37
@CheyuWu
Copy link
Contributor Author

CheyuWu commented Mar 6, 2025

Hi @rueian, I have solved those issues, PTAL

@rueian
Copy link
Collaborator

rueian commented Mar 6, 2025

Hi @rueian, I have solved those issues, PTAL

Hi @CheyuWu, LGTM! Could you add simple tests for these Mode() as well? I'd like to keep our test coverage from dropping.

@CheyuWu
Copy link
Contributor Author

CheyuWu commented Mar 7, 2025

Hi @rueian, I have solved those issues, PTAL

Hi @CheyuWu, LGTM! Could you add simple tests for these Mode() as well? I'd like to keep our test coverage from dropping.

Hi @rueian, I have added the unit test. If there is any additional test that I should add, please let me know.

@rueian rueian merged commit 714ea64 into redis:main Mar 7, 2025
27 checks passed
@rueian
Copy link
Collaborator

rueian commented Mar 7, 2025

Merged. Thanks @CheyuWu!

@FZambia
Copy link
Contributor

FZambia commented Mar 7, 2025

Many thanks for implementing this @CheyuWu @rueian ! ❤️

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.

A way to understand whether Client is a clusterClient
3 participants