Skip to content

Be able to create a redis ring from universal options #3012

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

Closed
Closed
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
11 changes: 11 additions & 0 deletions example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -678,6 +678,17 @@ func ExampleNewUniversalClient_cluster() {
rdb.Ping(ctx)
}

func ExampleNewUniversalClient_ring() {
rdb := redis.NewUniversalClient(&redis.UniversalOptions{
AddressMap: map[string]string{
"shard1": ":7000",
"shard2": ":7001",
"shard3": ":7002",
},
})
rdb.Ping(ctx)
}

func ExampleClient_SlowLogGet() {
if RECluster {
// skip slowlog test for cluster
Expand Down
50 changes: 49 additions & 1 deletion universal.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ type UniversalOptions struct {
// of cluster/sentinel nodes.
Addrs []string

// Map of name => host:port addresses of ring shards.
AddressMap map[string]string

// ClientName will execute the `CLIENT SETNAME ClientName` command for each conn.
ClientName string

Expand Down Expand Up @@ -165,6 +168,47 @@ func (o *UniversalOptions) Failover() *FailoverOptions {
}
}

// Ring returns ring options created from the universal options.
func (o *UniversalOptions) Ring() *RingOptions {
if len(o.Addrs) == 0 {
o.Addrs = []string{"127.0.0.1:26379"}
}

return &RingOptions{
Addrs: o.AddressMap,
ClientName: o.ClientName,

Dialer: o.Dialer,
OnConnect: o.OnConnect,

DB: o.DB,
Protocol: o.Protocol,
Username: o.Username,
Password: o.Password,

MaxRetries: o.MaxRetries,
MinRetryBackoff: o.MinRetryBackoff,
MaxRetryBackoff: o.MaxRetryBackoff,

DialTimeout: o.DialTimeout,
ReadTimeout: o.ReadTimeout,
WriteTimeout: o.WriteTimeout,
ContextTimeoutEnabled: o.ContextTimeoutEnabled,

PoolFIFO: o.PoolFIFO,
PoolSize: o.PoolSize,
PoolTimeout: o.PoolTimeout,
MinIdleConns: o.MinIdleConns,
MaxIdleConns: o.MaxIdleConns,
ConnMaxIdleTime: o.ConnMaxIdleTime,
ConnMaxLifetime: o.ConnMaxLifetime,

TLSConfig: o.TLSConfig,

// DisableIndentity: o.DisableIndentity, // field does not exist yet, see PR 2726
}
}

// Simple returns basic options created from the universal options.
func (o *UniversalOptions) Simple() *Options {
addr := "127.0.0.1:6379"
Expand Down Expand Up @@ -239,12 +283,16 @@ var (
//
// 1. If the MasterName option is specified, a sentinel-backed FailoverClient is returned.
// 2. if the number of Addrs is two or more, a ClusterClient is returned.
// 3. Otherwise, a single-node Client is returned.
// 3. If the AddressMap option is specified, a Ring is returned.
// 4. Otherwise, a single-node Client is returned.
func NewUniversalClient(opts *UniversalOptions) UniversalClient {
if opts.MasterName != "" {
return NewFailoverClient(opts.Failover())
} else if len(opts.Addrs) > 1 {
return NewClusterClient(opts.Cluster())
} else if len(opts.AddressMap) > 0 {
return NewRing(opts.Ring())
}

return NewClient(opts.Simple())
}
10 changes: 10 additions & 0 deletions universal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,14 @@ var _ = Describe("UniversalClient", func() {
})
Expect(client.Ping(ctx).Err()).NotTo(HaveOccurred())
})

It("should connect to ring", func() {
Skip("For some reason the ring tests are skipped")
ring := redisRingOptions()

client = redis.NewUniversalClient(&redis.UniversalOptions{
AddressMap: ring.Addrs,
})
Expect(client.Ping(ctx).Err()).NotTo(HaveOccurred())
})
})
Loading