src: Add DMA localagent
[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         // The sentinel master name.
16         // Only failover clients.
17         MasterName string
18
19         // Database to be selected after connecting to the server.
20         // Only single-node and failover clients.
21         DB int
22
23         // Only cluster clients.
24
25         // Enables read only queries on slave nodes.
26         ReadOnly bool
27
28         MaxRedirects   int
29         RouteByLatency bool
30
31         // Common options
32
33         OnConnect          func(*Conn) error
34         MaxRetries         int
35         Password           string
36         DialTimeout        time.Duration
37         ReadTimeout        time.Duration
38         WriteTimeout       time.Duration
39         PoolSize           int
40         PoolTimeout        time.Duration
41         IdleTimeout        time.Duration
42         IdleCheckFrequency time.Duration
43         TLSConfig          *tls.Config
44 }
45
46 func (o *UniversalOptions) cluster() *ClusterOptions {
47         if len(o.Addrs) == 0 {
48                 o.Addrs = []string{"127.0.0.1:6379"}
49         }
50
51         return &ClusterOptions{
52                 Addrs:          o.Addrs,
53                 MaxRedirects:   o.MaxRedirects,
54                 RouteByLatency: o.RouteByLatency,
55                 ReadOnly:       o.ReadOnly,
56
57                 OnConnect:          o.OnConnect,
58                 MaxRetries:         o.MaxRetries,
59                 Password:           o.Password,
60                 DialTimeout:        o.DialTimeout,
61                 ReadTimeout:        o.ReadTimeout,
62                 WriteTimeout:       o.WriteTimeout,
63                 PoolSize:           o.PoolSize,
64                 PoolTimeout:        o.PoolTimeout,
65                 IdleTimeout:        o.IdleTimeout,
66                 IdleCheckFrequency: o.IdleCheckFrequency,
67                 TLSConfig:          o.TLSConfig,
68         }
69 }
70
71 func (o *UniversalOptions) failover() *FailoverOptions {
72         if len(o.Addrs) == 0 {
73                 o.Addrs = []string{"127.0.0.1:26379"}
74         }
75
76         return &FailoverOptions{
77                 SentinelAddrs: o.Addrs,
78                 MasterName:    o.MasterName,
79                 DB:            o.DB,
80
81                 OnConnect:          o.OnConnect,
82                 MaxRetries:         o.MaxRetries,
83                 Password:           o.Password,
84                 DialTimeout:        o.DialTimeout,
85                 ReadTimeout:        o.ReadTimeout,
86                 WriteTimeout:       o.WriteTimeout,
87                 PoolSize:           o.PoolSize,
88                 PoolTimeout:        o.PoolTimeout,
89                 IdleTimeout:        o.IdleTimeout,
90                 IdleCheckFrequency: o.IdleCheckFrequency,
91                 TLSConfig:          o.TLSConfig,
92         }
93 }
94
95 func (o *UniversalOptions) simple() *Options {
96         addr := "127.0.0.1:6379"
97         if len(o.Addrs) > 0 {
98                 addr = o.Addrs[0]
99         }
100
101         return &Options{
102                 Addr: addr,
103                 DB:   o.DB,
104
105                 OnConnect:          o.OnConnect,
106                 MaxRetries:         o.MaxRetries,
107                 Password:           o.Password,
108                 DialTimeout:        o.DialTimeout,
109                 ReadTimeout:        o.ReadTimeout,
110                 WriteTimeout:       o.WriteTimeout,
111                 PoolSize:           o.PoolSize,
112                 PoolTimeout:        o.PoolTimeout,
113                 IdleTimeout:        o.IdleTimeout,
114                 IdleCheckFrequency: o.IdleCheckFrequency,
115                 TLSConfig:          o.TLSConfig,
116         }
117 }
118
119 // --------------------------------------------------------------------
120
121 // UniversalClient is an abstract client which - based on the provided options -
122 // can connect to either clusters, or sentinel-backed failover instances or simple
123 // single-instance servers. This can be useful for testing cluster-specific
124 // applications locally.
125 type UniversalClient interface {
126         Cmdable
127         Watch(fn func(*Tx) error, keys ...string) error
128         Process(cmd Cmder) error
129         WrapProcess(fn func(oldProcess func(cmd Cmder) error) func(cmd Cmder) error)
130         Subscribe(channels ...string) *PubSub
131         PSubscribe(channels ...string) *PubSub
132         Close() error
133 }
134
135 var _ UniversalClient = (*Client)(nil)
136 var _ UniversalClient = (*ClusterClient)(nil)
137
138 // NewUniversalClient returns a new multi client. The type of client returned depends
139 // on the following three conditions:
140 //
141 // 1. if a MasterName is passed a sentinel-backed FailoverClient will be returned
142 // 2. if the number of Addrs is two or more, a ClusterClient will be returned
143 // 3. otherwise, a single-node redis Client will be returned.
144 func NewUniversalClient(opts *UniversalOptions) UniversalClient {
145         if opts.MasterName != "" {
146                 return NewFailoverClient(opts.failover())
147         } else if len(opts.Addrs) > 1 {
148                 return NewClusterClient(opts.cluster())
149         }
150         return NewClient(opts.simple())
151 }