github.com/lingyao2333/mo-zero@v1.4.1/core/stores/redis/redisblockingnode.go (about) 1 package redis 2 3 import ( 4 "fmt" 5 6 red "github.com/go-redis/redis/v8" 7 "github.com/lingyao2333/mo-zero/core/logx" 8 ) 9 10 // ClosableNode interface represents a closable redis node. 11 type ClosableNode interface { 12 RedisNode 13 Close() 14 } 15 16 // CreateBlockingNode returns a ClosableNode. 17 func CreateBlockingNode(r *Redis) (ClosableNode, error) { 18 timeout := readWriteTimeout + blockingQueryTimeout 19 20 switch r.Type { 21 case NodeType: 22 client := red.NewClient(&red.Options{ 23 Addr: r.Addr, 24 Password: r.Pass, 25 DB: defaultDatabase, 26 MaxRetries: maxRetries, 27 PoolSize: 1, 28 MinIdleConns: 1, 29 ReadTimeout: timeout, 30 }) 31 return &clientBridge{client}, nil 32 case ClusterType: 33 client := red.NewClusterClient(&red.ClusterOptions{ 34 Addrs: []string{r.Addr}, 35 Password: r.Pass, 36 MaxRetries: maxRetries, 37 PoolSize: 1, 38 MinIdleConns: 1, 39 ReadTimeout: timeout, 40 }) 41 return &clusterBridge{client}, nil 42 default: 43 return nil, fmt.Errorf("unknown redis type: %s", r.Type) 44 } 45 } 46 47 type ( 48 clientBridge struct { 49 *red.Client 50 } 51 52 clusterBridge struct { 53 *red.ClusterClient 54 } 55 ) 56 57 func (bridge *clientBridge) Close() { 58 if err := bridge.Client.Close(); err != nil { 59 logx.Errorf("Error occurred on close redis client: %s", err) 60 } 61 } 62 63 func (bridge *clusterBridge) Close() { 64 if err := bridge.ClusterClient.Close(); err != nil { 65 logx.Errorf("Error occurred on close redis cluster: %s", err) 66 } 67 }