fixup
Some checks failed
codeql / analyze (go) (pull_request) Failing after 13s
prbuild / test (pull_request) Failing after 15s
automerge / automerge (pull_request) Failing after 15s
dependabot-automerge / automerge (pull_request) Has been skipped
autoapprove / autoapprove (pull_request) Failing after 13s
prbuild / lint (pull_request) Failing after 21s
Some checks failed
codeql / analyze (go) (pull_request) Failing after 13s
prbuild / test (pull_request) Failing after 15s
automerge / automerge (pull_request) Failing after 15s
dependabot-automerge / automerge (pull_request) Has been skipped
autoapprove / autoapprove (pull_request) Failing after 13s
prbuild / lint (pull_request) Failing after 21s
Signed-off-by: Vasiliy Tolstov <v.tolstov@unistack.org>
This commit is contained in:
parent
8448680806
commit
617f0f7811
@ -5,12 +5,6 @@ import (
|
||||
"go.unistack.org/micro/v4/options"
|
||||
)
|
||||
|
||||
type configKey struct{}
|
||||
|
||||
func Config(c *redis.Options) options.Option {
|
||||
return options.ContextOption(configKey{}, c)
|
||||
}
|
||||
|
||||
type universalConfigKey struct{}
|
||||
|
||||
func UniversalConfig(c *redis.UniversalOptions) options.Option {
|
||||
|
55
redis.go
55
redis.go
@ -30,7 +30,7 @@ func (r *Store) Init(opts ...options.Option) error {
|
||||
}
|
||||
|
||||
func (r *Store) Redis() redis.UniversalClient {
|
||||
return r.cli.(redis.UniversalClient)
|
||||
return r.cli
|
||||
}
|
||||
|
||||
func (r *Store) Disconnect(ctx context.Context) error {
|
||||
@ -336,28 +336,13 @@ func NewStore(opts ...options.Option) *Store {
|
||||
}
|
||||
|
||||
func (r *Store) configure() error {
|
||||
var redisOptions *redis.Options
|
||||
var redisUniversalOptions *redis.UniversalOptions
|
||||
var err error
|
||||
|
||||
nodes := r.opts.Address
|
||||
|
||||
if len(nodes) == 0 {
|
||||
nodes = []string{"redis://127.0.0.1:6379"}
|
||||
}
|
||||
|
||||
if r.cli != nil && r.opts.Context == nil {
|
||||
return nil
|
||||
}
|
||||
|
||||
if r.opts.Context != nil {
|
||||
if c, ok := r.opts.Context.Value(configKey{}).(*redis.Options); ok {
|
||||
redisOptions = c
|
||||
if r.opts.TLSConfig != nil {
|
||||
redisOptions.TLSConfig = r.opts.TLSConfig
|
||||
}
|
||||
}
|
||||
|
||||
if c, ok := r.opts.Context.Value(universalConfigKey{}).(*redis.UniversalOptions); ok {
|
||||
redisUniversalOptions = c
|
||||
if r.opts.TLSConfig != nil {
|
||||
@ -366,36 +351,12 @@ func (r *Store) configure() error {
|
||||
}
|
||||
}
|
||||
|
||||
if redisOptions != nil && redisUniversalOptions != nil {
|
||||
return fmt.Errorf("must specify only one option Config or ClusterConfig")
|
||||
}
|
||||
|
||||
if redisOptions == nil && redisUniversalOptions == nil && r.cli != nil {
|
||||
if redisUniversalOptions == nil && r.cli != nil {
|
||||
return nil
|
||||
}
|
||||
|
||||
if redisOptions == nil && redisUniversalOptions == nil && len(nodes) == 1 {
|
||||
redisOptions, err = redis.ParseURL(nodes[0])
|
||||
if err != nil {
|
||||
// Backwards compatibility
|
||||
redisOptions = &redis.Options{
|
||||
Addr: nodes[0],
|
||||
Username: "",
|
||||
Password: "", // no password set
|
||||
DB: 0, // use default DB
|
||||
MaxRetries: 2,
|
||||
MaxRetryBackoff: 256 * time.Millisecond,
|
||||
DialTimeout: 1 * time.Second,
|
||||
ReadTimeout: 1 * time.Second,
|
||||
WriteTimeout: 1 * time.Second,
|
||||
PoolTimeout: 1 * time.Second,
|
||||
MinIdleConns: 10,
|
||||
TLSConfig: r.opts.TLSConfig,
|
||||
}
|
||||
}
|
||||
} else if redisOptions == nil && redisUniversalOptions == nil && len(nodes) > 1 {
|
||||
if redisUniversalOptions == nil {
|
||||
redisUniversalOptions = &redis.UniversalOptions{
|
||||
Addrs: nodes,
|
||||
Username: "",
|
||||
Password: "", // no password set
|
||||
DB: 0, // use default DB
|
||||
@ -410,11 +371,13 @@ func (r *Store) configure() error {
|
||||
}
|
||||
}
|
||||
|
||||
if redisOptions != nil {
|
||||
r.cli = redis.NewClient(redisOptions)
|
||||
} else if redisUniversalOptions != nil {
|
||||
r.cli = redis.NewUniversalClient(redisUniversalOptions)
|
||||
if len(r.opts.Address) > 0 {
|
||||
redisUniversalOptions.Addrs = r.opts.Address
|
||||
} else if len(redisUniversalOptions.Addrs) == 0 {
|
||||
redisUniversalOptions.Addrs = []string{"redis://127.0.0.1:6379"}
|
||||
}
|
||||
|
||||
r.cli = redis.NewUniversalClient(redisUniversalOptions)
|
||||
|
||||
return nil
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user