Skip to content

Commit

Permalink
should be able to create a redis ring from universal options and univ…
Browse files Browse the repository at this point in the history
…ersal ctor

Signed-off-by: Tiago Peczenyj <[email protected]>
  • Loading branch information
peczenyj committed Sep 21, 2023
1 parent dac3314 commit c4706aa
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 0 deletions.
11 changes: 11 additions & 0 deletions example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -656,6 +656,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() {
const key = "slowlog-log-slower-than"

Expand Down
48 changes: 48 additions & 0 deletions 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 @@ -151,6 +154,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,
}
}

// Simple returns basic options created from the universal options.
func (o *UniversalOptions) Simple() *Options {
addr := "127.0.0.1:6379"
Expand Down Expand Up @@ -220,12 +264,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. If the AddressMap option is specified, a Ring is returned.
// 3. 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())
}
9 changes: 9 additions & 0 deletions universal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,13 @@ var _ = Describe("UniversalClient", func() {
})
Expect(client.Ping(ctx).Err()).NotTo(HaveOccurred())
})

It("should connect to ring", func() {
ring := redisRingOptions()

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

0 comments on commit c4706aa

Please sign in to comment.