mirror of
https://github.com/hibiken/asynq.git
synced 2025-09-19 05:17:30 +08:00
Add RedisClusterClientOpt to connect to redis cluster
This commit is contained in:
35
asynq.go
35
asynq.go
@@ -37,7 +37,9 @@ func NewTask(typename string, payload map[string]interface{}) *Task {
|
||||
//
|
||||
// RedisConnOpt represents a sum of following types:
|
||||
//
|
||||
// RedisClientOpt | *RedisClientOpt | RedisFailoverClientOpt | *RedisFailoverClientOpt
|
||||
// - RedisClientOpt
|
||||
// - RedisFailoverClientOpt
|
||||
// - RedisClusterClientOpt
|
||||
type RedisConnOpt interface{}
|
||||
|
||||
// RedisClientOpt is used to create a redis client that connects
|
||||
@@ -50,6 +52,7 @@ type RedisClientOpt struct {
|
||||
// Redis server address in "host:port" format.
|
||||
Addr string
|
||||
|
||||
// TODO: Add Username
|
||||
// Redis server password.
|
||||
Password string
|
||||
|
||||
@@ -81,6 +84,7 @@ type RedisFailoverClientOpt struct {
|
||||
// Redis sentinel password.
|
||||
SentinelPassword string
|
||||
|
||||
// TODO: Add Username
|
||||
// Redis server password.
|
||||
Password string
|
||||
|
||||
@@ -97,6 +101,21 @@ type RedisFailoverClientOpt struct {
|
||||
TLSConfig *tls.Config
|
||||
}
|
||||
|
||||
// RedisFailoverClientOpt is used to creates a redis client that connects to
|
||||
// redis cluster.
|
||||
type RedisClusterClientOpt struct {
|
||||
// A seed list of host:port addresses of cluster nodes.
|
||||
Addrs []string
|
||||
|
||||
// TODO: Add Username
|
||||
// Redis server password.
|
||||
Password string
|
||||
|
||||
// TLS Config used to connect to a server.
|
||||
// TLS will be negotiated only if this field is set.
|
||||
TLSConfig *tls.Config
|
||||
}
|
||||
|
||||
// ParseRedisURI parses redis uri string and returns RedisConnOpt if uri is valid.
|
||||
// It returns a non-nil error if uri cannot be parsed.
|
||||
//
|
||||
@@ -173,7 +192,7 @@ func parseRedisSentinelURI(u *url.URL) (RedisConnOpt, error) {
|
||||
// createRedisClient returns a redis client given a redis connection configuration.
|
||||
//
|
||||
// Passing an unexpected type as a RedisConnOpt argument will cause panic.
|
||||
func createRedisClient(r RedisConnOpt) *redis.Client {
|
||||
func createRedisClient(r RedisConnOpt) redis.UniversalClient {
|
||||
switch r := r.(type) {
|
||||
case *RedisClientOpt:
|
||||
return redis.NewClient(&redis.Options{
|
||||
@@ -213,6 +232,18 @@ func createRedisClient(r RedisConnOpt) *redis.Client {
|
||||
PoolSize: r.PoolSize,
|
||||
TLSConfig: r.TLSConfig,
|
||||
})
|
||||
case RedisClusterClientOpt:
|
||||
return redis.NewClusterClient(&redis.ClusterOptions{
|
||||
Addrs: r.Addrs,
|
||||
Password: r.Password,
|
||||
TLSConfig: r.TLSConfig,
|
||||
})
|
||||
case *RedisClusterClientOpt:
|
||||
return redis.NewClusterClient(&redis.ClusterOptions{
|
||||
Addrs: r.Addrs,
|
||||
Password: r.Password,
|
||||
TLSConfig: r.TLSConfig,
|
||||
})
|
||||
default:
|
||||
panic(fmt.Sprintf("asynq: unexpected type %T for RedisConnOpt", r))
|
||||
}
|
||||
|
Reference in New Issue
Block a user