-
Notifications
You must be signed in to change notification settings - Fork 18
/
hooks.go
65 lines (50 loc) · 1.09 KB
/
hooks.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
package pool
import (
"context"
"net"
"github.com/redis/go-redis/v9"
)
type failureHook struct {
*client
}
var _ redis.Hook = failureHook{}
func newFailureHook(c *client) *failureHook {
return &failureHook{
client: c,
}
}
func (h failureHook) DialHook(next redis.DialHook) redis.DialHook {
return func(ctx context.Context, network, addr string) (net.Conn, error) {
conn, err := next(ctx, network, addr)
if isNetworkError(err) {
h.onFailure()
}
return conn, err
}
}
func (h failureHook) ProcessHook(next redis.ProcessHook) redis.ProcessHook {
return func(ctx context.Context, cmd redis.Cmder) error {
err := next(ctx, cmd)
if !isNetworkError(err) {
h.onSuccess()
}
return err
}
}
func (h failureHook) ProcessPipelineHook(next redis.ProcessPipelineHook) redis.ProcessPipelineHook {
return func(ctx context.Context, cmds []redis.Cmder) error {
err := next(ctx, cmds)
if !isNetworkError(err) {
h.onSuccess()
}
return err
}
}
func isNetworkError(err error) bool {
if err == nil {
return false
}
// Network error
_, ok := err.(net.Error)
return ok
}