mirror of
https://github.com/tiennm99/keepalive.git
synced 2026-10-11 03:13:31 +00:00
- Generated service names come only from a URL host, a key=value DSN's host=, or a MySQL tcp() address, so a password in a key=value DSN can no longer end up in the name printed on every log line. - connect_timeout is added only to postgres:// and postgresql:// URLs (parsed, so it never lands after a fragment) or as a key=value token, never glued onto a key=value value containing "://". - Driver parse errors that quote password fragments (mongo escape errors, lib/pq's missing "=" error) are replaced with generic hints. - MongoDB keeps its client only after a successful connect, so a failed connect is not disconnected twice. - Couchbase gets ready_timeout plus 1 minute to connect, so raising ready_timeout takes effect. - Shutdown waits at most 7 seconds, inside Docker's 10-second grace period. - Each adapter declares its config keys; unknown keys anywhere in the file, including under config, log a warning without blocking start.
76 lines
1.7 KiB
Go
76 lines
1.7 KiB
Go
package adapter
|
|
|
|
import (
|
|
"context"
|
|
"strings"
|
|
|
|
"github.com/redis/go-redis/v9"
|
|
)
|
|
|
|
// discardRedisLogger silences go-redis's internal logger. It logs every
|
|
// failed dial attempt (several per connect), while the runner already logs
|
|
// the returned error once per retry.
|
|
type discardRedisLogger struct{}
|
|
|
|
func (discardRedisLogger) Printf(context.Context, string, ...interface{}) {}
|
|
|
|
func init() {
|
|
redis.SetLogger(discardRedisLogger{})
|
|
ConfigKeys["redis"] = []string{"url", "namespace"}
|
|
Registry["redis"] = func(cfg Config) (Adapter, error) {
|
|
url, err := cfg.Required("url")
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return &redisAdapter{
|
|
url: url,
|
|
key: redisCounterKey(cfg.Optional("counter_key", "counter"), cfg.Optional("namespace", "")),
|
|
}, nil
|
|
}
|
|
}
|
|
|
|
// redisAdapter serves every RESP-compatible store: Redis, Valkey, Dragonfly,
|
|
// KeyDB, Garnet and their hosted variants.
|
|
type redisAdapter struct {
|
|
client *redis.Client
|
|
url string
|
|
key string
|
|
}
|
|
|
|
func (a *redisAdapter) Connect(ctx context.Context) error {
|
|
opt, err := redis.ParseURL(a.url)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
client := redis.NewClient(opt)
|
|
if err := client.Ping(ctx).Err(); err != nil {
|
|
client.Close()
|
|
return err
|
|
}
|
|
if err := client.SetNX(ctx, a.key, 0, 0).Err(); err != nil {
|
|
client.Close()
|
|
return err
|
|
}
|
|
a.client = client
|
|
return nil
|
|
}
|
|
|
|
func (a *redisAdapter) Increment(ctx context.Context) (int64, error) {
|
|
return a.client.Incr(ctx, a.key).Result()
|
|
}
|
|
|
|
func (a *redisAdapter) Close(_ context.Context) error {
|
|
if a.client == nil {
|
|
return nil
|
|
}
|
|
return a.client.Close()
|
|
}
|
|
|
|
func redisCounterKey(counterKey, namespace string) string {
|
|
namespace = strings.TrimSpace(namespace)
|
|
if namespace == "" {
|
|
return counterKey
|
|
}
|
|
return namespace + ":" + counterKey
|
|
}
|