2023-10-20 23:51:29 +03:00
|
|
|
package redis
|
2019-10-03 11:57:49 +03:00
|
|
|
|
|
|
|
import (
|
2021-01-16 03:00:25 +03:00
|
|
|
"context"
|
2024-11-19 00:34:04 +03:00
|
|
|
"errors"
|
2022-10-26 13:46:48 +03:00
|
|
|
"reflect"
|
|
|
|
"strings"
|
2024-12-01 19:58:11 +03:00
|
|
|
"sync"
|
2024-11-27 01:57:39 +03:00
|
|
|
"sync/atomic"
|
2021-01-16 03:00:25 +03:00
|
|
|
"time"
|
2020-04-08 17:08:41 +03:00
|
|
|
|
2024-10-05 16:11:46 +03:00
|
|
|
goredis "github.com/redis/go-redis/v9"
|
2024-04-14 22:27:28 +03:00
|
|
|
"go.unistack.org/micro/v3/semconv"
|
2021-10-27 23:29:16 +03:00
|
|
|
"go.unistack.org/micro/v3/store"
|
2024-12-01 19:58:11 +03:00
|
|
|
"go.unistack.org/micro/v3/util/id"
|
2024-04-14 22:27:28 +03:00
|
|
|
pool "go.unistack.org/micro/v3/util/xpool"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2024-12-01 19:58:11 +03:00
|
|
|
_ store.Store = (*Store)(nil)
|
|
|
|
_ store.Event = (*event)(nil)
|
|
|
|
sendEventTime = 10 * time.Millisecond
|
|
|
|
DefaultUniversalOptions = &goredis.UniversalOptions{
|
2024-04-14 22:27:28 +03:00
|
|
|
Username: "",
|
|
|
|
Password: "", // no password set
|
|
|
|
MaxRetries: 2,
|
|
|
|
MaxRetryBackoff: 256 * time.Millisecond,
|
|
|
|
DialTimeout: 1 * time.Second,
|
|
|
|
ReadTimeout: 1 * time.Second,
|
|
|
|
WriteTimeout: 1 * time.Second,
|
|
|
|
PoolTimeout: 1 * time.Second,
|
|
|
|
MinIdleConns: 10,
|
|
|
|
}
|
|
|
|
|
2024-10-05 16:11:46 +03:00
|
|
|
DefaultClusterOptions = &goredis.ClusterOptions{
|
|
|
|
Username: "",
|
|
|
|
Password: "", // no password set
|
|
|
|
MaxRetries: 2,
|
|
|
|
MaxRetryBackoff: 256 * time.Millisecond,
|
|
|
|
DialTimeout: 1 * time.Second,
|
|
|
|
ReadTimeout: 1 * time.Second,
|
|
|
|
WriteTimeout: 1 * time.Second,
|
|
|
|
PoolTimeout: 1 * time.Second,
|
|
|
|
MinIdleConns: 10,
|
|
|
|
}
|
|
|
|
|
|
|
|
DefaultOptions = &goredis.Options{
|
2024-04-14 22:27:28 +03:00
|
|
|
Username: "",
|
|
|
|
Password: "", // no password set
|
|
|
|
DB: 0, // use default DB
|
|
|
|
MaxRetries: 2,
|
|
|
|
MaxRetryBackoff: 256 * time.Millisecond,
|
|
|
|
DialTimeout: 1 * time.Second,
|
|
|
|
ReadTimeout: 1 * time.Second,
|
|
|
|
WriteTimeout: 1 * time.Second,
|
|
|
|
PoolTimeout: 1 * time.Second,
|
|
|
|
MinIdleConns: 10,
|
|
|
|
}
|
2019-10-03 11:57:49 +03:00
|
|
|
)
|
|
|
|
|
2023-10-20 23:51:29 +03:00
|
|
|
type Store struct {
|
2024-11-27 01:57:39 +03:00
|
|
|
cli goredis.UniversalClient
|
|
|
|
pool *pool.StringsPool
|
2024-12-01 19:58:11 +03:00
|
|
|
connected *atomic.Uint32
|
2024-11-27 01:57:39 +03:00
|
|
|
opts store.Options
|
2024-12-01 19:58:11 +03:00
|
|
|
watchers map[string]*watcher
|
|
|
|
mu sync.RWMutex
|
2021-01-16 03:00:25 +03:00
|
|
|
}
|
|
|
|
|
2023-10-20 23:51:29 +03:00
|
|
|
func (r *Store) Connect(ctx context.Context) error {
|
2024-12-01 19:58:11 +03:00
|
|
|
if r.connected.Load() == 1 {
|
2024-11-27 01:57:39 +03:00
|
|
|
return nil
|
|
|
|
}
|
2024-10-05 16:11:46 +03:00
|
|
|
if r.cli == nil {
|
|
|
|
return store.ErrNotConnected
|
2024-04-23 09:30:35 +03:00
|
|
|
}
|
2024-11-27 01:57:39 +03:00
|
|
|
if r.opts.LazyConnect {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
if err := r.cli.Ping(ctx).Err(); err != nil {
|
|
|
|
setSpanError(ctx, err)
|
|
|
|
return err
|
|
|
|
}
|
2024-12-01 19:58:11 +03:00
|
|
|
r.connected.Store(1)
|
2024-11-27 01:57:39 +03:00
|
|
|
return nil
|
2019-10-03 11:57:49 +03:00
|
|
|
}
|
|
|
|
|
2023-10-20 23:51:29 +03:00
|
|
|
func (r *Store) Init(opts ...store.Option) error {
|
2020-05-13 12:59:23 +03:00
|
|
|
for _, o := range opts {
|
2021-01-16 03:00:25 +03:00
|
|
|
o(&r.opts)
|
2020-05-13 12:59:23 +03:00
|
|
|
}
|
|
|
|
|
2024-07-04 15:13:19 +03:00
|
|
|
err := r.configure()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
2020-02-12 18:53:18 +03:00
|
|
|
}
|
2019-10-03 11:57:49 +03:00
|
|
|
|
2024-10-05 16:11:46 +03:00
|
|
|
func (r *Store) Client() *goredis.Client {
|
|
|
|
if c, ok := r.cli.(*goredis.Client); ok {
|
|
|
|
return c
|
2024-04-23 09:30:35 +03:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2024-10-05 16:11:46 +03:00
|
|
|
func (r *Store) UniversalClient() goredis.UniversalClient {
|
|
|
|
return r.cli
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *Store) ClusterClient() *goredis.ClusterClient {
|
|
|
|
if c, ok := r.cli.(*goredis.ClusterClient); ok {
|
|
|
|
return c
|
2024-04-23 09:30:35 +03:00
|
|
|
}
|
|
|
|
return nil
|
2023-12-12 13:48:47 +03:00
|
|
|
}
|
|
|
|
|
2023-10-20 23:51:29 +03:00
|
|
|
func (r *Store) Disconnect(ctx context.Context) error {
|
2024-12-01 19:58:11 +03:00
|
|
|
if r.connected.Load() == 0 {
|
2024-11-27 01:57:39 +03:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if r.cli != nil {
|
|
|
|
if err := r.cli.Close(); err != nil {
|
|
|
|
return err
|
2024-07-04 15:13:19 +03:00
|
|
|
}
|
2024-04-23 09:30:35 +03:00
|
|
|
}
|
2024-11-27 01:57:39 +03:00
|
|
|
|
2024-12-01 19:58:11 +03:00
|
|
|
r.connected.Store(1)
|
2024-11-27 01:57:39 +03:00
|
|
|
return nil
|
2020-04-08 16:34:27 +03:00
|
|
|
}
|
|
|
|
|
2023-10-20 23:51:29 +03:00
|
|
|
func (r *Store) Exists(ctx context.Context, key string, opts ...store.ExistsOption) error {
|
2024-10-05 16:11:46 +03:00
|
|
|
b := r.pool.Get()
|
|
|
|
defer r.pool.Put(b)
|
2021-11-24 15:23:47 +03:00
|
|
|
options := store.NewExistsOptions(opts...)
|
2024-11-19 00:34:04 +03:00
|
|
|
labels := make([]string, 0, 6)
|
|
|
|
labels = append(labels, "name", options.Name, "statement", "exists")
|
2024-04-14 22:27:28 +03:00
|
|
|
|
|
|
|
timeout := r.opts.Timeout
|
|
|
|
if options.Timeout > 0 {
|
|
|
|
timeout = options.Timeout
|
2022-10-26 13:46:48 +03:00
|
|
|
}
|
2024-04-14 22:27:28 +03:00
|
|
|
|
|
|
|
if timeout > 0 {
|
|
|
|
var cancel context.CancelFunc
|
|
|
|
ctx, cancel = context.WithTimeout(ctx, timeout)
|
|
|
|
defer cancel()
|
2021-05-20 22:49:30 +03:00
|
|
|
}
|
2024-04-14 22:27:28 +03:00
|
|
|
|
2024-10-05 16:11:46 +03:00
|
|
|
rkey := r.getKey(b, r.opts.Namespace, options.Namespace, key)
|
2024-04-14 22:27:28 +03:00
|
|
|
|
2024-11-19 00:34:04 +03:00
|
|
|
r.opts.Meter.Counter(semconv.StoreRequestInflight, labels...).Inc()
|
2024-04-14 22:27:28 +03:00
|
|
|
ts := time.Now()
|
2024-10-05 16:11:46 +03:00
|
|
|
val, err := r.cli.Exists(ctx, rkey).Result()
|
2024-07-05 09:16:08 +03:00
|
|
|
setSpanError(ctx, err)
|
2024-04-14 22:27:28 +03:00
|
|
|
te := time.Since(ts)
|
2024-11-19 00:34:04 +03:00
|
|
|
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()
|
2021-01-16 03:00:25 +03:00
|
|
|
return store.ErrNotFound
|
2024-04-14 22:27:28 +03:00
|
|
|
} else if err == nil {
|
2024-11-19 00:34:04 +03:00
|
|
|
r.opts.Meter.Counter(semconv.StoreRequestTotal, append(labels, "status", "hit")...).Inc()
|
2024-11-27 01:57:39 +03:00
|
|
|
} else {
|
2024-11-19 00:34:04 +03:00
|
|
|
r.opts.Meter.Counter(semconv.StoreRequestTotal, append(labels, "status", "failure")...).Inc()
|
2024-04-14 22:27:28 +03:00
|
|
|
return err
|
2020-05-13 14:45:16 +03:00
|
|
|
}
|
2024-04-14 22:27:28 +03:00
|
|
|
|
2021-01-16 03:00:25 +03:00
|
|
|
return nil
|
|
|
|
}
|
2020-05-13 14:45:16 +03:00
|
|
|
|
2023-10-20 23:51:29 +03:00
|
|
|
func (r *Store) Read(ctx context.Context, key string, val interface{}, opts ...store.ReadOption) error {
|
2024-10-05 16:11:46 +03:00
|
|
|
b := r.pool.Get()
|
|
|
|
defer r.pool.Put(b)
|
|
|
|
|
2021-01-16 03:00:25 +03:00
|
|
|
options := store.NewReadOptions(opts...)
|
2024-11-19 00:34:04 +03:00
|
|
|
labels := make([]string, 0, 6)
|
|
|
|
labels = append(labels, "name", options.Name, "statement", "read")
|
2024-04-14 22:27:28 +03:00
|
|
|
|
|
|
|
timeout := r.opts.Timeout
|
|
|
|
if options.Timeout > 0 {
|
|
|
|
timeout = options.Timeout
|
2021-01-16 03:00:25 +03:00
|
|
|
}
|
2024-04-14 22:27:28 +03:00
|
|
|
|
|
|
|
if timeout > 0 {
|
|
|
|
var cancel context.CancelFunc
|
|
|
|
ctx, cancel = context.WithTimeout(ctx, timeout)
|
|
|
|
defer cancel()
|
2022-10-26 13:46:48 +03:00
|
|
|
}
|
2023-10-20 23:51:29 +03:00
|
|
|
|
2024-10-05 16:11:46 +03:00
|
|
|
rkey := r.getKey(b, r.opts.Namespace, options.Namespace, key)
|
2024-04-14 22:40:30 +03:00
|
|
|
|
2024-11-19 00:34:04 +03:00
|
|
|
r.opts.Meter.Counter(semconv.StoreRequestInflight, labels...).Inc()
|
2024-04-14 22:27:28 +03:00
|
|
|
ts := time.Now()
|
2024-10-05 16:11:46 +03:00
|
|
|
buf, err := r.cli.Get(ctx, rkey).Bytes()
|
2024-07-05 09:16:08 +03:00
|
|
|
setSpanError(ctx, err)
|
2024-04-14 22:27:28 +03:00
|
|
|
te := time.Since(ts)
|
2024-11-19 00:34:04 +03:00
|
|
|
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()
|
2021-01-16 03:00:25 +03:00
|
|
|
return store.ErrNotFound
|
2024-04-14 22:27:28 +03:00
|
|
|
} else if err == nil {
|
2024-11-19 00:34:04 +03:00
|
|
|
r.opts.Meter.Counter(semconv.StoreRequestTotal, append(labels, "status", "hit")...).Inc()
|
2021-01-16 03:00:25 +03:00
|
|
|
} else if err != nil {
|
2024-11-19 00:34:04 +03:00
|
|
|
r.opts.Meter.Counter(semconv.StoreRequestTotal, append(labels, "status", "failure")...).Inc()
|
2021-01-16 03:00:25 +03:00
|
|
|
return err
|
2020-02-12 18:53:18 +03:00
|
|
|
}
|
2024-04-14 22:27:28 +03:00
|
|
|
|
|
|
|
switch b := val.(type) {
|
|
|
|
case *[]byte:
|
|
|
|
*b = buf
|
|
|
|
case *string:
|
|
|
|
*b = string(buf)
|
|
|
|
default:
|
2024-04-14 22:40:30 +03:00
|
|
|
if err = r.opts.Codec.Unmarshal(buf, val); err != nil {
|
2024-07-05 09:16:08 +03:00
|
|
|
setSpanError(ctx, err)
|
2024-04-14 22:40:30 +03:00
|
|
|
}
|
2020-06-30 13:07:12 +03:00
|
|
|
}
|
2024-04-14 22:27:28 +03:00
|
|
|
|
|
|
|
return err
|
2019-10-03 11:57:49 +03:00
|
|
|
}
|
|
|
|
|
2023-10-20 23:51:29 +03:00
|
|
|
func (r *Store) MRead(ctx context.Context, keys []string, vals interface{}, opts ...store.ReadOption) error {
|
2024-04-14 22:27:28 +03:00
|
|
|
options := store.NewReadOptions(opts...)
|
|
|
|
|
|
|
|
timeout := r.opts.Timeout
|
|
|
|
if options.Timeout > 0 {
|
|
|
|
timeout = options.Timeout
|
2022-10-26 13:46:48 +03:00
|
|
|
}
|
2024-04-14 22:27:28 +03:00
|
|
|
|
|
|
|
if timeout > 0 {
|
2023-10-20 23:51:29 +03:00
|
|
|
var cancel context.CancelFunc
|
2024-04-14 22:27:28 +03:00
|
|
|
ctx, cancel = context.WithTimeout(ctx, timeout)
|
2023-10-20 23:51:29 +03:00
|
|
|
defer cancel()
|
|
|
|
}
|
2024-04-14 22:27:28 +03:00
|
|
|
|
2024-09-25 15:26:52 +03:00
|
|
|
var rkeys []string
|
2024-10-05 16:11:46 +03:00
|
|
|
var pools []*strings.Builder
|
2024-04-14 22:27:28 +03:00
|
|
|
if r.opts.Namespace != "" || options.Namespace != "" {
|
2024-09-25 15:26:52 +03:00
|
|
|
rkeys = make([]string, len(keys))
|
2024-10-05 16:11:46 +03:00
|
|
|
pools = make([]*strings.Builder, len(keys))
|
2024-04-14 22:27:28 +03:00
|
|
|
for idx, key := range keys {
|
2024-10-05 16:11:46 +03:00
|
|
|
b := r.pool.Get()
|
|
|
|
pools[idx] = b
|
|
|
|
rkeys[idx] = r.getKey(b, r.opts.Namespace, options.Namespace, key)
|
2022-10-26 13:46:48 +03:00
|
|
|
}
|
|
|
|
}
|
2023-10-20 23:51:29 +03:00
|
|
|
|
2024-07-04 15:13:19 +03:00
|
|
|
r.opts.Meter.Counter(semconv.StoreRequestInflight, "name", options.Name).Inc()
|
2024-04-14 22:27:28 +03:00
|
|
|
ts := time.Now()
|
2024-04-23 09:30:35 +03:00
|
|
|
var rvals []interface{}
|
|
|
|
var err error
|
2024-09-25 15:26:52 +03:00
|
|
|
if r.opts.Namespace != "" || options.Namespace != "" {
|
2024-10-05 16:11:46 +03:00
|
|
|
rvals, err = r.cli.MGet(ctx, rkeys...).Result()
|
|
|
|
for idx := range pools {
|
|
|
|
r.pool.Put(pools[idx])
|
2024-09-25 15:26:52 +03:00
|
|
|
}
|
2024-04-23 09:30:35 +03:00
|
|
|
} else {
|
2024-10-05 16:11:46 +03:00
|
|
|
rvals, err = r.cli.MGet(ctx, keys...).Result()
|
2024-04-23 09:30:35 +03:00
|
|
|
}
|
2024-07-05 09:16:08 +03:00
|
|
|
setSpanError(ctx, err)
|
2024-04-14 22:27:28 +03:00
|
|
|
te := time.Since(ts)
|
2024-07-04 15:13:19 +03:00
|
|
|
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())
|
2024-10-05 16:11:46 +03:00
|
|
|
if err == goredis.Nil || (len(rvals) == 0) {
|
2024-07-04 15:13:19 +03:00
|
|
|
r.opts.Meter.Counter(semconv.StoreRequestTotal, "name", options.Name, "status", "miss").Inc()
|
2022-10-26 13:46:48 +03:00
|
|
|
return store.ErrNotFound
|
2024-04-14 22:27:28 +03:00
|
|
|
} else if err == nil {
|
2024-07-04 15:13:19 +03:00
|
|
|
r.opts.Meter.Counter(semconv.StoreRequestTotal, "name", options.Name, "status", "hit").Inc()
|
2022-10-26 13:46:48 +03:00
|
|
|
} else if err != nil {
|
2024-07-04 15:13:19 +03:00
|
|
|
r.opts.Meter.Counter(semconv.StoreRequestTotal, "name", options.Name, "status", "failure").Inc()
|
2022-10-26 13:46:48 +03:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
vv := reflect.ValueOf(vals)
|
|
|
|
vt := reflect.TypeOf(vals)
|
|
|
|
switch vv.Kind() {
|
|
|
|
case reflect.Ptr:
|
|
|
|
vv = reflect.Indirect(vv)
|
|
|
|
vt = vt.Elem()
|
|
|
|
}
|
|
|
|
if vv.Kind() != reflect.Slice {
|
|
|
|
return store.ErrNotFound
|
|
|
|
}
|
|
|
|
nvv := reflect.MakeSlice(vt, len(rvals), len(rvals))
|
|
|
|
vt = vt.Elem()
|
|
|
|
for idx := 0; idx < len(rvals); idx++ {
|
|
|
|
if rvals[idx] == nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
itm := nvv.Index(idx)
|
|
|
|
var buf []byte
|
|
|
|
switch b := rvals[idx].(type) {
|
|
|
|
case []byte:
|
|
|
|
buf = b
|
|
|
|
case string:
|
|
|
|
buf = []byte(b)
|
|
|
|
}
|
|
|
|
// special case for raw data
|
|
|
|
if vt.Kind() == reflect.Slice && vt.Elem().Kind() == reflect.Uint8 {
|
|
|
|
itm.Set(reflect.MakeSlice(itm.Type(), len(buf), len(buf)))
|
2024-04-14 22:27:28 +03:00
|
|
|
itm.SetBytes(buf)
|
|
|
|
continue
|
|
|
|
} else if vt.Kind() == reflect.String {
|
|
|
|
itm.SetString(string(buf))
|
|
|
|
continue
|
2022-10-26 13:46:48 +03:00
|
|
|
}
|
2024-04-14 22:27:28 +03:00
|
|
|
|
|
|
|
itm.Set(reflect.New(vt.Elem()))
|
2022-10-26 13:46:48 +03:00
|
|
|
if err = r.opts.Codec.Unmarshal(buf, itm.Interface()); err != nil {
|
2024-07-05 09:16:08 +03:00
|
|
|
setSpanError(ctx, err)
|
2022-10-26 13:46:48 +03:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
vv.Set(nvv)
|
2024-04-14 22:40:30 +03:00
|
|
|
|
2022-10-26 13:46:48 +03:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-10-20 23:51:29 +03:00
|
|
|
func (r *Store) MDelete(ctx context.Context, keys []string, opts ...store.DeleteOption) error {
|
2024-04-14 22:27:28 +03:00
|
|
|
options := store.NewDeleteOptions(opts...)
|
2024-11-27 01:57:39 +03:00
|
|
|
labels := make([]string, 0, 6)
|
|
|
|
labels = append(labels, "name", options.Name, "statement", "delete")
|
2024-04-14 22:27:28 +03:00
|
|
|
|
|
|
|
timeout := r.opts.Timeout
|
|
|
|
if options.Timeout > 0 {
|
|
|
|
timeout = options.Timeout
|
2023-10-20 23:51:29 +03:00
|
|
|
}
|
2024-04-14 22:27:28 +03:00
|
|
|
|
|
|
|
if timeout > 0 {
|
2023-10-20 23:51:29 +03:00
|
|
|
var cancel context.CancelFunc
|
2024-04-14 22:27:28 +03:00
|
|
|
ctx, cancel = context.WithTimeout(ctx, timeout)
|
2023-10-20 23:51:29 +03:00
|
|
|
defer cancel()
|
|
|
|
}
|
2024-04-14 22:27:28 +03:00
|
|
|
|
2024-09-25 15:26:52 +03:00
|
|
|
var rkeys []string
|
2024-10-05 16:11:46 +03:00
|
|
|
var pools []*strings.Builder
|
2024-04-14 22:27:28 +03:00
|
|
|
if r.opts.Namespace != "" || options.Namespace != "" {
|
2024-09-25 15:26:52 +03:00
|
|
|
rkeys = make([]string, len(keys))
|
2024-10-05 16:11:46 +03:00
|
|
|
pools = make([]*strings.Builder, len(keys))
|
2024-04-14 22:27:28 +03:00
|
|
|
for idx, key := range keys {
|
2024-10-05 16:11:46 +03:00
|
|
|
b := r.pool.Get()
|
|
|
|
pools[idx] = b
|
|
|
|
rkeys[idx] = r.getKey(b, r.opts.Namespace, options.Namespace, key)
|
2024-04-14 22:27:28 +03:00
|
|
|
}
|
2023-03-30 20:30:31 +03:00
|
|
|
}
|
2024-04-14 22:27:28 +03:00
|
|
|
|
2024-11-27 01:57:39 +03:00
|
|
|
r.opts.Meter.Counter(semconv.StoreRequestInflight, labels...).Inc()
|
2024-04-14 22:27:28 +03:00
|
|
|
ts := time.Now()
|
2024-04-23 09:30:35 +03:00
|
|
|
var err error
|
2024-09-25 15:26:52 +03:00
|
|
|
if r.opts.Namespace != "" || options.Namespace != "" {
|
2024-10-05 16:11:46 +03:00
|
|
|
err = r.cli.Del(ctx, rkeys...).Err()
|
|
|
|
for idx := range pools {
|
|
|
|
r.pool.Put(pools[idx])
|
2024-09-25 15:26:52 +03:00
|
|
|
}
|
2024-04-23 09:30:35 +03:00
|
|
|
} else {
|
2024-10-05 16:11:46 +03:00
|
|
|
err = r.cli.Del(ctx, keys...).Err()
|
2024-04-23 09:30:35 +03:00
|
|
|
}
|
2024-07-05 09:16:08 +03:00
|
|
|
setSpanError(ctx, err)
|
2024-04-14 22:27:28 +03:00
|
|
|
te := time.Since(ts)
|
2024-11-27 01:57:39 +03:00
|
|
|
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())
|
2024-10-05 16:11:46 +03:00
|
|
|
if err == goredis.Nil {
|
2024-11-27 01:57:39 +03:00
|
|
|
r.opts.Meter.Counter(semconv.StoreRequestTotal, append(labels, "status", "miss")...).Inc()
|
2024-04-14 22:27:28 +03:00
|
|
|
return store.ErrNotFound
|
|
|
|
} else if err == nil {
|
2024-11-27 01:57:39 +03:00
|
|
|
r.opts.Meter.Counter(semconv.StoreRequestTotal, append(labels, "status", "hit")...).Inc()
|
2024-04-14 22:27:28 +03:00
|
|
|
} else if err != nil {
|
2024-11-27 01:57:39 +03:00
|
|
|
r.opts.Meter.Counter(semconv.StoreRequestTotal, append(labels, "status", "failure")...).Inc()
|
2024-04-14 22:27:28 +03:00
|
|
|
return err
|
2023-03-30 20:30:31 +03:00
|
|
|
}
|
2024-04-14 22:27:28 +03:00
|
|
|
|
|
|
|
return nil
|
2023-03-30 20:30:31 +03:00
|
|
|
}
|
|
|
|
|
2023-10-20 23:51:29 +03:00
|
|
|
func (r *Store) Delete(ctx context.Context, key string, opts ...store.DeleteOption) error {
|
2024-10-05 16:11:46 +03:00
|
|
|
b := r.pool.Get()
|
|
|
|
defer r.pool.Put(b)
|
|
|
|
|
2024-04-14 22:27:28 +03:00
|
|
|
options := store.NewDeleteOptions(opts...)
|
2024-11-19 00:34:04 +03:00
|
|
|
labels := make([]string, 0, 6)
|
|
|
|
labels = append(labels, "name", options.Name, "statement", "delete")
|
2024-04-14 22:27:28 +03:00
|
|
|
|
|
|
|
timeout := r.opts.Timeout
|
|
|
|
if options.Timeout > 0 {
|
|
|
|
timeout = options.Timeout
|
|
|
|
}
|
|
|
|
|
|
|
|
if timeout > 0 {
|
2023-10-20 23:51:29 +03:00
|
|
|
var cancel context.CancelFunc
|
2024-04-14 22:27:28 +03:00
|
|
|
ctx, cancel = context.WithTimeout(ctx, timeout)
|
2023-10-20 23:51:29 +03:00
|
|
|
defer cancel()
|
|
|
|
}
|
2024-04-14 22:27:28 +03:00
|
|
|
|
2024-11-19 00:34:04 +03:00
|
|
|
r.opts.Meter.Counter(semconv.StoreRequestInflight, labels...).Inc()
|
2024-04-14 22:27:28 +03:00
|
|
|
ts := time.Now()
|
2024-10-05 16:11:46 +03:00
|
|
|
err := r.cli.Del(ctx, r.getKey(b, r.opts.Namespace, options.Namespace, key)).Err()
|
2024-04-14 22:27:28 +03:00
|
|
|
te := time.Since(ts)
|
2024-10-05 16:11:46 +03:00
|
|
|
setSpanError(ctx, err)
|
2024-11-19 00:34:04 +03:00
|
|
|
|
|
|
|
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()
|
2024-04-14 22:27:28 +03:00
|
|
|
return store.ErrNotFound
|
|
|
|
} else if err == nil {
|
2024-11-19 00:34:04 +03:00
|
|
|
r.opts.Meter.Counter(semconv.StoreRequestTotal, append(labels, "status", "hit")...).Inc()
|
2024-04-14 22:27:28 +03:00
|
|
|
} else if err != nil {
|
2024-11-19 00:34:04 +03:00
|
|
|
r.opts.Meter.Counter(semconv.StoreRequestTotal, append(labels, "status", "failure")...).Inc()
|
2024-04-14 22:27:28 +03:00
|
|
|
return err
|
2022-10-26 13:46:48 +03:00
|
|
|
}
|
2024-04-14 22:27:28 +03:00
|
|
|
|
|
|
|
return nil
|
2023-10-20 23:51:29 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func (r *Store) MWrite(ctx context.Context, keys []string, vals []interface{}, opts ...store.WriteOption) error {
|
2024-04-14 22:27:28 +03:00
|
|
|
options := store.NewWriteOptions(opts...)
|
2024-11-27 01:57:39 +03:00
|
|
|
labels := make([]string, 0, 6)
|
|
|
|
labels = append(labels, "name", options.Name, "statement", "write")
|
2024-04-14 22:27:28 +03:00
|
|
|
|
|
|
|
timeout := r.opts.Timeout
|
|
|
|
if options.Timeout > 0 {
|
|
|
|
timeout = options.Timeout
|
2023-10-20 23:51:29 +03:00
|
|
|
}
|
2024-04-14 22:27:28 +03:00
|
|
|
|
|
|
|
if timeout > 0 {
|
2022-07-08 23:17:14 +03:00
|
|
|
var cancel context.CancelFunc
|
2024-04-14 22:27:28 +03:00
|
|
|
ctx, cancel = context.WithTimeout(ctx, timeout)
|
2022-07-08 23:17:14 +03:00
|
|
|
defer cancel()
|
|
|
|
}
|
2023-10-20 23:51:29 +03:00
|
|
|
|
2024-04-14 22:27:28 +03:00
|
|
|
kvs := make([]string, 0, len(keys)*2)
|
2024-10-05 16:11:46 +03:00
|
|
|
pools := make([]*strings.Builder, len(keys))
|
2023-10-20 23:51:29 +03:00
|
|
|
for idx, key := range keys {
|
2024-10-05 16:11:46 +03:00
|
|
|
b := r.pool.Get()
|
|
|
|
pools[idx] = b
|
|
|
|
kvs = append(kvs, r.getKey(b, r.opts.Namespace, options.Namespace, key))
|
2023-10-20 23:51:29 +03:00
|
|
|
|
|
|
|
switch vt := vals[idx].(type) {
|
|
|
|
case string:
|
|
|
|
kvs = append(kvs, vt)
|
|
|
|
case []byte:
|
|
|
|
kvs = append(kvs, string(vt))
|
|
|
|
default:
|
|
|
|
buf, err := r.opts.Codec.Marshal(vt)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
kvs = append(kvs, string(buf))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-11-27 01:57:39 +03:00
|
|
|
r.opts.Meter.Counter(semconv.StoreRequestInflight, labels...).Inc()
|
2024-04-14 22:27:28 +03:00
|
|
|
|
2024-10-05 16:11:46 +03:00
|
|
|
pipeliner := func(pipe goredis.Pipeliner) error {
|
2023-10-20 23:51:29 +03:00
|
|
|
for idx := 0; idx < len(kvs); idx += 2 {
|
2024-04-14 22:27:28 +03:00
|
|
|
if _, err := pipe.Set(ctx, kvs[idx], kvs[idx+1], options.TTL).Result(); err != nil {
|
2024-07-05 09:16:08 +03:00
|
|
|
setSpanError(ctx, err)
|
2024-04-14 22:27:28 +03:00
|
|
|
return err
|
|
|
|
}
|
2023-10-20 23:51:29 +03:00
|
|
|
}
|
|
|
|
return nil
|
2024-04-23 09:30:35 +03:00
|
|
|
}
|
|
|
|
|
2024-10-05 16:11:46 +03:00
|
|
|
ts := time.Now()
|
|
|
|
cmds, err := r.cli.Pipelined(ctx, pipeliner)
|
|
|
|
for idx := range pools {
|
|
|
|
r.pool.Put(pools[idx])
|
2024-04-23 09:30:35 +03:00
|
|
|
}
|
2024-04-14 22:27:28 +03:00
|
|
|
te := time.Since(ts)
|
2024-10-05 16:11:46 +03:00
|
|
|
setSpanError(ctx, err)
|
|
|
|
|
2024-11-27 01:57:39 +03:00
|
|
|
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())
|
2024-10-05 16:11:46 +03:00
|
|
|
if err == goredis.Nil {
|
2024-11-27 01:57:39 +03:00
|
|
|
r.opts.Meter.Counter(semconv.StoreRequestTotal, append(labels, "status", "miss")...).Inc()
|
2024-04-14 22:27:28 +03:00
|
|
|
return store.ErrNotFound
|
|
|
|
} else if err == nil {
|
2024-11-27 01:57:39 +03:00
|
|
|
r.opts.Meter.Counter(semconv.StoreRequestTotal, append(labels, "status", "hit")...).Inc()
|
2024-04-14 22:27:28 +03:00
|
|
|
} else if err != nil {
|
2024-11-27 01:57:39 +03:00
|
|
|
r.opts.Meter.Counter(semconv.StoreRequestTotal, append(labels, "status", "failure")...).Inc()
|
2021-01-16 03:00:25 +03:00
|
|
|
return err
|
2020-05-13 14:43:21 +03:00
|
|
|
}
|
2023-10-20 23:51:29 +03:00
|
|
|
|
|
|
|
for _, cmd := range cmds {
|
|
|
|
if err = cmd.Err(); err != nil {
|
2024-10-05 16:11:46 +03:00
|
|
|
if err == goredis.Nil {
|
2024-11-27 01:57:39 +03:00
|
|
|
r.opts.Meter.Counter(semconv.StoreRequestTotal, append(labels, "status", "miss")...).Inc()
|
2024-04-14 22:27:28 +03:00
|
|
|
return store.ErrNotFound
|
|
|
|
}
|
2024-07-05 09:16:08 +03:00
|
|
|
setSpanError(ctx, err)
|
2024-11-27 01:57:39 +03:00
|
|
|
r.opts.Meter.Counter(semconv.StoreRequestTotal, append(labels, "status", "failure")...).Inc()
|
2023-10-20 23:51:29 +03:00
|
|
|
return err
|
|
|
|
}
|
2022-10-26 13:46:48 +03:00
|
|
|
}
|
2023-10-20 23:51:29 +03:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *Store) Write(ctx context.Context, key string, val interface{}, opts ...store.WriteOption) error {
|
2024-10-05 16:11:46 +03:00
|
|
|
b := r.pool.Get()
|
|
|
|
defer r.pool.Put(b)
|
|
|
|
|
2024-04-14 22:27:28 +03:00
|
|
|
options := store.NewWriteOptions(opts...)
|
2024-11-19 00:34:04 +03:00
|
|
|
labels := make([]string, 0, 6)
|
|
|
|
labels = append(labels, "name", options.Name, "statement", "write")
|
2024-04-14 22:27:28 +03:00
|
|
|
|
|
|
|
timeout := r.opts.Timeout
|
|
|
|
if options.Timeout > 0 {
|
|
|
|
timeout = options.Timeout
|
2022-07-08 23:17:14 +03:00
|
|
|
}
|
2023-10-20 23:51:29 +03:00
|
|
|
|
2024-04-14 22:27:28 +03:00
|
|
|
if timeout > 0 {
|
|
|
|
var cancel context.CancelFunc
|
|
|
|
ctx, cancel = context.WithTimeout(ctx, timeout)
|
|
|
|
defer cancel()
|
2023-10-20 23:51:29 +03:00
|
|
|
}
|
|
|
|
|
2024-10-05 16:11:46 +03:00
|
|
|
rkey := r.getKey(b, r.opts.Namespace, options.Namespace, key)
|
2024-04-14 22:40:30 +03:00
|
|
|
|
2023-10-20 23:51:29 +03:00
|
|
|
var buf []byte
|
|
|
|
switch vt := val.(type) {
|
|
|
|
case string:
|
|
|
|
buf = []byte(vt)
|
|
|
|
case []byte:
|
|
|
|
buf = vt
|
|
|
|
default:
|
|
|
|
var err error
|
|
|
|
buf, err = r.opts.Codec.Marshal(val)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-11-19 00:34:04 +03:00
|
|
|
r.opts.Meter.Counter(semconv.StoreRequestInflight, labels...).Inc()
|
2024-04-14 22:27:28 +03:00
|
|
|
ts := time.Now()
|
2024-10-05 16:11:46 +03:00
|
|
|
err := r.cli.Set(ctx, rkey, buf, options.TTL).Err()
|
2024-04-14 22:27:28 +03:00
|
|
|
te := time.Since(ts)
|
2024-10-05 16:11:46 +03:00
|
|
|
setSpanError(ctx, err)
|
|
|
|
|
2024-11-19 00:34:04 +03:00
|
|
|
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()
|
2024-04-14 22:27:28 +03:00
|
|
|
return store.ErrNotFound
|
|
|
|
} else if err == nil {
|
2024-11-19 00:34:04 +03:00
|
|
|
r.opts.Meter.Counter(semconv.StoreRequestTotal, append(labels, "status", "hit")...).Inc()
|
2024-11-27 01:57:39 +03:00
|
|
|
} else {
|
2024-11-19 00:34:04 +03:00
|
|
|
r.opts.Meter.Counter(semconv.StoreRequestTotal, append(labels, "status", "failure")...).Inc()
|
2024-04-14 22:27:28 +03:00
|
|
|
return err
|
2023-10-20 23:51:29 +03:00
|
|
|
}
|
|
|
|
|
2024-04-14 22:27:28 +03:00
|
|
|
return err
|
2019-10-03 11:57:49 +03:00
|
|
|
}
|
|
|
|
|
2023-10-20 23:51:29 +03:00
|
|
|
func (r *Store) List(ctx context.Context, opts ...store.ListOption) ([]string, error) {
|
2024-10-05 16:11:46 +03:00
|
|
|
b := r.pool.Get()
|
|
|
|
defer r.pool.Put(b)
|
|
|
|
|
2021-01-16 03:00:25 +03:00
|
|
|
options := store.NewListOptions(opts...)
|
2024-11-27 01:57:39 +03:00
|
|
|
labels := make([]string, 0, 6)
|
|
|
|
labels = append(labels, "name", options.Name, "statement", "list")
|
|
|
|
|
2021-11-24 15:23:47 +03:00
|
|
|
if len(options.Namespace) == 0 {
|
|
|
|
options.Namespace = r.opts.Namespace
|
2020-05-13 14:43:21 +03:00
|
|
|
}
|
2022-10-26 13:46:48 +03:00
|
|
|
|
2024-10-05 16:11:46 +03:00
|
|
|
rkey := r.getKey(b, options.Namespace, "", options.Prefix+"*")
|
2022-10-26 13:46:48 +03:00
|
|
|
if options.Suffix != "" {
|
|
|
|
rkey += options.Suffix
|
|
|
|
}
|
2024-04-14 22:27:28 +03:00
|
|
|
|
|
|
|
timeout := r.opts.Timeout
|
|
|
|
if options.Timeout > 0 {
|
|
|
|
timeout = options.Timeout
|
|
|
|
}
|
|
|
|
|
|
|
|
if timeout > 0 {
|
2022-07-08 23:17:14 +03:00
|
|
|
var cancel context.CancelFunc
|
2024-04-14 22:27:28 +03:00
|
|
|
ctx, cancel = context.WithTimeout(ctx, timeout)
|
2022-07-08 23:17:14 +03:00
|
|
|
defer cancel()
|
|
|
|
}
|
2024-04-14 22:27:28 +03:00
|
|
|
|
2021-01-16 03:00:25 +03:00
|
|
|
// TODO: add support for prefix/suffix/limit
|
2024-11-27 01:57:39 +03:00
|
|
|
r.opts.Meter.Counter(semconv.StoreRequestInflight, labels...).Inc()
|
2024-04-14 22:27:28 +03:00
|
|
|
ts := time.Now()
|
2024-04-23 09:30:35 +03:00
|
|
|
var keys []string
|
|
|
|
var err error
|
|
|
|
|
2024-10-05 16:11:46 +03:00
|
|
|
if c, ok := r.cli.(*goredis.ClusterClient); ok {
|
|
|
|
err = c.ForEachMaster(ctx, func(nctx context.Context, cli *goredis.Client) error {
|
2024-04-23 09:30:35 +03:00
|
|
|
nkeys, nerr := cli.Keys(nctx, rkey).Result()
|
|
|
|
if nerr != nil {
|
|
|
|
return nerr
|
|
|
|
}
|
|
|
|
keys = append(keys, nkeys...)
|
|
|
|
return nil
|
|
|
|
})
|
2024-10-05 16:11:46 +03:00
|
|
|
} else {
|
|
|
|
keys, err = r.cli.Keys(ctx, rkey).Result()
|
2024-04-23 09:30:35 +03:00
|
|
|
}
|
2024-04-14 22:27:28 +03:00
|
|
|
te := time.Since(ts)
|
2024-10-05 16:11:46 +03:00
|
|
|
setSpanError(ctx, err)
|
|
|
|
|
2024-11-27 01:57:39 +03:00
|
|
|
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())
|
2024-10-05 16:11:46 +03:00
|
|
|
if err == goredis.Nil {
|
2024-11-27 01:57:39 +03:00
|
|
|
r.opts.Meter.Counter(semconv.StoreRequestTotal, append(labels, "status", "miss")...).Inc()
|
2024-04-14 22:27:28 +03:00
|
|
|
return nil, store.ErrNotFound
|
|
|
|
} else if err == nil {
|
2024-11-27 01:57:39 +03:00
|
|
|
r.opts.Meter.Counter(semconv.StoreRequestTotal, append(labels, "status", "git")...).Inc()
|
2024-04-14 22:27:28 +03:00
|
|
|
} else if err != nil {
|
2024-11-27 01:57:39 +03:00
|
|
|
r.opts.Meter.Counter(semconv.StoreRequestTotal, append(labels, "status", "failure")...).Inc()
|
2019-10-03 11:57:49 +03:00
|
|
|
return nil, err
|
|
|
|
}
|
2024-04-14 22:27:28 +03:00
|
|
|
|
|
|
|
prefix := r.opts.Namespace
|
|
|
|
if options.Namespace != "" {
|
|
|
|
prefix = options.Namespace
|
|
|
|
}
|
|
|
|
if prefix == "" {
|
2022-10-26 13:46:48 +03:00
|
|
|
return keys, nil
|
|
|
|
}
|
2019-12-04 13:36:30 +03:00
|
|
|
|
2024-04-14 22:27:28 +03:00
|
|
|
for idx, key := range keys {
|
|
|
|
keys[idx] = strings.TrimPrefix(key, prefix)
|
2022-10-26 13:46:48 +03:00
|
|
|
}
|
2024-04-14 22:27:28 +03:00
|
|
|
|
|
|
|
return keys, nil
|
2020-03-13 01:44:40 +03:00
|
|
|
}
|
|
|
|
|
2023-10-20 23:51:29 +03:00
|
|
|
func (r *Store) Options() store.Options {
|
2021-01-16 03:00:25 +03:00
|
|
|
return r.opts
|
2019-10-03 11:57:49 +03:00
|
|
|
}
|
|
|
|
|
2023-10-20 23:51:29 +03:00
|
|
|
func (r *Store) Name() string {
|
2021-01-29 22:47:57 +03:00
|
|
|
return r.opts.Name
|
|
|
|
}
|
|
|
|
|
2023-10-20 23:51:29 +03:00
|
|
|
func (r *Store) String() string {
|
2019-10-03 11:57:49 +03:00
|
|
|
return "redis"
|
|
|
|
}
|
|
|
|
|
2023-10-20 23:51:29 +03:00
|
|
|
func NewStore(opts ...store.Option) *Store {
|
2024-12-01 19:58:11 +03:00
|
|
|
b := atomic.Uint32{}
|
2024-11-27 01:57:39 +03:00
|
|
|
return &Store{
|
|
|
|
opts: store.NewOptions(opts...),
|
|
|
|
connected: &b,
|
2024-12-01 19:58:11 +03:00
|
|
|
watchers: make(map[string]*watcher),
|
2024-11-27 01:57:39 +03:00
|
|
|
}
|
2020-05-13 12:59:23 +03:00
|
|
|
}
|
|
|
|
|
2023-10-20 23:51:29 +03:00
|
|
|
func (r *Store) configure() error {
|
2023-03-17 12:40:50 +03:00
|
|
|
if r.cli != nil && r.opts.Context == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2024-10-08 16:52:26 +03:00
|
|
|
universalOptions := DefaultUniversalOptions
|
|
|
|
|
2021-01-16 03:00:25 +03:00
|
|
|
if r.opts.Context != nil {
|
2024-10-05 16:11:46 +03:00
|
|
|
if o, ok := r.opts.Context.Value(configKey{}).(*goredis.Options); ok {
|
|
|
|
universalOptions.Addrs = []string{o.Addr}
|
|
|
|
universalOptions.Dialer = o.Dialer
|
|
|
|
universalOptions.OnConnect = o.OnConnect
|
|
|
|
universalOptions.Username = o.Username
|
|
|
|
universalOptions.Password = o.Password
|
|
|
|
|
|
|
|
universalOptions.MaxRetries = o.MaxRetries
|
|
|
|
universalOptions.MinRetryBackoff = o.MinRetryBackoff
|
|
|
|
universalOptions.MaxRetryBackoff = o.MaxRetryBackoff
|
|
|
|
|
|
|
|
universalOptions.DialTimeout = o.DialTimeout
|
|
|
|
universalOptions.ReadTimeout = o.ReadTimeout
|
|
|
|
universalOptions.WriteTimeout = o.WriteTimeout
|
|
|
|
universalOptions.ContextTimeoutEnabled = o.ContextTimeoutEnabled
|
|
|
|
|
|
|
|
universalOptions.PoolFIFO = o.PoolFIFO
|
|
|
|
|
|
|
|
universalOptions.PoolSize = o.PoolSize
|
|
|
|
universalOptions.PoolTimeout = o.PoolTimeout
|
|
|
|
universalOptions.MinIdleConns = o.MinIdleConns
|
|
|
|
universalOptions.MaxIdleConns = o.MaxIdleConns
|
|
|
|
universalOptions.ConnMaxIdleTime = o.ConnMaxIdleTime
|
|
|
|
universalOptions.ConnMaxLifetime = o.ConnMaxLifetime
|
|
|
|
|
2023-02-01 01:48:47 +03:00
|
|
|
if r.opts.TLSConfig != nil {
|
2024-10-05 16:11:46 +03:00
|
|
|
universalOptions.TLSConfig = r.opts.TLSConfig
|
2023-02-01 01:48:47 +03:00
|
|
|
}
|
2021-01-16 03:00:25 +03:00
|
|
|
}
|
|
|
|
|
2024-10-05 16:11:46 +03:00
|
|
|
if o, ok := r.opts.Context.Value(clusterConfigKey{}).(*goredis.ClusterOptions); ok {
|
|
|
|
universalOptions.Addrs = o.Addrs
|
|
|
|
universalOptions.Dialer = o.Dialer
|
|
|
|
universalOptions.OnConnect = o.OnConnect
|
|
|
|
universalOptions.Username = o.Username
|
|
|
|
universalOptions.Password = o.Password
|
|
|
|
|
|
|
|
universalOptions.MaxRedirects = o.MaxRedirects
|
|
|
|
universalOptions.ReadOnly = o.ReadOnly
|
|
|
|
universalOptions.RouteByLatency = o.RouteByLatency
|
|
|
|
universalOptions.RouteRandomly = o.RouteRandomly
|
|
|
|
|
|
|
|
universalOptions.MaxRetries = o.MaxRetries
|
|
|
|
universalOptions.MinRetryBackoff = o.MinRetryBackoff
|
|
|
|
universalOptions.MaxRetryBackoff = o.MaxRetryBackoff
|
|
|
|
|
|
|
|
universalOptions.DialTimeout = o.DialTimeout
|
|
|
|
universalOptions.ReadTimeout = o.ReadTimeout
|
|
|
|
universalOptions.WriteTimeout = o.WriteTimeout
|
|
|
|
universalOptions.ContextTimeoutEnabled = o.ContextTimeoutEnabled
|
|
|
|
|
|
|
|
universalOptions.PoolFIFO = o.PoolFIFO
|
|
|
|
|
|
|
|
universalOptions.PoolSize = o.PoolSize
|
|
|
|
universalOptions.PoolTimeout = o.PoolTimeout
|
|
|
|
universalOptions.MinIdleConns = o.MinIdleConns
|
|
|
|
universalOptions.MaxIdleConns = o.MaxIdleConns
|
|
|
|
universalOptions.ConnMaxIdleTime = o.ConnMaxIdleTime
|
|
|
|
universalOptions.ConnMaxLifetime = o.ConnMaxLifetime
|
2023-02-01 01:48:47 +03:00
|
|
|
if r.opts.TLSConfig != nil {
|
2024-10-05 16:11:46 +03:00
|
|
|
universalOptions.TLSConfig = r.opts.TLSConfig
|
2023-02-01 01:48:47 +03:00
|
|
|
}
|
2020-07-06 15:59:32 +03:00
|
|
|
}
|
|
|
|
|
2024-10-05 16:11:46 +03:00
|
|
|
if o, ok := r.opts.Context.Value(universalConfigKey{}).(*goredis.UniversalOptions); ok {
|
|
|
|
universalOptions = o
|
|
|
|
if r.opts.TLSConfig != nil {
|
|
|
|
universalOptions.TLSConfig = r.opts.TLSConfig
|
|
|
|
}
|
|
|
|
}
|
2021-01-16 03:00:25 +03:00
|
|
|
}
|
|
|
|
|
2024-10-05 16:11:46 +03:00
|
|
|
if len(r.opts.Addrs) > 0 {
|
|
|
|
universalOptions.Addrs = r.opts.Addrs
|
|
|
|
} else {
|
2024-10-06 18:49:50 +03:00
|
|
|
universalOptions.Addrs = []string{"127.0.0.1:6379"}
|
2021-01-16 03:00:25 +03:00
|
|
|
}
|
|
|
|
|
2024-10-05 16:11:46 +03:00
|
|
|
r.cli = goredis.NewUniversalClient(universalOptions)
|
|
|
|
setTracing(r.cli, r.opts.Tracer)
|
2024-12-01 19:58:11 +03:00
|
|
|
r.cli.AddHook(newEventHook(r))
|
2020-05-13 12:59:23 +03:00
|
|
|
|
2024-10-05 16:11:46 +03:00
|
|
|
r.pool = pool.NewStringsPool(50)
|
2024-07-04 15:13:19 +03:00
|
|
|
|
|
|
|
r.statsMeter()
|
|
|
|
|
2020-05-13 12:59:23 +03:00
|
|
|
return nil
|
2019-10-03 11:57:49 +03:00
|
|
|
}
|
2024-04-14 22:27:28 +03:00
|
|
|
|
2024-10-05 16:11:46 +03:00
|
|
|
func (r *Store) getKey(b *strings.Builder, mainNamespace string, opNamespace string, key string) string {
|
2024-04-14 22:27:28 +03:00
|
|
|
if opNamespace == "" {
|
|
|
|
opNamespace = mainNamespace
|
|
|
|
}
|
|
|
|
if opNamespace != "" {
|
|
|
|
b.WriteString(opNamespace)
|
2024-12-01 19:58:11 +03:00
|
|
|
b.WriteString(r.opts.Separator)
|
2024-04-14 22:27:28 +03:00
|
|
|
}
|
|
|
|
b.WriteString(key)
|
|
|
|
return b.String()
|
|
|
|
}
|
2024-12-01 19:58:11 +03:00
|
|
|
|
|
|
|
func (r *Store) Watch(ctx context.Context, opts ...store.WatchOption) (store.Watcher, error) {
|
|
|
|
id, err := id.New()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
wo, err := store.NewWatchOptions(opts...)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
// construct the watcher
|
|
|
|
w := &watcher{
|
|
|
|
exit: make(chan bool),
|
|
|
|
ch: make(chan store.Event),
|
|
|
|
id: id,
|
|
|
|
opts: wo,
|
|
|
|
}
|
|
|
|
|
|
|
|
r.mu.Lock()
|
|
|
|
r.watchers[w.id] = w
|
|
|
|
r.mu.Unlock()
|
|
|
|
|
|
|
|
return w, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *Store) sendEvent(e store.Event) {
|
|
|
|
r.mu.RLock()
|
|
|
|
watchers := make([]*watcher, 0, len(r.watchers))
|
|
|
|
for _, w := range r.watchers {
|
|
|
|
watchers = append(watchers, w)
|
|
|
|
}
|
|
|
|
r.mu.RUnlock()
|
|
|
|
for _, w := range watchers {
|
|
|
|
select {
|
|
|
|
case <-w.exit:
|
|
|
|
r.mu.Lock()
|
|
|
|
delete(r.watchers, w.id)
|
|
|
|
r.mu.Unlock()
|
|
|
|
default:
|
|
|
|
select {
|
|
|
|
case w.ch <- e:
|
|
|
|
case <-time.After(sendEventTime):
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type watcher struct {
|
|
|
|
ch chan store.Event
|
|
|
|
exit chan bool
|
|
|
|
opts store.WatchOptions
|
|
|
|
id string
|
|
|
|
}
|
|
|
|
|
|
|
|
func (w *watcher) Next() (store.Event, error) {
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case e := <-w.ch:
|
|
|
|
return e, nil
|
|
|
|
case <-w.exit:
|
|
|
|
return nil, store.ErrWatcherStopped
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (w *watcher) Stop() {
|
|
|
|
select {
|
|
|
|
case <-w.exit:
|
|
|
|
return
|
|
|
|
default:
|
|
|
|
close(w.exit)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type event struct {
|
|
|
|
ts time.Time
|
|
|
|
t store.EventType
|
|
|
|
err error
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e *event) Error() error {
|
|
|
|
return e.err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e *event) Timestamp() time.Time {
|
|
|
|
return e.ts
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e *event) Type() store.EventType {
|
|
|
|
return e.t
|
|
|
|
}
|