Skip to content

Commit e611262

Browse files
committed
Add query parameter parsing to ParseURL()
Before this change, ParseURL would only accept a very restricted set of URLs (it returned an error, if it encountered any parameter). This commit introduces the ability to process URLs like redis://localhost/1?dial_timeout=10s and similar. Go programs which were providing a configuration tunable (e.g. CLI flag, config entry or environment variable) to configure the Redis connection now don't need to perform this task themselves.
1 parent 3ac3452 commit e611262

File tree

3 files changed

+217
-19
lines changed

3 files changed

+217
-19
lines changed

example_test.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,20 +39,22 @@ func ExampleNewClient() {
3939
}
4040

4141
func ExampleParseURL() {
42-
opt, err := redis.ParseURL("redis://:qwerty@localhost:6379/1")
42+
opt, err := redis.ParseURL("redis://:qwerty@localhost:6379/1?dial_timeout=5s")
4343
if err != nil {
4444
panic(err)
4545
}
4646
fmt.Println("addr is", opt.Addr)
4747
fmt.Println("db is", opt.DB)
4848
fmt.Println("password is", opt.Password)
49+
fmt.Println("dial timeout is", opt.DialTimeout)
4950

5051
// Create client as usually.
5152
_ = redis.NewClient(opt)
5253

5354
// Output: addr is localhost:6379
5455
// db is 1
5556
// password is qwerty
57+
// dial timeout is 5s
5658
}
5759

5860
func ExampleNewFailoverClient() {

options.go

Lines changed: 136 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"net"
99
"net/url"
1010
"runtime"
11+
"sort"
1112
"strconv"
1213
"strings"
1314
"time"
@@ -192,9 +193,31 @@ func (opt *Options) clone() *Options {
192193
// Scheme is required.
193194
// There are two connection types: by tcp socket and by unix socket.
194195
// Tcp connection:
195-
// redis://<user>:<password>@<host>:<port>/<db_number>
196+
// redis://<user>:<password>@<host>:<port>/<db_number>
196197
// Unix connection:
197198
// unix://<user>:<password>@</path/to/redis.sock>?db=<db_number>
199+
// Most Option fields can be set using query parameters, with the following restrictions:
200+
// - field names are mapped using snake-case conversion: to set MaxRetries, use max_retries
201+
// - query parameters "network", "addr", "username" and "password" are ignored
202+
// (these are covered by other URL attributes)
203+
// - only scalar type fields are supported (bool, int, duration)
204+
// - for duration fields, values must be a valid input for time.ParseDuration();
205+
// additionally a plain integer as value (i.e. without unit) is intepreted as seconds
206+
// - to disable a duration field, use value 0; to use the default value, leave the value blank
207+
// or remove the parameter
208+
// - only the last value is interpreted if a parameter is given multiple times
209+
// - unknown parameter names will result in an error
210+
// Examples:
211+
// redis://user:password@localhost:6789/3?dial_timeout=3&db=1&read_timeout=6s&max_retries=2
212+
// is equivalent to:
213+
// &Options{
214+
// Network: "tcp",
215+
// Addr: "localhost:6789",
216+
// DB: 1, // path "/3" was overridden by "&db=1"
217+
// DialTimeout: 3 * time.Second, // no time unit = seconds
218+
// ReadTimeout: 6 * time.Second,
219+
// MaxRetries: 2,
220+
// }
198221
func ParseURL(redisURL string) (*Options, error) {
199222
u, err := url.Parse(redisURL)
200223
if err != nil {
@@ -216,10 +239,6 @@ func setupTCPConn(u *url.URL) (*Options, error) {
216239

217240
o.Username, o.Password = getUserPassword(u)
218241

219-
if len(u.Query()) > 0 {
220-
return nil, errors.New("redis: no options supported")
221-
}
222-
223242
h, p, err := net.SplitHostPort(u.Host)
224243
if err != nil {
225244
h = u.Host
@@ -250,7 +269,7 @@ func setupTCPConn(u *url.URL) (*Options, error) {
250269
o.TLSConfig = &tls.Config{ServerName: h}
251270
}
252271

253-
return o, nil
272+
return setupConnParams(u, o)
254273
}
255274

256275
func setupUnixConn(u *url.URL) (*Options, error) {
@@ -262,19 +281,122 @@ func setupUnixConn(u *url.URL) (*Options, error) {
262281
return nil, errors.New("redis: empty unix socket path")
263282
}
264283
o.Addr = u.Path
265-
266284
o.Username, o.Password = getUserPassword(u)
285+
return setupConnParams(u, o)
286+
}
267287

268-
dbStr := u.Query().Get("db")
269-
if dbStr == "" {
270-
return o, nil // if database is not set, connect to 0 db.
288+
type queryOptions struct {
289+
q url.Values
290+
err error
291+
}
292+
293+
func (o *queryOptions) string(name string) string {
294+
vs := o.q[name]
295+
if len(vs) == 0 {
296+
return ""
271297
}
298+
delete(o.q, name) // enable detection of unknown parameters
299+
return vs[len(vs)-1]
300+
}
272301

273-
db, err := strconv.Atoi(dbStr)
274-
if err != nil {
275-
return nil, fmt.Errorf("redis: invalid database number: %w", err)
302+
func (o *queryOptions) int(name string) int {
303+
s := o.string(name)
304+
if s == "" {
305+
return 0
306+
}
307+
i, err := strconv.Atoi(s)
308+
if err == nil {
309+
return i
310+
}
311+
if o.err == nil {
312+
o.err = fmt.Errorf("redis: invalid %s number: %s", name, err)
313+
}
314+
return 0
315+
}
316+
317+
func (o *queryOptions) duration(name string) time.Duration {
318+
s := o.string(name)
319+
if s == "" {
320+
return 0
321+
}
322+
// try plain number first
323+
if i, err := strconv.Atoi(s); err == nil {
324+
if i <= 0 {
325+
// disable timeouts
326+
return -1
327+
}
328+
return time.Duration(i) * time.Second
329+
}
330+
dur, err := time.ParseDuration(s)
331+
if err == nil {
332+
return dur
333+
}
334+
if o.err == nil {
335+
o.err = fmt.Errorf("redis: invalid %s duration: %w", name, err)
336+
}
337+
return 0
338+
}
339+
340+
func (o *queryOptions) bool(name string) bool {
341+
switch s := o.string(name); s {
342+
case "true", "1":
343+
return true
344+
case "false", "0", "":
345+
return false
346+
default:
347+
if o.err == nil {
348+
o.err = fmt.Errorf("redis: invalid %s boolean: expected true/false/1/0 or an empty string, got %q", name, s)
349+
}
350+
return false
351+
}
352+
}
353+
354+
func (o *queryOptions) remaining() []string {
355+
if len(o.q) == 0 {
356+
return nil
357+
}
358+
keys := make([]string, 0, len(o.q))
359+
for k := range o.q {
360+
keys = append(keys, k)
361+
}
362+
sort.Strings(keys)
363+
return keys
364+
}
365+
366+
// setupConnParams converts query parameters in u to option value in o.
367+
func setupConnParams(u *url.URL, o *Options) (*Options, error) {
368+
q := queryOptions{q: u.Query()}
369+
370+
// compat: a future major release may use q.int("db")
371+
if tmp := q.string("db"); tmp != "" {
372+
db, err := strconv.Atoi(tmp)
373+
if err != nil {
374+
return nil, fmt.Errorf("redis: invalid database number: %w", err)
375+
}
376+
o.DB = db
377+
}
378+
379+
o.MaxRetries = q.int("max_retries")
380+
o.MinRetryBackoff = q.duration("min_retry_backoff")
381+
o.MaxRetryBackoff = q.duration("max_retry_backoff")
382+
o.DialTimeout = q.duration("dial_timeout")
383+
o.ReadTimeout = q.duration("read_timeout")
384+
o.WriteTimeout = q.duration("write_timeout")
385+
o.PoolFIFO = q.bool("pool_fifo")
386+
o.PoolSize = q.int("pool_size")
387+
o.MinIdleConns = q.int("min_idle_conns")
388+
o.MaxConnAge = q.duration("max_conn_age")
389+
o.PoolTimeout = q.duration("pool_timeout")
390+
o.IdleTimeout = q.duration("idle_timeout")
391+
o.IdleCheckFrequency = q.duration("idle_check_frequency")
392+
if q.err != nil {
393+
return nil, q.err
394+
}
395+
396+
// any parameters left?
397+
if r := q.remaining(); len(r) > 0 {
398+
return nil, fmt.Errorf("redis: unexpected option: %s", strings.Join(r, ", "))
276399
}
277-
o.DB = db
278400

279401
return o, nil
280402
}

options_test.go

Lines changed: 78 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,25 @@ func TestParseURL(t *testing.T) {
4040
}, {
4141
url: "redis://foo:bar@localhost:123",
4242
o: &Options{Addr: "localhost:123", Username: "foo", Password: "bar"},
43+
}, {
44+
// multiple params
45+
url: "redis://localhost:123/?db=2&read_timeout=2&pool_fifo=true",
46+
o: &Options{Addr: "localhost:123", DB: 2, ReadTimeout: 2 * time.Second, PoolFIFO: true},
47+
}, {
48+
// special case handling for disabled timeouts
49+
url: "redis://localhost:123/?db=2&idle_timeout=0",
50+
o: &Options{Addr: "localhost:123", DB: 2, IdleTimeout: -1},
51+
}, {
52+
// negative values disable timeouts as well
53+
url: "redis://localhost:123/?db=2&idle_timeout=-1",
54+
o: &Options{Addr: "localhost:123", DB: 2, IdleTimeout: -1},
55+
}, {
56+
// absent timeout values will use defaults
57+
url: "redis://localhost:123/?db=2&idle_timeout=",
58+
o: &Options{Addr: "localhost:123", DB: 2, IdleTimeout: 0},
59+
}, {
60+
url: "redis://localhost:123/?db=2&idle_timeout", // missing "=" at the end
61+
o: &Options{Addr: "localhost:123", DB: 2, IdleTimeout: 0},
4362
}, {
4463
url: "unix:///tmp/redis.sock",
4564
o: &Options{Addr: "/tmp/redis.sock"},
@@ -50,11 +69,27 @@ func TestParseURL(t *testing.T) {
5069
url: "unix://foo:bar@/tmp/redis.sock?db=3",
5170
o: &Options{Addr: "/tmp/redis.sock", Username: "foo", Password: "bar", DB: 3},
5271
}, {
72+
// invalid db format
5373
url: "unix://foo:bar@/tmp/redis.sock?db=test",
5474
err: errors.New(`redis: invalid database number: strconv.Atoi: parsing "test": invalid syntax`),
75+
}, {
76+
// invalid int value
77+
url: "redis://localhost/?pool_size=five",
78+
err: errors.New(`redis: invalid pool_size number: strconv.Atoi: parsing "five": invalid syntax`),
79+
}, {
80+
// invalid bool value
81+
url: "redis://localhost/?pool_fifo=yes",
82+
err: errors.New(`redis: invalid pool_fifo boolean: expected true/false/1/0 or an empty string, got "yes"`),
83+
}, {
84+
// it returns first error
85+
url: "redis://localhost/?db=foo&pool_size=five",
86+
err: errors.New(`redis: invalid database number: strconv.Atoi: parsing "foo": invalid syntax`),
5587
}, {
5688
url: "redis://localhost/?abc=123",
57-
err: errors.New("redis: no options supported"),
89+
err: errors.New("redis: unexpected option: abc"),
90+
}, {
91+
url: "redis://localhost/?wrte_timout=10s&abc=123",
92+
err: errors.New("redis: unexpected option: abc, wrte_timout"),
5893
}, {
5994
url: "http://google.com",
6095
err: errors.New("redis: invalid URL scheme: http"),
@@ -98,7 +133,7 @@ func comprareOptions(t *testing.T, actual, expected *Options) {
98133
t.Errorf("got %q, want %q", actual.Addr, expected.Addr)
99134
}
100135
if actual.DB != expected.DB {
101-
t.Errorf("got %q, expected %q", actual.DB, expected.DB)
136+
t.Errorf("DB: got %q, expected %q", actual.DB, expected.DB)
102137
}
103138
if actual.TLSConfig == nil && expected.TLSConfig != nil {
104139
t.Errorf("got nil TLSConfig, expected a TLSConfig")
@@ -107,10 +142,49 @@ func comprareOptions(t *testing.T, actual, expected *Options) {
107142
t.Errorf("got TLSConfig, expected no TLSConfig")
108143
}
109144
if actual.Username != expected.Username {
110-
t.Errorf("got %q, expected %q", actual.Username, expected.Username)
145+
t.Errorf("Username: got %q, expected %q", actual.Username, expected.Username)
111146
}
112147
if actual.Password != expected.Password {
113-
t.Errorf("got %q, expected %q", actual.Password, expected.Password)
148+
t.Errorf("Password: got %q, expected %q", actual.Password, expected.Password)
149+
}
150+
if actual.MaxRetries != expected.MaxRetries {
151+
t.Errorf("MaxRetries: got %v, expected %v", actual.MaxRetries, expected.MaxRetries)
152+
}
153+
if actual.MinRetryBackoff != expected.MinRetryBackoff {
154+
t.Errorf("MinRetryBackoff: got %v, expected %v", actual.MinRetryBackoff, expected.MinRetryBackoff)
155+
}
156+
if actual.MaxRetryBackoff != expected.MaxRetryBackoff {
157+
t.Errorf("MaxRetryBackoff: got %v, expected %v", actual.MaxRetryBackoff, expected.MaxRetryBackoff)
158+
}
159+
if actual.DialTimeout != expected.DialTimeout {
160+
t.Errorf("DialTimeout: got %v, expected %v", actual.DialTimeout, expected.DialTimeout)
161+
}
162+
if actual.ReadTimeout != expected.ReadTimeout {
163+
t.Errorf("ReadTimeout: got %v, expected %v", actual.ReadTimeout, expected.ReadTimeout)
164+
}
165+
if actual.WriteTimeout != expected.WriteTimeout {
166+
t.Errorf("WriteTimeout: got %v, expected %v", actual.WriteTimeout, expected.WriteTimeout)
167+
}
168+
if actual.PoolFIFO != expected.PoolFIFO {
169+
t.Errorf("PoolFIFO: got %v, expected %v", actual.PoolFIFO, expected.PoolFIFO)
170+
}
171+
if actual.PoolSize != expected.PoolSize {
172+
t.Errorf("PoolSize: got %v, expected %v", actual.PoolSize, expected.PoolSize)
173+
}
174+
if actual.MinIdleConns != expected.MinIdleConns {
175+
t.Errorf("MinIdleConns: got %v, expected %v", actual.MinIdleConns, expected.MinIdleConns)
176+
}
177+
if actual.MaxConnAge != expected.MaxConnAge {
178+
t.Errorf("MaxConnAge: got %v, expected %v", actual.MaxConnAge, expected.MaxConnAge)
179+
}
180+
if actual.PoolTimeout != expected.PoolTimeout {
181+
t.Errorf("PoolTimeout: got %v, expected %v", actual.PoolTimeout, expected.PoolTimeout)
182+
}
183+
if actual.IdleTimeout != expected.IdleTimeout {
184+
t.Errorf("IdleTimeout: got %v, expected %v", actual.IdleTimeout, expected.IdleTimeout)
185+
}
186+
if actual.IdleCheckFrequency != expected.IdleCheckFrequency {
187+
t.Errorf("IdleCheckFrequency: got %v, expected %v", actual.IdleCheckFrequency, expected.IdleCheckFrequency)
114188
}
115189
}
116190

0 commit comments

Comments
 (0)