add using lazy connect
Some checks failed
automerge / automerge (pull_request) Has been skipped
dependabot-automerge / automerge (pull_request) Has been skipped
autoapprove / autoapprove (pull_request) Successful in 15s
codeql / analyze (go) (pull_request) Failing after 1m5s
prbuild / test (pull_request) Failing after 5m12s
prbuild / lint (pull_request) Successful in 9m47s
Some checks failed
automerge / automerge (pull_request) Has been skipped
dependabot-automerge / automerge (pull_request) Has been skipped
autoapprove / autoapprove (pull_request) Successful in 15s
codeql / analyze (go) (pull_request) Failing after 1m5s
prbuild / test (pull_request) Failing after 5m12s
prbuild / lint (pull_request) Successful in 9m47s
This commit is contained in:
parent
ccd65c61af
commit
24e1acc35c
8
go.mod
8
go.mod
@ -5,9 +5,9 @@ go 1.22
|
||||
toolchain go1.22.4
|
||||
|
||||
require (
|
||||
github.com/redis/go-redis/extra/rediscmd/v9 v9.6.2
|
||||
github.com/redis/go-redis/v9 v9.6.2
|
||||
go.unistack.org/micro/v3 v3.10.97
|
||||
github.com/redis/go-redis/extra/rediscmd/v9 v9.7.0
|
||||
github.com/redis/go-redis/v9 v9.7.0
|
||||
go.unistack.org/micro/v3 v3.10.105
|
||||
)
|
||||
|
||||
require (
|
||||
@ -15,5 +15,5 @@ require (
|
||||
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f // indirect
|
||||
github.com/google/go-cmp v0.6.0 // indirect
|
||||
go.unistack.org/micro-proto/v3 v3.4.1 // indirect
|
||||
google.golang.org/protobuf v1.35.1 // indirect
|
||||
google.golang.org/protobuf v1.35.2 // indirect
|
||||
)
|
||||
|
16
go.sum
16
go.sum
@ -8,13 +8,13 @@ github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f h1:lO4WD4F/r
|
||||
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f/go.mod h1:cuUVRXasLTGF7a8hSLbxyZXjz+1KgoB3wDUb6vlszIc=
|
||||
github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI=
|
||||
github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
|
||||
github.com/redis/go-redis/extra/rediscmd/v9 v9.6.2 h1:oBlErygFka9FAVfowzW7sRUfB7n31YX0aFzfaOflM3w=
|
||||
github.com/redis/go-redis/extra/rediscmd/v9 v9.6.2/go.mod h1:jYLUE5tC8UsFnpSclhEjZlFLMMtHH4jTfFMnshNWkoo=
|
||||
github.com/redis/go-redis/v9 v9.6.2 h1:w0uvkRbc9KpgD98zcvo5IrVUsn0lXpRMuhNgiHDJzdk=
|
||||
github.com/redis/go-redis/v9 v9.6.2/go.mod h1:0C0c6ycQsdpVNQpxb1njEQIqkx5UcsM8FJCQLgE9+RA=
|
||||
github.com/redis/go-redis/extra/rediscmd/v9 v9.7.0 h1:BIx9TNZH/Jsr4l1i7VVxnV0JPiwYj8qyrHyuL0fGZrk=
|
||||
github.com/redis/go-redis/extra/rediscmd/v9 v9.7.0/go.mod h1:eTg/YQtGYAZD5r3DlGlJptJ45AHA+/G+2NPn30PKzik=
|
||||
github.com/redis/go-redis/v9 v9.7.0 h1:HhLSs+B6O021gwzl+locl0zEDnyNkxMtf/Z3NNBMa9E=
|
||||
github.com/redis/go-redis/v9 v9.7.0/go.mod h1:f6zhXITC7JUJIlPEiBOTXxJgPLdZcA93GewI7inzyWw=
|
||||
go.unistack.org/micro-proto/v3 v3.4.1 h1:UTjLSRz2YZuaHk9iSlVqqsA50JQNAEK2ZFboGqtEa9Q=
|
||||
go.unistack.org/micro-proto/v3 v3.4.1/go.mod h1:okx/cnOhzuCX0ggl/vToatbCupi0O44diiiLLsZ93Zo=
|
||||
go.unistack.org/micro/v3 v3.10.97 h1:8l7fv+i06/PjPrBBhRC/ZQkWGIOuHPg3jJN0vktYE78=
|
||||
go.unistack.org/micro/v3 v3.10.97/go.mod h1:YzMldzHN9Ei+zy5t/Psu7RUWDZwUfrNYiStSQtTz90g=
|
||||
google.golang.org/protobuf v1.35.1 h1:m3LfL6/Ca+fqnjnlqQXNpFPABW1UD7mjh8KO2mKFytA=
|
||||
google.golang.org/protobuf v1.35.1/go.mod h1:9fA7Ob0pmnwhb644+1+CVWFRbNajQ6iRojtC/QF5bRE=
|
||||
go.unistack.org/micro/v3 v3.10.105 h1:JYNV0d+fnR7Hy8d4/sjr+25DbSNqq1Z7IPeDDdB+f1I=
|
||||
go.unistack.org/micro/v3 v3.10.105/go.mod h1:YzMldzHN9Ei+zy5t/Psu7RUWDZwUfrNYiStSQtTz90g=
|
||||
google.golang.org/protobuf v1.35.2 h1:8Ar7bF+apOIoThw1EdZl0p1oWvMqTHmpA2fRTyZO8io=
|
||||
google.golang.org/protobuf v1.35.2/go.mod h1:9fA7Ob0pmnwhb644+1+CVWFRbNajQ6iRojtC/QF5bRE=
|
||||
|
82
redis.go
82
redis.go
@ -5,6 +5,7 @@ import (
|
||||
"errors"
|
||||
"reflect"
|
||||
"strings"
|
||||
"sync/atomic"
|
||||
"time"
|
||||
|
||||
goredis "github.com/redis/go-redis/v9"
|
||||
@ -55,19 +56,22 @@ var (
|
||||
)
|
||||
|
||||
type Store struct {
|
||||
opts store.Options
|
||||
cli goredis.UniversalClient
|
||||
done chan struct{}
|
||||
pool *pool.StringsPool
|
||||
opts store.Options
|
||||
cli goredis.UniversalClient
|
||||
done chan struct{}
|
||||
pool *pool.StringsPool
|
||||
isConnected atomic.Int32
|
||||
}
|
||||
|
||||
func (r *Store) Connect(ctx context.Context) error {
|
||||
if r.cli == nil {
|
||||
return store.ErrNotConnected
|
||||
}
|
||||
err := r.cli.Ping(ctx).Err()
|
||||
setSpanError(ctx, err)
|
||||
return err
|
||||
if r.opts.LazyConnect {
|
||||
return nil
|
||||
}
|
||||
return r.connect(ctx)
|
||||
|
||||
}
|
||||
|
||||
func (r *Store) Init(opts ...store.Option) error {
|
||||
@ -108,7 +112,9 @@ func (r *Store) Disconnect(ctx context.Context) error {
|
||||
return err
|
||||
default:
|
||||
if r.cli != nil {
|
||||
err = r.cli.Close()
|
||||
if err = r.cli.Close(); err != nil {
|
||||
r.isConnected.Store(0)
|
||||
}
|
||||
}
|
||||
close(r.done)
|
||||
return err
|
||||
@ -116,6 +122,12 @@ func (r *Store) Disconnect(ctx context.Context) error {
|
||||
}
|
||||
|
||||
func (r *Store) Exists(ctx context.Context, key string, opts ...store.ExistsOption) error {
|
||||
if r.opts.LazyConnect {
|
||||
if err := r.connect(ctx); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
b := r.pool.Get()
|
||||
defer r.pool.Put(b)
|
||||
options := store.NewExistsOptions(opts...)
|
||||
@ -158,6 +170,12 @@ func (r *Store) Exists(ctx context.Context, key string, opts ...store.ExistsOpti
|
||||
}
|
||||
|
||||
func (r *Store) Read(ctx context.Context, key string, val interface{}, opts ...store.ReadOption) error {
|
||||
if r.opts.LazyConnect {
|
||||
if err := r.connect(ctx); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
b := r.pool.Get()
|
||||
defer r.pool.Put(b)
|
||||
|
||||
@ -211,6 +229,12 @@ func (r *Store) Read(ctx context.Context, key string, val interface{}, opts ...s
|
||||
}
|
||||
|
||||
func (r *Store) MRead(ctx context.Context, keys []string, vals interface{}, opts ...store.ReadOption) error {
|
||||
if r.opts.LazyConnect {
|
||||
if err := r.connect(ctx); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
options := store.NewReadOptions(opts...)
|
||||
|
||||
timeout := r.opts.Timeout
|
||||
@ -310,6 +334,12 @@ func (r *Store) MRead(ctx context.Context, keys []string, vals interface{}, opts
|
||||
}
|
||||
|
||||
func (r *Store) MDelete(ctx context.Context, keys []string, opts ...store.DeleteOption) error {
|
||||
if r.opts.LazyConnect {
|
||||
if err := r.connect(ctx); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
options := store.NewDeleteOptions(opts...)
|
||||
|
||||
timeout := r.opts.Timeout
|
||||
@ -365,6 +395,12 @@ func (r *Store) MDelete(ctx context.Context, keys []string, opts ...store.Delete
|
||||
}
|
||||
|
||||
func (r *Store) Delete(ctx context.Context, key string, opts ...store.DeleteOption) error {
|
||||
if r.opts.LazyConnect {
|
||||
if err := r.connect(ctx); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
b := r.pool.Get()
|
||||
defer r.pool.Put(b)
|
||||
|
||||
@ -406,6 +442,12 @@ func (r *Store) Delete(ctx context.Context, key string, opts ...store.DeleteOpti
|
||||
}
|
||||
|
||||
func (r *Store) MWrite(ctx context.Context, keys []string, vals []interface{}, opts ...store.WriteOption) error {
|
||||
if r.opts.LazyConnect {
|
||||
if err := r.connect(ctx); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
options := store.NewWriteOptions(opts...)
|
||||
|
||||
timeout := r.opts.Timeout
|
||||
@ -489,6 +531,12 @@ func (r *Store) MWrite(ctx context.Context, keys []string, vals []interface{}, o
|
||||
}
|
||||
|
||||
func (r *Store) Write(ctx context.Context, key string, val interface{}, opts ...store.WriteOption) error {
|
||||
if r.opts.LazyConnect {
|
||||
if err := r.connect(ctx); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
b := r.pool.Get()
|
||||
defer r.pool.Put(b)
|
||||
|
||||
@ -546,6 +594,12 @@ func (r *Store) Write(ctx context.Context, key string, val interface{}, opts ...
|
||||
}
|
||||
|
||||
func (r *Store) List(ctx context.Context, opts ...store.ListOption) ([]string, error) {
|
||||
if r.opts.LazyConnect {
|
||||
if err := r.connect(ctx); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
|
||||
b := r.pool.Get()
|
||||
defer r.pool.Put(b)
|
||||
|
||||
@ -742,3 +796,15 @@ func (r *Store) getKey(b *strings.Builder, mainNamespace string, opNamespace str
|
||||
b.WriteString(key)
|
||||
return b.String()
|
||||
}
|
||||
|
||||
func (r *Store) connect(ctx context.Context) error {
|
||||
var err error
|
||||
if r.isConnected.CompareAndSwap(0, 1) {
|
||||
if err = r.cli.Ping(ctx).Err(); err != nil {
|
||||
r.isConnected.Store(0)
|
||||
setSpanError(ctx, err)
|
||||
}
|
||||
}
|
||||
|
||||
return err
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user