From 9c3d0a6a6e879457ef90a4ccf4560865509d8ac0 Mon Sep 17 00:00:00 2001 From: Evstigneev Denis Date: Tue, 26 Nov 2024 16:52:16 +0300 Subject: [PATCH] update atomic operation --- redis.go | 57 ++++++++++++++++++++------------------------------------ 1 file changed, 20 insertions(+), 37 deletions(-) diff --git a/redis.go b/redis.go index 9686f73..a1da16e 100755 --- a/redis.go +++ b/redis.go @@ -121,10 +121,8 @@ 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 - } + if err := r.connect(ctx); err != nil { + return err } b := r.pool.Get() @@ -166,10 +164,8 @@ 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 - } + if err := r.connect(ctx); err != nil { + return err } b := r.pool.Get() @@ -223,10 +219,8 @@ 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 - } + if err := r.connect(ctx); err != nil { + return err } options := store.NewReadOptions(opts...) @@ -328,10 +322,8 @@ 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 - } + if err := r.connect(ctx); err != nil { + return err } options := store.NewDeleteOptions(opts...) @@ -389,10 +381,8 @@ 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 - } + if err := r.connect(ctx); err != nil { + return err } b := r.pool.Get() @@ -433,10 +423,8 @@ 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 - } + if err := r.connect(ctx); err != nil { + return err } options := store.NewWriteOptions(opts...) @@ -522,10 +510,8 @@ 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 - } + if err := r.connect(ctx); err != nil { + return err } b := r.pool.Get() @@ -583,10 +569,8 @@ 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 - } + if err := r.connect(ctx); err != nil { + return nil, err } b := r.pool.Get() @@ -786,14 +770,13 @@ func (r *Store) getKey(b *strings.Builder, mainNamespace string, opNamespace str return b.String() } -func (r *Store) connect(ctx context.Context) error { - var err error - if r.isConnected.CompareAndSwap(0, 1) { +func (r *Store) connect(ctx context.Context) (err error) { + if r.isConnected.Load() == 0 { if err = r.cli.Ping(ctx).Err(); err != nil { - r.isConnected.Store(0) setSpanError(ctx, err) + return err } } - + r.isConnected.Store(1) return err }