add using lazy connect #122

Merged
vtolstov merged 4 commits from devstigneev/micro-store-redis:lazy_conn into v3 2024-11-27 01:57:41 +03:00
3 changed files with 69 additions and 20 deletions
Showing only changes of commit 70047c85c3 - Show all commits

8
go.mod
View File

@ -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
View File

@ -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=

View File

@ -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,10 @@ func (r *Store) Disconnect(ctx context.Context) 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()
defer r.pool.Put(b)
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 {
if err := r.connect(ctx); err != nil {
return err
}
b := r.pool.Get()
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 {
if err := r.connect(ctx); err != nil {
return err
}
options := store.NewReadOptions(opts...)
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 {
if err := r.connect(ctx); err != nil {
return err
}
options := store.NewDeleteOptions(opts...)
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 {
if err := r.connect(ctx); err != nil {
return err
}
b := r.pool.Get()
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 {
if err := r.connect(ctx); err != nil {
return err
}
options := store.NewWriteOptions(opts...)
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 {
if err := r.connect(ctx); err != nil {
return err
}
b := r.pool.Get()
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) {
if err := r.connect(ctx); err != nil {
return nil, err
}
b := r.pool.Get()
defer r.pool.Put(b)
@ -731,3 +769,14 @@ 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 {
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
}