| 1 |
package redis |
| 2 |
|
| 3 |
import ( |
| 4 |
"crypto/tls" |
| 5 |
"errors" |
| 6 |
"fmt" |
| 7 |
"net" |
| 8 |
"net/url" |
| 9 |
"strconv" |
| 10 |
"strings" |
| 11 |
"time" |
| 12 |
|
| 13 |
"gopkg.in/redis.v5/internal/pool" |
| 14 |
) |
| 15 |
|
| 16 |
type Options struct { |
| 17 |
// The network type, either tcp or unix. |
| 18 |
// Default is tcp. |
| 19 |
Network string |
| 20 |
// host:port address. |
| 21 |
Addr string |
| 22 |
|
| 23 |
// Dialer creates new network connection and has priority over |
| 24 |
// Network and Addr options. |
| 25 |
Dialer func() (net.Conn, error) |
| 26 |
|
| 27 |
// Optional password. Must match the password specified in the |
| 28 |
// requirepass server configuration option. |
| 29 |
Password string |
| 30 |
// Database to be selected after connecting to the server. |
| 31 |
DB int |
| 32 |
|
| 33 |
// Maximum number of retries before giving up. |
| 34 |
// Default is to not retry failed commands. |
| 35 |
MaxRetries int |
| 36 |
|
| 37 |
// Dial timeout for establishing new connections. |
| 38 |
// Default is 5 seconds. |
| 39 |
DialTimeout time.Duration |
| 40 |
// Timeout for socket reads. If reached, commands will fail |
| 41 |
// with a timeout instead of blocking. |
| 42 |
// Default is 3 seconds. |
| 43 |
ReadTimeout time.Duration |
| 44 |
// Timeout for socket writes. If reached, commands will fail |
| 45 |
// with a timeout instead of blocking. |
| 46 |
// Default is 3 seconds. |
| 47 |
WriteTimeout time.Duration |
| 48 |
|
| 49 |
// Maximum number of socket connections. |
| 50 |
// Default is 10 connections. |
| 51 |
PoolSize int |
| 52 |
// Amount of time client waits for connection if all connections |
| 53 |
// are busy before returning an error. |
| 54 |
// Default is ReadTimeout + 1 second. |
| 55 |
PoolTimeout time.Duration |
| 56 |
// Amount of time after which client closes idle connections. |
| 57 |
// Should be less than server's timeout. |
| 58 |
// Default is to not close idle connections. |
| 59 |
IdleTimeout time.Duration |
| 60 |
// Frequency of idle checks. |
| 61 |
// Default is 1 minute. |
| 62 |
// When minus value is set, then idle check is disabled. |
| 63 |
IdleCheckFrequency time.Duration |
| 64 |
|
| 65 |
// Enables read only queries on slave nodes. |
| 66 |
ReadOnly bool |
| 67 |
|
| 68 |
// TLS Config to use. When set TLS will be negotiated. |
| 69 |
TLSConfig *tls.Config |
| 70 |
} |
| 71 |
|
| 72 |
func (opt *Options) init() { |
| 73 |
if opt.Network == "" { |
| 74 |
opt.Network = "tcp" |
| 75 |
} |
| 76 |
if opt.Dialer == nil { |
| 77 |
opt.Dialer = func() (net.Conn, error) { |
| 78 |
conn, err := net.DialTimeout(opt.Network, opt.Addr, opt.DialTimeout) |
| 79 |
if opt.TLSConfig == nil || err != nil { |
| 80 |
return conn, err |
| 81 |
} |
| 82 |
t := tls.Client(conn, opt.TLSConfig) |
| 83 |
return t, t.Handshake() |
| 84 |
} |
| 85 |
} |
| 86 |
if opt.PoolSize == 0 { |
| 87 |
opt.PoolSize = 10 |
| 88 |
} |
| 89 |
if opt.DialTimeout == 0 { |
| 90 |
opt.DialTimeout = 5 * time.Second |
| 91 |
} |
| 92 |
if opt.ReadTimeout == 0 { |
| 93 |
opt.ReadTimeout = 3 * time.Second |
| 94 |
} else if opt.ReadTimeout == -1 { |
| 95 |
opt.ReadTimeout = 0 |
| 96 |
} |
| 97 |
if opt.WriteTimeout == 0 { |
| 98 |
opt.WriteTimeout = opt.ReadTimeout |
| 99 |
} else if opt.WriteTimeout == -1 { |
| 100 |
opt.WriteTimeout = 0 |
| 101 |
} |
| 102 |
if opt.PoolTimeout == 0 { |
| 103 |
opt.PoolTimeout = opt.ReadTimeout + time.Second |
| 104 |
} |
| 105 |
if opt.IdleTimeout == 0 { |
| 106 |
opt.IdleTimeout = 5 * time.Minute |
| 107 |
} |
| 108 |
if opt.IdleCheckFrequency == 0 { |
| 109 |
opt.IdleCheckFrequency = time.Minute |
| 110 |
} |
| 111 |
} |
| 112 |
|
| 113 |
// ParseURL parses a redis URL into options that can be used to connect to redis |
| 114 |
func ParseURL(redisURL string) (*Options, error) { |
| 115 |
o := &Options{Network: "tcp"} |
| 116 |
u, err := url.Parse(redisURL) |
| 117 |
if err != nil { |
| 118 |
return nil, err |
| 119 |
} |
| 120 |
|
| 121 |
if u.Scheme != "redis" && u.Scheme != "rediss" { |
| 122 |
return nil, errors.New("invalid redis URL scheme: " + u.Scheme) |
| 123 |
} |
| 124 |
|
| 125 |
if u.User != nil { |
| 126 |
if p, ok := u.User.Password(); ok { |
| 127 |
o.Password = p |
| 128 |
} |
| 129 |
} |
| 130 |
|
| 131 |
if len(u.Query()) > 0 { |
| 132 |
return nil, errors.New("no options supported") |
| 133 |
} |
| 134 |
|
| 135 |
h, p, err := net.SplitHostPort(u.Host) |
| 136 |
if err != nil { |
| 137 |
h = u.Host |
| 138 |
} |
| 139 |
if h == "" { |
| 140 |
h = "localhost" |
| 141 |
} |
| 142 |
if p == "" { |
| 143 |
p = "6379" |
| 144 |
} |
| 145 |
o.Addr = net.JoinHostPort(h, p) |
| 146 |
|
| 147 |
f := strings.FieldsFunc(u.Path, func(r rune) bool { |
| 148 |
return r == '/' |
| 149 |
}) |
| 150 |
switch len(f) { |
| 151 |
case 0: |
| 152 |
o.DB = 0 |
| 153 |
case 1: |
| 154 |
if o.DB, err = strconv.Atoi(f[0]); err != nil { |
| 155 |
return nil, fmt.Errorf("invalid redis database number: %q", f[0]) |
| 156 |
} |
| 157 |
default: |
| 158 |
return nil, errors.New("invalid redis URL path: " + u.Path) |
| 159 |
} |
| 160 |
|
| 161 |
if u.Scheme == "rediss" { |
| 162 |
o.TLSConfig = &tls.Config{ServerName: h} |
| 163 |
} |
| 164 |
return o, nil |
| 165 |
} |
| 166 |
|
| 167 |
func newConnPool(opt *Options) *pool.ConnPool { |
| 168 |
return pool.NewConnPool( |
| 169 |
opt.Dialer, |
| 170 |
opt.PoolSize, |
| 171 |
opt.PoolTimeout, |
| 172 |
opt.IdleTimeout, |
| 173 |
opt.IdleCheckFrequency, |
| 174 |
) |
| 175 |
} |
| 176 |
|
| 177 |
// PoolStats contains pool state information and accumulated stats. |
| 178 |
type PoolStats struct { |
| 179 |
Requests uint32 // number of times a connection was requested by the pool |
| 180 |
Hits uint32 // number of times free connection was found in the pool |
| 181 |
Timeouts uint32 // number of times a wait timeout occurred |
| 182 |
|
| 183 |
TotalConns uint32 // the number of total connections in the pool |
| 184 |
FreeConns uint32 // the number of free connections in the pool |
| 185 |
} |