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

Signed-off-by: Vasiliy Tolstov <v.tolstov@unistack.org>
This commit is contained in:
Василий Толстов 2024-10-04 00:19:22 +03:00
parent 8448680806
commit 617f0f7811
2 changed files with 9 additions and 52 deletions

View File

@ -5,12 +5,6 @@ import (
"go.unistack.org/micro/v4/options" "go.unistack.org/micro/v4/options"
) )
type configKey struct{}
func Config(c *redis.Options) options.Option {
return options.ContextOption(configKey{}, c)
}
type universalConfigKey struct{} type universalConfigKey struct{}
func UniversalConfig(c *redis.UniversalOptions) options.Option { func UniversalConfig(c *redis.UniversalOptions) options.Option {

View File

@ -30,7 +30,7 @@ func (r *Store) Init(opts ...options.Option) error {
} }
func (r *Store) Redis() redis.UniversalClient { func (r *Store) Redis() redis.UniversalClient {
return r.cli.(redis.UniversalClient) return r.cli
} }
func (r *Store) Disconnect(ctx context.Context) error { func (r *Store) Disconnect(ctx context.Context) error {
@ -336,28 +336,13 @@ func NewStore(opts ...options.Option) *Store {
} }
func (r *Store) configure() error { func (r *Store) configure() error {
var redisOptions *redis.Options
var redisUniversalOptions *redis.UniversalOptions 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 { if r.cli != nil && r.opts.Context == nil {
return nil return nil
} }
if r.opts.Context != 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 { if c, ok := r.opts.Context.Value(universalConfigKey{}).(*redis.UniversalOptions); ok {
redisUniversalOptions = c redisUniversalOptions = c
if r.opts.TLSConfig != nil { if r.opts.TLSConfig != nil {
@ -366,36 +351,12 @@ func (r *Store) configure() error {
} }
} }
if redisOptions != nil && redisUniversalOptions != nil { if redisUniversalOptions == nil && r.cli != nil {
return fmt.Errorf("must specify only one option Config or ClusterConfig")
}
if redisOptions == nil && redisUniversalOptions == nil && r.cli != nil {
return nil return nil
} }
if redisOptions == nil && redisUniversalOptions == nil && len(nodes) == 1 { if redisUniversalOptions == nil {
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 {
redisUniversalOptions = &redis.UniversalOptions{ redisUniversalOptions = &redis.UniversalOptions{
Addrs: nodes,
Username: "", Username: "",
Password: "", // no password set Password: "", // no password set
DB: 0, // use default DB DB: 0, // use default DB
@ -410,11 +371,13 @@ func (r *Store) configure() error {
} }
} }
if redisOptions != nil { if len(r.opts.Address) > 0 {
r.cli = redis.NewClient(redisOptions) redisUniversalOptions.Addrs = r.opts.Address
} else if redisUniversalOptions != nil { } else if len(redisUniversalOptions.Addrs) == 0 {
r.cli = redis.NewUniversalClient(redisUniversalOptions) redisUniversalOptions.Addrs = []string{"redis://127.0.0.1:6379"}
} }
r.cli = redis.NewUniversalClient(redisUniversalOptions)
return nil return nil
} }