mirror of
https://github.com/tiennm99/keepalive.git
synced 2026-10-14 00:16:49 +00:00
refactor(adapter)!: drop the valkey adapter in favor of redis
Valkey speaks the Redis protocol, so the redis adapter (go-redis) already serves it. Dropping valkey-go removes a dependency and shrinks the stripped binary by about 9 MB. The redis adapter now also closes a client that failed to connect only once. BREAKING CHANGE: `adapter: valkey` is no longer accepted. Use `adapter: redis` with a redis:// or rediss:// URL instead of valkey:// or valkeys://. The namespace option now applies to these services.
This commit is contained in:
1 parent
77b9a9673b
commit
cbcee9ccd8
6 files changed
+15
-70
No files matched your search
+8
-5
@@ -20,6 +20,8 @@ func init() {
|
||||
}
|
||||
}
|
||||
|
||||
// redisAdapter serves every RESP-compatible store: Redis, Valkey, Dragonfly,
|
||||
// KeyDB, Garnet and their hosted variants.
|
||||
type redisAdapter struct {
|
||||
client *redis.Client
|
||||
url string
|
||||
@@ -31,15 +33,16 @@ func (a *redisAdapter) Connect(ctx context.Context) error {
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
a.client = redis.NewClient(opt)
|
||||
if err := a.client.Ping(ctx).Err(); err != nil {
|
||||
a.client.Close()
|
||||
client := redis.NewClient(opt)
|
||||
if err := client.Ping(ctx).Err(); err != nil {
|
||||
client.Close()
|
||||
return err
|
||||
}
|
||||
if err := a.client.SetNX(ctx, a.key, 0, 0).Err(); err != nil {
|
||||
a.client.Close()
|
||||
if err := client.SetNX(ctx, a.key, 0, 0).Err(); err != nil {
|
||||
client.Close()
|
||||
return err
|
||||
}
|
||||
a.client = client
|
||||
return nil
|
||||
}
|
||||
|
||||
|
||||
@@ -1,55 +0,0 @@
|
||||
package adapter
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/valkey-io/valkey-go"
|
||||
)
|
||||
|
||||
func init() {
|
||||
Registry["valkey"] = func(cfg Config) (Adapter, error) {
|
||||
url, err := cfg.Required("url")
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &valkeyAdapter{
|
||||
url: url,
|
||||
key: cfg.Optional("counter_key", "counter"),
|
||||
}, nil
|
||||
}
|
||||
}
|
||||
|
||||
type valkeyAdapter struct {
|
||||
client valkey.Client
|
||||
url string
|
||||
key string
|
||||
}
|
||||
|
||||
func (a *valkeyAdapter) Connect(ctx context.Context) error {
|
||||
opt, err := valkey.ParseURL(a.url)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
client, err := valkey.NewClient(opt)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
a.client = client
|
||||
if err := a.client.Do(ctx, a.client.B().Setnx().Key(a.key).Value("0").Build()).Error(); err != nil {
|
||||
a.client.Close()
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (a *valkeyAdapter) Increment(ctx context.Context) (int64, error) {
|
||||
return a.client.Do(ctx, a.client.B().Incr().Key(a.key).Build()).AsInt64()
|
||||
}
|
||||
|
||||
func (a *valkeyAdapter) Close(_ context.Context) error {
|
||||
if a.client == nil {
|
||||
return nil
|
||||
}
|
||||
a.client.Close()
|
||||
return nil
|
||||
}
|
||||
Reference in new issue
Block a user