-
Notifications
You must be signed in to change notification settings - Fork 2
/
client.go
104 lines (88 loc) · 2.88 KB
/
client.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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
package redis
import (
"context"
"github.com/americanas-go/log"
"github.com/go-redis/redis/v8"
)
// Plugin defines a function to process plugin.
type Plugin func(context.Context, *redis.Client) error
// NewClient returns a new redis client.
func NewClient(ctx context.Context, plugins ...Plugin) (*redis.Client, error) {
o, err := NewOptions()
if err != nil {
return nil, err
}
return NewClientWithOptions(ctx, o, plugins...)
}
// NewClientWithConfigPath returns a new redis client with options from config path.
func NewClientWithConfigPath(ctx context.Context, path string, plugins ...Plugin) (*redis.Client, error) {
opts, err := NewOptionsWithPath(path)
if err != nil {
return nil, err
}
return NewClientWithOptions(ctx, opts, plugins...)
}
// NewClientWithOptions returns a new redis client with options.
func NewClientWithOptions(ctx context.Context, o *Options, plugins ...Plugin) (client *redis.Client, err error) {
logger := log.FromContext(ctx)
if redisSentinel(o) {
client = failOverClient(o)
} else {
client = standaloneClient(o)
}
ping := client.Conn(ctx).Ping(ctx)
if ping.Err() != nil {
return nil, ping.Err()
}
for _, plugin := range plugins {
if err := plugin(ctx, client); err != nil {
return nil, err
}
}
logger.Infof("Connected to Redis server: %s %s", client.Options().Addr, ping.String())
return client, err
}
func failOverClient(o *Options) *redis.Client {
return redis.NewFailoverClient(&redis.FailoverOptions{
MasterName: o.Sentinel.MasterName,
SentinelAddrs: o.Sentinel.Addrs,
SentinelPassword: o.Sentinel.Password,
Password: o.Password,
MaxRetries: o.MaxRetries,
MinRetryBackoff: o.MinRetryBackoff,
MaxRetryBackoff: o.MaxRetryBackoff,
DialTimeout: o.DialTimeout,
DB: o.Client.DB,
ReadTimeout: o.ReadTimeout,
WriteTimeout: o.WriteTimeout,
PoolSize: o.PoolSize,
MinIdleConns: o.MinIdleConns,
MaxConnAge: o.MaxConnAge,
PoolTimeout: o.PoolTimeout,
IdleTimeout: o.IdleTimeout,
IdleCheckFrequency: o.IdleCheckFrequency,
})
}
func standaloneClient(o *Options) *redis.Client {
return redis.NewClient(&redis.Options{
Addr: o.Client.Addr,
Network: o.Client.Network,
Password: o.Password,
MaxRetries: o.MaxRetries,
MinRetryBackoff: o.MinRetryBackoff,
MaxRetryBackoff: o.MaxRetryBackoff,
DialTimeout: o.DialTimeout,
DB: o.Client.DB,
ReadTimeout: o.ReadTimeout,
WriteTimeout: o.WriteTimeout,
PoolSize: o.PoolSize,
MinIdleConns: o.MinIdleConns,
MaxConnAge: o.MaxConnAge,
PoolTimeout: o.PoolTimeout,
IdleTimeout: o.IdleTimeout,
IdleCheckFrequency: o.IdleCheckFrequency,
})
}
func redisSentinel(o *Options) bool {
return o.Sentinel.MasterName != "" || o.Sentinel.Addrs != nil
}