From 15d7ad156f0ffb0721c160830adff22947a81991 Mon Sep 17 00:00:00 2001 From: Evstigneev Denis Date: Tue, 26 Nov 2024 13:14:10 +0300 Subject: [PATCH] add using lazy connect --- go.mod | 8 +++--- go.sum | 16 +++++------ redis.go | 82 ++++++++++++++++++++++++++++++++++++++++++++++++++------ 3 files changed, 86 insertions(+), 20 deletions(-) diff --git a/go.mod b/go.mod index bcdd999..e00ab6c 100644 --- a/go.mod +++ b/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 ) diff --git a/go.sum b/go.sum index 78c91f0..706c028 100644 --- a/go.sum +++ b/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= diff --git a/redis.go b/redis.go index e951663..9686f73 100755 --- a/redis.go +++ b/redis.go @@ -4,6 +4,7 @@ import ( "context" "reflect" "strings" + "sync/atomic" "time" goredis "github.com/redis/go-redis/v9" @@ -54,19 +55,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 { @@ -107,7 +111,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 @@ -115,6 +121,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...) @@ -154,6 +166,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) @@ -205,6 +223,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 @@ -304,6 +328,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 @@ -359,6 +389,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) @@ -397,6 +433,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 @@ -480,6 +522,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) @@ -535,6 +583,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) @@ -731,3 +785,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 +}