|
|
|
@@ -2,9 +2,9 @@ package redis
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"context"
|
|
|
|
|
"errors"
|
|
|
|
|
"reflect"
|
|
|
|
|
"strings"
|
|
|
|
|
"sync/atomic"
|
|
|
|
|
"time"
|
|
|
|
|
|
|
|
|
|
goredis "github.com/redis/go-redis/v9"
|
|
|
|
@@ -55,22 +55,19 @@ var (
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
type Store struct {
|
|
|
|
|
opts store.Options
|
|
|
|
|
cli goredis.UniversalClient
|
|
|
|
|
done chan struct{}
|
|
|
|
|
pool *pool.StringsPool
|
|
|
|
|
isConnected atomic.Int32
|
|
|
|
|
opts store.Options
|
|
|
|
|
cli goredis.UniversalClient
|
|
|
|
|
done chan struct{}
|
|
|
|
|
pool *pool.StringsPool
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (r *Store) Connect(ctx context.Context) error {
|
|
|
|
|
if r.cli == nil {
|
|
|
|
|
return store.ErrNotConnected
|
|
|
|
|
}
|
|
|
|
|
if r.opts.LazyConnect {
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
return r.connect(ctx)
|
|
|
|
|
|
|
|
|
|
err := r.cli.Ping(ctx).Err()
|
|
|
|
|
setSpanError(ctx, err)
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (r *Store) Init(opts ...store.Option) error {
|
|
|
|
@@ -111,9 +108,7 @@ func (r *Store) Disconnect(ctx context.Context) error {
|
|
|
|
|
return err
|
|
|
|
|
default:
|
|
|
|
|
if r.cli != nil {
|
|
|
|
|
if err = r.cli.Close(); err != nil {
|
|
|
|
|
r.isConnected.Store(0)
|
|
|
|
|
}
|
|
|
|
|
err = r.cli.Close()
|
|
|
|
|
}
|
|
|
|
|
close(r.done)
|
|
|
|
|
return err
|
|
|
|
@@ -121,13 +116,11 @@ 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...)
|
|
|
|
|
labels := make([]string, 0, 6)
|
|
|
|
|
labels = append(labels, "name", options.Name, "statement", "exists")
|
|
|
|
|
|
|
|
|
|
timeout := r.opts.Timeout
|
|
|
|
|
if options.Timeout > 0 {
|
|
|
|
@@ -142,21 +135,22 @@ func (r *Store) Exists(ctx context.Context, key string, opts ...store.ExistsOpti
|
|
|
|
|
|
|
|
|
|
rkey := r.getKey(b, r.opts.Namespace, options.Namespace, key)
|
|
|
|
|
|
|
|
|
|
r.opts.Meter.Counter(semconv.StoreRequestInflight, "name", options.Name).Inc()
|
|
|
|
|
r.opts.Meter.Counter(semconv.StoreRequestInflight, labels...).Inc()
|
|
|
|
|
ts := time.Now()
|
|
|
|
|
val, err := r.cli.Exists(ctx, rkey).Result()
|
|
|
|
|
setSpanError(ctx, err)
|
|
|
|
|
te := time.Since(ts)
|
|
|
|
|
r.opts.Meter.Counter(semconv.StoreRequestInflight, "name", options.Name).Dec()
|
|
|
|
|
r.opts.Meter.Summary(semconv.StoreRequestLatencyMicroseconds, "name", options.Name).Update(te.Seconds())
|
|
|
|
|
r.opts.Meter.Histogram(semconv.StoreRequestDurationSeconds, "name", options.Name).Update(te.Seconds())
|
|
|
|
|
if err == goredis.Nil || (err == nil && val == 0) {
|
|
|
|
|
r.opts.Meter.Counter(semconv.StoreRequestTotal, "name", options.Name, "status", "miss").Inc()
|
|
|
|
|
r.opts.Meter.Counter(semconv.StoreRequestInflight, labels...).Dec()
|
|
|
|
|
|
|
|
|
|
r.opts.Meter.Summary(semconv.StoreRequestLatencyMicroseconds, labels...).Update(te.Seconds())
|
|
|
|
|
r.opts.Meter.Histogram(semconv.StoreRequestDurationSeconds, labels...).Update(te.Seconds())
|
|
|
|
|
if errors.Is(err, goredis.Nil) || (err == nil && val == 0) {
|
|
|
|
|
r.opts.Meter.Counter(semconv.StoreRequestTotal, append(labels, "status", "miss")...).Inc()
|
|
|
|
|
return store.ErrNotFound
|
|
|
|
|
} else if err == nil {
|
|
|
|
|
r.opts.Meter.Counter(semconv.StoreRequestTotal, "name", options.Name, "status", "hit").Inc()
|
|
|
|
|
r.opts.Meter.Counter(semconv.StoreRequestTotal, append(labels, "status", "hit")...).Inc()
|
|
|
|
|
} else if err != nil {
|
|
|
|
|
r.opts.Meter.Counter(semconv.StoreRequestTotal, "name", options.Name, "status", "failure").Inc()
|
|
|
|
|
r.opts.Meter.Counter(semconv.StoreRequestTotal, append(labels, "status", "failure")...).Inc()
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@@ -164,14 +158,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 err := r.connect(ctx); err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
b := r.pool.Get()
|
|
|
|
|
defer r.pool.Put(b)
|
|
|
|
|
|
|
|
|
|
options := store.NewReadOptions(opts...)
|
|
|
|
|
labels := make([]string, 0, 6)
|
|
|
|
|
labels = append(labels, "name", options.Name, "statement", "read")
|
|
|
|
|
|
|
|
|
|
timeout := r.opts.Timeout
|
|
|
|
|
if options.Timeout > 0 {
|
|
|
|
@@ -186,21 +178,21 @@ func (r *Store) Read(ctx context.Context, key string, val interface{}, opts ...s
|
|
|
|
|
|
|
|
|
|
rkey := r.getKey(b, r.opts.Namespace, options.Namespace, key)
|
|
|
|
|
|
|
|
|
|
r.opts.Meter.Counter(semconv.StoreRequestInflight, "name", options.Name).Inc()
|
|
|
|
|
r.opts.Meter.Counter(semconv.StoreRequestInflight, labels...).Inc()
|
|
|
|
|
ts := time.Now()
|
|
|
|
|
buf, err := r.cli.Get(ctx, rkey).Bytes()
|
|
|
|
|
setSpanError(ctx, err)
|
|
|
|
|
te := time.Since(ts)
|
|
|
|
|
r.opts.Meter.Counter(semconv.StoreRequestInflight, "name", options.Name).Dec()
|
|
|
|
|
r.opts.Meter.Summary(semconv.StoreRequestLatencyMicroseconds, "name", options.Name).Update(te.Seconds())
|
|
|
|
|
r.opts.Meter.Histogram(semconv.StoreRequestDurationSeconds, "name", options.Name).Update(te.Seconds())
|
|
|
|
|
if err == goredis.Nil || (err == nil && buf == nil) {
|
|
|
|
|
r.opts.Meter.Counter(semconv.StoreRequestTotal, "name", options.Name, "status", "miss").Inc()
|
|
|
|
|
r.opts.Meter.Counter(semconv.StoreRequestInflight, labels...).Dec()
|
|
|
|
|
r.opts.Meter.Summary(semconv.StoreRequestLatencyMicroseconds, labels...).Update(te.Seconds())
|
|
|
|
|
r.opts.Meter.Histogram(semconv.StoreRequestDurationSeconds, labels...).Update(te.Seconds())
|
|
|
|
|
if errors.Is(err, goredis.Nil) || (err == nil && buf == nil) {
|
|
|
|
|
r.opts.Meter.Counter(semconv.StoreRequestTotal, append(labels, "status", "miss")...).Inc()
|
|
|
|
|
return store.ErrNotFound
|
|
|
|
|
} else if err == nil {
|
|
|
|
|
r.opts.Meter.Counter(semconv.StoreRequestTotal, "name", options.Name, "status", "hit").Inc()
|
|
|
|
|
r.opts.Meter.Counter(semconv.StoreRequestTotal, append(labels, "status", "hit")...).Inc()
|
|
|
|
|
} else if err != nil {
|
|
|
|
|
r.opts.Meter.Counter(semconv.StoreRequestTotal, "name", options.Name, "status", "failure").Inc()
|
|
|
|
|
r.opts.Meter.Counter(semconv.StoreRequestTotal, append(labels, "status", "failure")...).Inc()
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@@ -219,10 +211,6 @@ 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
|
|
|
|
@@ -322,10 +310,6 @@ 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
|
|
|
|
@@ -381,14 +365,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 err := r.connect(ctx); err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
b := r.pool.Get()
|
|
|
|
|
defer r.pool.Put(b)
|
|
|
|
|
|
|
|
|
|
options := store.NewDeleteOptions(opts...)
|
|
|
|
|
labels := make([]string, 0, 6)
|
|
|
|
|
labels = append(labels, "name", options.Name, "statement", "delete")
|
|
|
|
|
|
|
|
|
|
timeout := r.opts.Timeout
|
|
|
|
|
if options.Timeout > 0 {
|
|
|
|
@@ -401,21 +383,22 @@ func (r *Store) Delete(ctx context.Context, key string, opts ...store.DeleteOpti
|
|
|
|
|
defer cancel()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
r.opts.Meter.Counter(semconv.StoreRequestInflight, "name", options.Name).Inc()
|
|
|
|
|
r.opts.Meter.Counter(semconv.StoreRequestInflight, labels...).Inc()
|
|
|
|
|
ts := time.Now()
|
|
|
|
|
err := r.cli.Del(ctx, r.getKey(b, r.opts.Namespace, options.Namespace, key)).Err()
|
|
|
|
|
te := time.Since(ts)
|
|
|
|
|
setSpanError(ctx, err)
|
|
|
|
|
r.opts.Meter.Counter(semconv.StoreRequestInflight, "name", options.Name).Dec()
|
|
|
|
|
r.opts.Meter.Summary(semconv.StoreRequestLatencyMicroseconds, "name", options.Name).Update(te.Seconds())
|
|
|
|
|
r.opts.Meter.Histogram(semconv.StoreRequestDurationSeconds, "name", options.Name).Update(te.Seconds())
|
|
|
|
|
if err == goredis.Nil {
|
|
|
|
|
r.opts.Meter.Counter(semconv.StoreRequestTotal, "name", options.Name, "status", "miss").Inc()
|
|
|
|
|
|
|
|
|
|
r.opts.Meter.Counter(semconv.StoreRequestInflight, labels...).Dec()
|
|
|
|
|
r.opts.Meter.Summary(semconv.StoreRequestLatencyMicroseconds, labels...).Update(te.Seconds())
|
|
|
|
|
r.opts.Meter.Histogram(semconv.StoreRequestDurationSeconds, labels...).Update(te.Seconds())
|
|
|
|
|
if errors.Is(err, goredis.Nil) {
|
|
|
|
|
r.opts.Meter.Counter(semconv.StoreRequestTotal, append(labels, "status", "miss")...).Inc()
|
|
|
|
|
return store.ErrNotFound
|
|
|
|
|
} else if err == nil {
|
|
|
|
|
r.opts.Meter.Counter(semconv.StoreRequestTotal, "name", options.Name, "status", "hit").Inc()
|
|
|
|
|
r.opts.Meter.Counter(semconv.StoreRequestTotal, append(labels, "status", "hit")...).Inc()
|
|
|
|
|
} else if err != nil {
|
|
|
|
|
r.opts.Meter.Counter(semconv.StoreRequestTotal, "name", options.Name, "status", "failure").Inc()
|
|
|
|
|
r.opts.Meter.Counter(semconv.StoreRequestTotal, append(labels, "status", "failure")...).Inc()
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@@ -423,10 +406,6 @@ 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
|
|
|
|
@@ -510,14 +489,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 err := r.connect(ctx); err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
b := r.pool.Get()
|
|
|
|
|
defer r.pool.Put(b)
|
|
|
|
|
|
|
|
|
|
options := store.NewWriteOptions(opts...)
|
|
|
|
|
labels := make([]string, 0, 6)
|
|
|
|
|
labels = append(labels, "name", options.Name, "statement", "write")
|
|
|
|
|
|
|
|
|
|
timeout := r.opts.Timeout
|
|
|
|
|
if options.Timeout > 0 {
|
|
|
|
@@ -546,22 +523,22 @@ func (r *Store) Write(ctx context.Context, key string, val interface{}, opts ...
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
r.opts.Meter.Counter(semconv.StoreRequestInflight, "name", options.Name).Inc()
|
|
|
|
|
r.opts.Meter.Counter(semconv.StoreRequestInflight, labels...).Inc()
|
|
|
|
|
ts := time.Now()
|
|
|
|
|
err := r.cli.Set(ctx, rkey, buf, options.TTL).Err()
|
|
|
|
|
te := time.Since(ts)
|
|
|
|
|
setSpanError(ctx, err)
|
|
|
|
|
|
|
|
|
|
r.opts.Meter.Counter(semconv.StoreRequestInflight, "name", options.Name).Dec()
|
|
|
|
|
r.opts.Meter.Summary(semconv.StoreRequestLatencyMicroseconds, "name", options.Name).Update(te.Seconds())
|
|
|
|
|
r.opts.Meter.Histogram(semconv.StoreRequestDurationSeconds, "name", options.Name).Update(te.Seconds())
|
|
|
|
|
if err == goredis.Nil {
|
|
|
|
|
r.opts.Meter.Counter(semconv.StoreRequestTotal, "name", options.Name, "status", "miss").Inc()
|
|
|
|
|
r.opts.Meter.Counter(semconv.StoreRequestInflight, labels...).Dec()
|
|
|
|
|
r.opts.Meter.Summary(semconv.StoreRequestLatencyMicroseconds, labels...).Update(te.Seconds())
|
|
|
|
|
r.opts.Meter.Histogram(semconv.StoreRequestDurationSeconds, labels...).Update(te.Seconds())
|
|
|
|
|
if errors.Is(err, goredis.Nil) {
|
|
|
|
|
r.opts.Meter.Counter(semconv.StoreRequestTotal, append(labels, "status", "miss")...).Inc()
|
|
|
|
|
return store.ErrNotFound
|
|
|
|
|
} else if err == nil {
|
|
|
|
|
r.opts.Meter.Counter(semconv.StoreRequestTotal, "name", options.Name, "status", "hit").Inc()
|
|
|
|
|
r.opts.Meter.Counter(semconv.StoreRequestTotal, append(labels, "status", "hit")...).Inc()
|
|
|
|
|
} else if err != nil {
|
|
|
|
|
r.opts.Meter.Counter(semconv.StoreRequestTotal, "name", options.Name, "status", "failure").Inc()
|
|
|
|
|
r.opts.Meter.Counter(semconv.StoreRequestTotal, append(labels, "status", "failure")...).Inc()
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@@ -569,10 +546,6 @@ 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)
|
|
|
|
|
|
|
|
|
@@ -769,14 +742,3 @@ func (r *Store) getKey(b *strings.Builder, mainNamespace string, opNamespace str
|
|
|
|
|
b.WriteString(key)
|
|
|
|
|
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 err
|
|
|
|
|
}
|
|
|
|
|