update atomic operation
Some checks failed
automerge / automerge (pull_request) Has been skipped
autoapprove / autoapprove (pull_request) Successful in 13s
dependabot-automerge / automerge (pull_request) Has been skipped
codeql / analyze (go) (pull_request) Failing after 52s
prbuild / test (pull_request) Failing after 5m12s
prbuild / lint (pull_request) Successful in 9m45s
Some checks failed
automerge / automerge (pull_request) Has been skipped
autoapprove / autoapprove (pull_request) Successful in 13s
dependabot-automerge / automerge (pull_request) Has been skipped
codeql / analyze (go) (pull_request) Failing after 52s
prbuild / test (pull_request) Failing after 5m12s
prbuild / lint (pull_request) Successful in 9m45s
add using lazy connect
This commit is contained in:
parent
0981f89f60
commit
b2d89018b8
8
go.mod
8
go.mod
@ -5,9 +5,9 @@ go 1.22
|
|||||||
toolchain go1.22.4
|
toolchain go1.22.4
|
||||||
|
|
||||||
require (
|
require (
|
||||||
github.com/redis/go-redis/extra/rediscmd/v9 v9.6.2
|
github.com/redis/go-redis/extra/rediscmd/v9 v9.7.0
|
||||||
github.com/redis/go-redis/v9 v9.6.2
|
github.com/redis/go-redis/v9 v9.7.0
|
||||||
go.unistack.org/micro/v3 v3.10.97
|
go.unistack.org/micro/v3 v3.10.105
|
||||||
)
|
)
|
||||||
|
|
||||||
require (
|
require (
|
||||||
@ -15,5 +15,5 @@ require (
|
|||||||
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f // indirect
|
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f // indirect
|
||||||
github.com/google/go-cmp v0.6.0 // indirect
|
github.com/google/go-cmp v0.6.0 // indirect
|
||||||
go.unistack.org/micro-proto/v3 v3.4.1 // 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/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 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI=
|
||||||
github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
|
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.7.0 h1:BIx9TNZH/Jsr4l1i7VVxnV0JPiwYj8qyrHyuL0fGZrk=
|
||||||
github.com/redis/go-redis/extra/rediscmd/v9 v9.6.2/go.mod h1:jYLUE5tC8UsFnpSclhEjZlFLMMtHH4jTfFMnshNWkoo=
|
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.6.2 h1:w0uvkRbc9KpgD98zcvo5IrVUsn0lXpRMuhNgiHDJzdk=
|
github.com/redis/go-redis/v9 v9.7.0 h1:HhLSs+B6O021gwzl+locl0zEDnyNkxMtf/Z3NNBMa9E=
|
||||||
github.com/redis/go-redis/v9 v9.6.2/go.mod h1:0C0c6ycQsdpVNQpxb1njEQIqkx5UcsM8FJCQLgE9+RA=
|
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 h1:UTjLSRz2YZuaHk9iSlVqqsA50JQNAEK2ZFboGqtEa9Q=
|
||||||
go.unistack.org/micro-proto/v3 v3.4.1/go.mod h1:okx/cnOhzuCX0ggl/vToatbCupi0O44diiiLLsZ93Zo=
|
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.105 h1:JYNV0d+fnR7Hy8d4/sjr+25DbSNqq1Z7IPeDDdB+f1I=
|
||||||
go.unistack.org/micro/v3 v3.10.97/go.mod h1:YzMldzHN9Ei+zy5t/Psu7RUWDZwUfrNYiStSQtTz90g=
|
go.unistack.org/micro/v3 v3.10.105/go.mod h1:YzMldzHN9Ei+zy5t/Psu7RUWDZwUfrNYiStSQtTz90g=
|
||||||
google.golang.org/protobuf v1.35.1 h1:m3LfL6/Ca+fqnjnlqQXNpFPABW1UD7mjh8KO2mKFytA=
|
google.golang.org/protobuf v1.35.2 h1:8Ar7bF+apOIoThw1EdZl0p1oWvMqTHmpA2fRTyZO8io=
|
||||||
google.golang.org/protobuf v1.35.1/go.mod h1:9fA7Ob0pmnwhb644+1+CVWFRbNajQ6iRojtC/QF5bRE=
|
google.golang.org/protobuf v1.35.2/go.mod h1:9fA7Ob0pmnwhb644+1+CVWFRbNajQ6iRojtC/QF5bRE=
|
||||||
|
65
redis.go
65
redis.go
@ -4,6 +4,7 @@ import (
|
|||||||
"context"
|
"context"
|
||||||
"reflect"
|
"reflect"
|
||||||
"strings"
|
"strings"
|
||||||
|
"sync/atomic"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
goredis "github.com/redis/go-redis/v9"
|
goredis "github.com/redis/go-redis/v9"
|
||||||
@ -54,19 +55,22 @@ var (
|
|||||||
)
|
)
|
||||||
|
|
||||||
type Store struct {
|
type Store struct {
|
||||||
opts store.Options
|
opts store.Options
|
||||||
cli goredis.UniversalClient
|
cli goredis.UniversalClient
|
||||||
done chan struct{}
|
done chan struct{}
|
||||||
pool *pool.StringsPool
|
pool *pool.StringsPool
|
||||||
|
isConnected atomic.Int32
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r *Store) Connect(ctx context.Context) error {
|
func (r *Store) Connect(ctx context.Context) error {
|
||||||
if r.cli == nil {
|
if r.cli == nil {
|
||||||
return store.ErrNotConnected
|
return store.ErrNotConnected
|
||||||
}
|
}
|
||||||
err := r.cli.Ping(ctx).Err()
|
if r.opts.LazyConnect {
|
||||||
setSpanError(ctx, err)
|
return nil
|
||||||
return err
|
}
|
||||||
|
return r.connect(ctx)
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r *Store) Init(opts ...store.Option) error {
|
func (r *Store) Init(opts ...store.Option) error {
|
||||||
@ -107,7 +111,9 @@ func (r *Store) Disconnect(ctx context.Context) error {
|
|||||||
return err
|
return err
|
||||||
default:
|
default:
|
||||||
if r.cli != nil {
|
if r.cli != nil {
|
||||||
err = r.cli.Close()
|
if err = r.cli.Close(); err != nil {
|
||||||
|
r.isConnected.Store(0)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
close(r.done)
|
close(r.done)
|
||||||
return err
|
return err
|
||||||
@ -115,6 +121,10 @@ func (r *Store) Disconnect(ctx context.Context) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (r *Store) Exists(ctx context.Context, key string, opts ...store.ExistsOption) error {
|
func (r *Store) Exists(ctx context.Context, key string, opts ...store.ExistsOption) error {
|
||||||
|
if err := r.connect(ctx); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
b := r.pool.Get()
|
b := r.pool.Get()
|
||||||
defer r.pool.Put(b)
|
defer r.pool.Put(b)
|
||||||
options := store.NewExistsOptions(opts...)
|
options := store.NewExistsOptions(opts...)
|
||||||
@ -154,6 +164,10 @@ 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 {
|
func (r *Store) Read(ctx context.Context, key string, val interface{}, opts ...store.ReadOption) error {
|
||||||
|
if err := r.connect(ctx); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
b := r.pool.Get()
|
b := r.pool.Get()
|
||||||
defer r.pool.Put(b)
|
defer r.pool.Put(b)
|
||||||
|
|
||||||
@ -205,6 +219,10 @@ 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 {
|
func (r *Store) MRead(ctx context.Context, keys []string, vals interface{}, opts ...store.ReadOption) error {
|
||||||
|
if err := r.connect(ctx); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
options := store.NewReadOptions(opts...)
|
options := store.NewReadOptions(opts...)
|
||||||
|
|
||||||
timeout := r.opts.Timeout
|
timeout := r.opts.Timeout
|
||||||
@ -304,6 +322,10 @@ 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 {
|
func (r *Store) MDelete(ctx context.Context, keys []string, opts ...store.DeleteOption) error {
|
||||||
|
if err := r.connect(ctx); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
options := store.NewDeleteOptions(opts...)
|
options := store.NewDeleteOptions(opts...)
|
||||||
|
|
||||||
timeout := r.opts.Timeout
|
timeout := r.opts.Timeout
|
||||||
@ -359,6 +381,10 @@ 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 {
|
func (r *Store) Delete(ctx context.Context, key string, opts ...store.DeleteOption) error {
|
||||||
|
if err := r.connect(ctx); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
b := r.pool.Get()
|
b := r.pool.Get()
|
||||||
defer r.pool.Put(b)
|
defer r.pool.Put(b)
|
||||||
|
|
||||||
@ -397,6 +423,10 @@ 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 {
|
func (r *Store) MWrite(ctx context.Context, keys []string, vals []interface{}, opts ...store.WriteOption) error {
|
||||||
|
if err := r.connect(ctx); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
options := store.NewWriteOptions(opts...)
|
options := store.NewWriteOptions(opts...)
|
||||||
|
|
||||||
timeout := r.opts.Timeout
|
timeout := r.opts.Timeout
|
||||||
@ -480,6 +510,10 @@ 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 {
|
func (r *Store) Write(ctx context.Context, key string, val interface{}, opts ...store.WriteOption) error {
|
||||||
|
if err := r.connect(ctx); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
b := r.pool.Get()
|
b := r.pool.Get()
|
||||||
defer r.pool.Put(b)
|
defer r.pool.Put(b)
|
||||||
|
|
||||||
@ -535,6 +569,10 @@ func (r *Store) Write(ctx context.Context, key string, val interface{}, opts ...
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (r *Store) List(ctx context.Context, opts ...store.ListOption) ([]string, error) {
|
func (r *Store) List(ctx context.Context, opts ...store.ListOption) ([]string, error) {
|
||||||
|
if err := r.connect(ctx); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
b := r.pool.Get()
|
b := r.pool.Get()
|
||||||
defer r.pool.Put(b)
|
defer r.pool.Put(b)
|
||||||
|
|
||||||
@ -731,3 +769,14 @@ func (r *Store) getKey(b *strings.Builder, mainNamespace string, opNamespace str
|
|||||||
b.WriteString(key)
|
b.WriteString(key)
|
||||||
return b.String()
|
return b.String()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (r *Store) connect(ctx context.Context) (err error) {
|
||||||
|
if r.isConnected.Load() == 0 {
|
||||||
|
if err = r.cli.Ping(ctx).Err(); err != nil {
|
||||||
|
setSpanError(ctx, err)
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
r.isConnected.Store(1)
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user