2024-11-27 01:57:39 +03:00
|
|
|
package redis
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"errors"
|
|
|
|
"net"
|
|
|
|
"sync/atomic"
|
|
|
|
|
|
|
|
goredis "github.com/redis/go-redis/v9"
|
|
|
|
)
|
|
|
|
|
|
|
|
type eventHook struct {
|
|
|
|
connected *atomic.Bool
|
|
|
|
}
|
|
|
|
|
|
|
|
var _ goredis.Hook = (*eventHook)(nil)
|
|
|
|
|
|
|
|
func newEventHook(connected *atomic.Bool) *eventHook {
|
|
|
|
return &eventHook{connected: connected}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (h *eventHook) DialHook(hook goredis.DialHook) goredis.DialHook {
|
|
|
|
return func(ctx context.Context, network, addr string) (net.Conn, error) {
|
|
|
|
conn, err := hook(ctx, network, addr)
|
2024-11-28 09:49:29 +03:00
|
|
|
if err != nil {
|
|
|
|
if !isRedisError(err) {
|
|
|
|
h.connected.Store(false)
|
|
|
|
}
|
|
|
|
h.connected.Store(true)
|
|
|
|
} else {
|
|
|
|
h.connected.Store(true)
|
2024-11-27 01:57:39 +03:00
|
|
|
}
|
|
|
|
return conn, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (h *eventHook) ProcessHook(hook goredis.ProcessHook) goredis.ProcessHook {
|
|
|
|
return func(ctx context.Context, cmd goredis.Cmder) error {
|
|
|
|
err := hook(ctx, cmd)
|
2024-11-28 09:53:03 +03:00
|
|
|
if err != nil {
|
|
|
|
if !isRedisError(err) {
|
|
|
|
h.connected.Store(false)
|
|
|
|
}
|
|
|
|
h.connected.Store(true)
|
|
|
|
} else {
|
|
|
|
h.connected.Store(true)
|
2024-11-27 01:57:39 +03:00
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (h *eventHook) ProcessPipelineHook(hook goredis.ProcessPipelineHook) goredis.ProcessPipelineHook {
|
|
|
|
return func(ctx context.Context, cmds []goredis.Cmder) error {
|
|
|
|
err := hook(ctx, cmds)
|
2024-11-28 09:53:03 +03:00
|
|
|
if err != nil {
|
|
|
|
if !isRedisError(err) {
|
|
|
|
h.connected.Store(false)
|
|
|
|
}
|
|
|
|
h.connected.Store(true)
|
|
|
|
} else {
|
|
|
|
h.connected.Store(true)
|
2024-11-27 01:57:39 +03:00
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func isRedisError(err error) bool {
|
|
|
|
var rerr goredis.Error
|
|
|
|
return errors.As(err, &rerr)
|
|
|
|
}
|