Updates based on PR feedback

This commit is contained in:
Edward Muller 2016-11-15 10:27:20 -08:00
parent 70eddf606d
commit 4aa583b6f8
2 changed files with 21 additions and 26 deletions

View File

@ -64,7 +64,7 @@ type Options struct {
// Enables read only queries on slave nodes. // Enables read only queries on slave nodes.
ReadOnly bool ReadOnly bool
// Config to use when connecting via TLS // TLS Config to use. When set TLS will be negotiated.
TLSConfig *tls.Config TLSConfig *tls.Config
} }
@ -74,7 +74,12 @@ func (opt *Options) init() {
} }
if opt.Dialer == nil { if opt.Dialer == nil {
opt.Dialer = func() (net.Conn, error) { opt.Dialer = func() (net.Conn, error) {
return net.DialTimeout(opt.Network, opt.Addr, opt.DialTimeout) conn, err := net.DialTimeout(opt.Network, opt.Addr, opt.DialTimeout)
if opt.TLSConfig == nil || err != nil {
return conn, err
}
t := tls.Client(conn, opt.TLSConfig)
return t, t.Handshake()
} }
} }
if opt.PoolSize == 0 { if opt.PoolSize == 0 {
@ -142,25 +147,15 @@ func ParseURL(redisURL string) (*Options, error) {
o.DB = 0 o.DB = 0
case 1: case 1:
if o.DB, err = strconv.Atoi(f[0]); err != nil { if o.DB, err = strconv.Atoi(f[0]); err != nil {
return nil, errors.New("Invalid redis database number: " + err.Error()) return nil, errors.New("invalid redis database number: " + err.Error())
} }
default: default:
return nil, errors.New("invalid redis URL path: " + u.Path) return nil, errors.New("invalid redis URL path: " + u.Path)
} }
if u.Scheme == "rediss" { if u.Scheme == "rediss" {
o.Dialer = func() (net.Conn, error) {
conn, err := net.DialTimeout(o.Network, o.Addr, o.DialTimeout)
if err != nil {
return nil, err
}
if o.TLSConfig == nil {
o.TLSConfig = &tls.Config{InsecureSkipVerify: true} o.TLSConfig = &tls.Config{InsecureSkipVerify: true}
} }
t := tls.Client(conn, o.TLSConfig)
return t, t.Handshake()
}
}
return o, nil return o, nil
} }

View File

@ -12,7 +12,7 @@ func TestParseURL(t *testing.T) {
u string u string
addr string addr string
db int db int
dialer bool tls bool
err error err error
}{ }{
{ {
@ -63,7 +63,7 @@ func TestParseURL(t *testing.T) {
{ {
"redis://localhost/iamadatabase", "redis://localhost/iamadatabase",
"", "",
0, false, errors.New("Invalid redis database number: strconv.ParseInt: parsing \"iamadatabase\": invalid syntax"), 0, false, errors.New("invalid redis database number: strconv.ParseInt: parsing \"iamadatabase\": invalid syntax"),
}, },
} }
@ -71,23 +71,23 @@ func TestParseURL(t *testing.T) {
t.Run(c.u, func(t *testing.T) { t.Run(c.u, func(t *testing.T) {
o, err := ParseURL(c.u) o, err := ParseURL(c.u)
if c.err == nil && err != nil { if c.err == nil && err != nil {
t.Fatalf("Expected err to be nil, but got: '%q'", err) t.Fatalf("unexpected error: '%q'", err)
return return
} }
if c.err != nil && err != nil { if c.err != nil && err != nil {
if c.err.Error() != err.Error() { if c.err.Error() != err.Error() {
t.Fatalf("Expected err to be '%q', but got '%q'", c.err, err) t.Fatalf("got %q, expected %q", err, c.err)
} }
return return
} }
if o.Addr != c.addr { if o.Addr != c.addr {
t.Errorf("Expected Addr to be '%s', but got '%s'", c.addr, o.Addr) t.Errorf("got %q, want %q", o.Addr, c.addr)
} }
if o.DB != c.db { if o.DB != c.db {
t.Errorf("Expecdted DB to be '%d', but got '%d'", c.db, o.DB) t.Errorf("got %q, expected %q", o.DB, c.db)
} }
if c.dialer && o.Dialer == nil { if c.tls && o.TLSConfig == nil {
t.Errorf("Expected a Dialer to be set, but isn't") t.Errorf("got nil TLSConfig, expected a TLSConfig")
} }
}) })
} }