update atomic operation
Some checks failed
automerge / automerge (pull_request) Has been skipped
dependabot-automerge / automerge (pull_request) Has been skipped
autoapprove / autoapprove (pull_request) Successful in 17s
codeql / analyze (go) (pull_request) Failing after 52s
prbuild / test (pull_request) Failing after 5m12s
prbuild / lint (pull_request) Successful in 9m42s

This commit is contained in:
Денис Евстигнеев 2024-11-26 16:52:16 +03:00
parent 15d7ad156f
commit 9c3d0a6a6e

View File

@ -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 { func (r *Store) Exists(ctx context.Context, key string, opts ...store.ExistsOption) error {
if r.opts.LazyConnect { if err := r.connect(ctx); err != nil {
if err := r.connect(ctx); err != nil { return err
return err
}
} }
b := r.pool.Get() 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 { 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 {
if err := r.connect(ctx); err != nil { return err
return err
}
} }
b := r.pool.Get() 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 { 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 {
if err := r.connect(ctx); err != nil { return err
return err
}
} }
options := store.NewReadOptions(opts...) 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 { func (r *Store) MDelete(ctx context.Context, keys []string, opts ...store.DeleteOption) error {
if r.opts.LazyConnect { if err := r.connect(ctx); err != nil {
if err := r.connect(ctx); err != nil { return err
return err
}
} }
options := store.NewDeleteOptions(opts...) 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 { func (r *Store) Delete(ctx context.Context, key string, opts ...store.DeleteOption) error {
if r.opts.LazyConnect { if err := r.connect(ctx); err != nil {
if err := r.connect(ctx); err != nil { return err
return err
}
} }
b := r.pool.Get() 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 { 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 {
if err := r.connect(ctx); err != nil { return err
return err
}
} }
options := store.NewWriteOptions(opts...) 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 { 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 {
if err := r.connect(ctx); err != nil { return err
return err
}
} }
b := r.pool.Get() 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) { func (r *Store) List(ctx context.Context, opts ...store.ListOption) ([]string, error) {
if r.opts.LazyConnect { if err := r.connect(ctx); err != nil {
if err := r.connect(ctx); err != nil { return nil, err
return nil, err
}
} }
b := r.pool.Get() b := r.pool.Get()
@ -786,14 +770,13 @@ func (r *Store) getKey(b *strings.Builder, mainNamespace string, opNamespace str
return b.String() return b.String()
} }
func (r *Store) connect(ctx context.Context) error { func (r *Store) connect(ctx context.Context) (err error) {
var err error if r.isConnected.Load() == 0 {
if r.isConnected.CompareAndSwap(0, 1) {
if err = r.cli.Ping(ctx).Err(); err != nil { if err = r.cli.Ping(ctx).Err(); err != nil {
r.isConnected.Store(0)
setSpanError(ctx, err) setSpanError(ctx, err)
return err
} }
} }
r.isConnected.Store(1)
return err return err
} }