barometer: update DMA's vendoring packages
[barometer.git] / src / dma / vendor / github.com / go-redis / redis / universal.go
1 package redis
2
3 import (
4         "crypto/tls"
5         "time"
6 )
7
8 // UniversalOptions information is required by UniversalClient to establish
9 // connections.
10 type UniversalOptions struct {
11         // Either a single address or a seed list of host:port addresses
12         // of cluster/sentinel nodes.
13         Addrs []string
14
15         // Database to be selected after connecting to the server.
16         // Only single-node and failover clients.
17         DB int
18
19         // Common options.
20
21         OnConnect          func(*Conn) error
22         Password           string
23         MaxRetries         int
24         MinRetryBackoff    time.Duration
25         MaxRetryBackoff    time.Duration
26         DialTimeout        time.Duration
27         ReadTimeout        time.Duration
28         WriteTimeout       time.Duration
29         PoolSize           int
30         MinIdleConns       int
31         MaxConnAge         time.Duration
32         PoolTimeout        time.Duration
33         IdleTimeout        time.Duration
34         IdleCheckFrequency time.Duration
35         TLSConfig          *tls.Config
36
37         // Only cluster clients.
38
39         MaxRedirects   int
40         ReadOnly       bool
41         RouteByLatency bool
42         RouteRandomly  bool
43
44         // The sentinel master name.
45         // Only failover clients.
46         MasterName string
47 }
48
49 func (o *UniversalOptions) cluster() *ClusterOptions {
50         if len(o.Addrs) == 0 {
51                 o.Addrs = []string{"127.0.0.1:6379"}
52         }
53
54         return &ClusterOptions{
55                 Addrs:     o.Addrs,
56                 OnConnect: o.OnConnect,
57
58                 Password: o.Password,
59
60                 MaxRedirects:   o.MaxRedirects,
61                 ReadOnly:       o.ReadOnly,
62                 RouteByLatency: o.RouteByLatency,
63                 RouteRandomly:  o.RouteRandomly,
64
65                 MaxRetries:      o.MaxRetries,
66                 MinRetryBackoff: o.MinRetryBackoff,
67                 MaxRetryBackoff: o.MaxRetryBackoff,
68
69                 DialTimeout:        o.DialTimeout,
70                 ReadTimeout:        o.ReadTimeout,
71                 WriteTimeout:       o.WriteTimeout,
72                 PoolSize:           o.PoolSize,
73                 MinIdleConns:       o.MinIdleConns,
74                 MaxConnAge:         o.MaxConnAge,
75                 PoolTimeout:        o.PoolTimeout,
76                 IdleTimeout:        o.IdleTimeout,
77                 IdleCheckFrequency: o.IdleCheckFrequency,
78
79                 TLSConfig: o.TLSConfig,
80         }
81 }
82
83 func (o *UniversalOptions) failover() *FailoverOptions {
84         if len(o.Addrs) == 0 {
85                 o.Addrs = []string{"127.0.0.1:26379"}
86         }
87
88         return &FailoverOptions{
89                 SentinelAddrs: o.Addrs,
90                 MasterName:    o.MasterName,
91                 OnConnect:     o.OnConnect,
92
93                 DB:       o.DB,
94                 Password: o.Password,
95
96                 MaxRetries:      o.MaxRetries,
97                 MinRetryBackoff: o.MinRetryBackoff,
98                 MaxRetryBackoff: o.MaxRetryBackoff,
99
100                 DialTimeout:  o.DialTimeout,
101                 ReadTimeout:  o.ReadTimeout,
102                 WriteTimeout: o.WriteTimeout,
103
104                 PoolSize:           o.PoolSize,
105                 MinIdleConns:       o.MinIdleConns,
106                 MaxConnAge:         o.MaxConnAge,
107                 PoolTimeout:        o.PoolTimeout,
108                 IdleTimeout:        o.IdleTimeout,
109                 IdleCheckFrequency: o.IdleCheckFrequency,
110
111                 TLSConfig: o.TLSConfig,
112         }
113 }
114
115 func (o *UniversalOptions) simple() *Options {
116         addr := "127.0.0.1:6379"
117         if len(o.Addrs) > 0 {
118                 addr = o.Addrs[0]
119         }
120
121         return &Options{
122                 Addr:      addr,
123                 OnConnect: o.OnConnect,
124
125                 DB:       o.DB,
126                 Password: o.Password,
127
128                 MaxRetries:      o.MaxRetries,
129                 MinRetryBackoff: o.MinRetryBackoff,
130                 MaxRetryBackoff: o.MaxRetryBackoff,
131
132                 DialTimeout:  o.DialTimeout,
133                 ReadTimeout:  o.ReadTimeout,
134                 WriteTimeout: o.WriteTimeout,
135
136                 PoolSize:           o.PoolSize,
137                 MinIdleConns:       o.MinIdleConns,
138                 MaxConnAge:         o.MaxConnAge,
139                 PoolTimeout:        o.PoolTimeout,
140                 IdleTimeout:        o.IdleTimeout,
141                 IdleCheckFrequency: o.IdleCheckFrequency,
142
143                 TLSConfig: o.TLSConfig,
144         }
145 }
146
147 // --------------------------------------------------------------------
148
149 // UniversalClient is an abstract client which - based on the provided options -
150 // can connect to either clusters, or sentinel-backed failover instances or simple
151 // single-instance servers. This can be useful for testing cluster-specific
152 // applications locally.
153 type UniversalClient interface {
154         Cmdable
155         Watch(fn func(*Tx) error, keys ...string) error
156         Process(cmd Cmder) error
157         WrapProcess(fn func(oldProcess func(cmd Cmder) error) func(cmd Cmder) error)
158         Subscribe(channels ...string) *PubSub
159         PSubscribe(channels ...string) *PubSub
160         Close() error
161 }
162
163 var _ UniversalClient = (*Client)(nil)
164 var _ UniversalClient = (*ClusterClient)(nil)
165
166 // NewUniversalClient returns a new multi client. The type of client returned depends
167 // on the following three conditions:
168 //
169 // 1. if a MasterName is passed a sentinel-backed FailoverClient will be returned
170 // 2. if the number of Addrs is two or more, a ClusterClient will be returned
171 // 3. otherwise, a single-node redis Client will be returned.
172 func NewUniversalClient(opts *UniversalOptions) UniversalClient {
173         if opts.MasterName != "" {
174                 return NewFailoverClient(opts.failover())
175         } else if len(opts.Addrs) > 1 {
176                 return NewClusterClient(opts.cluster())
177         }
178         return NewClient(opts.simple())
179 }