forked from zeromicro/go-zero
/
redisblockingnode.go
65 lines (56 loc) · 1.29 KB
/
redisblockingnode.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 redis
import (
"fmt"
red "github.com/go-redis/redis"
"github.com/tal-tech/go-zero/core/logx"
)
type ClosableNode interface {
RedisNode
Close()
}
func CreateBlockingNode(r *Redis) (ClosableNode, error) {
timeout := readWriteTimeout + blockingQueryTimeout
switch r.Type {
case NodeType:
client := red.NewClient(&red.Options{
Addr: r.Addr,
Password: r.Pass,
DB: defaultDatabase,
MaxRetries: maxRetries,
PoolSize: 1,
MinIdleConns: 1,
ReadTimeout: timeout,
})
return &clientBridge{client}, nil
case ClusterType:
client := red.NewClusterClient(&red.ClusterOptions{
Addrs: []string{r.Addr},
Password: r.Pass,
MaxRetries: maxRetries,
PoolSize: 1,
MinIdleConns: 1,
ReadTimeout: timeout,
})
return &clusterBridge{client}, nil
default:
return nil, fmt.Errorf("unknown redis type: %s", r.Type)
}
}
type (
clientBridge struct {
*red.Client
}
clusterBridge struct {
*red.ClusterClient
}
)
func (bridge *clientBridge) Close() {
if err := bridge.Client.Close(); err != nil {
logx.Errorf("Error occurred on close redis client: %s", err)
}
}
func (bridge *clusterBridge) Close() {
if err := bridge.ClusterClient.Close(); err != nil {
logx.Errorf("Error occurred on close redis cluster: %s", err)
}
}