forked from bitleak/go-redis-pool
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhooks.go
55 lines (46 loc) · 1010 Bytes
/
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
package pool
import (
"context"
"net"
"github.com/go-redis/redis/v8"
)
type failureHook struct {
*client
}
func newFailureHook(c *client) *failureHook {
return &failureHook{
client: c,
}
}
func (hook *failureHook) BeforeProcess(ctx context.Context, cmd redis.Cmder) (context.Context, error) {
return ctx, nil
}
func (hook *failureHook) AfterProcess(ctx context.Context, cmd redis.Cmder) error {
if isNetworkError(cmd.Err()) {
hook.onFailure()
} else {
hook.onSuccess()
}
return nil
}
func (hook *failureHook) BeforeProcessPipeline(ctx context.Context, cmds []redis.Cmder) (context.Context, error) {
return ctx, nil
}
func (hook *failureHook) AfterProcessPipeline(ctx context.Context, cmds []redis.Cmder) error {
for _, cmd := range cmds {
if isNetworkError(cmd.Err()) {
hook.client.onFailure()
return nil
}
}
hook.onSuccess()
return nil
}
func isNetworkError(err error) bool {
if err == nil {
return false
}
// Network error
_, ok := err.(net.Error)
return ok
}