Skip to content

Commit

Permalink
feat: implement DialURLContext(...) (#574)
Browse files Browse the repository at this point in the history
Add DialURLContext so that we consumers have
control over cancelation and timeout

Convert DialURL to call DialURLContext() and update
docs to ensure consumers are aware of the new method,
which should be preferred to ensure requests can't hang
forever.
  • Loading branch information
cameronelliott committed Sep 30, 2021
1 parent 46992b0 commit bf63cd5
Showing 1 changed file with 10 additions and 3 deletions.
13 changes: 10 additions & 3 deletions redis/conn.go
Expand Up @@ -316,10 +316,17 @@ func DialContext(ctx context.Context, network, address string, options ...DialOp

var pathDBRegexp = regexp.MustCompile(`/(\d*)\z`)

// DialURL connects to a Redis server at the given URL using the Redis
// DialURL wraps DialURLContext using context.Background.
func DialURL(rawurl string, options ...DialOption) (Conn, error) {
ctx := context.Background()

return DialURLContext(ctx, rawurl, options...)
}

// DialURLContext connects to a Redis server at the given URL using the Redis
// URI scheme. URLs should follow the draft IANA specification for the
// scheme (https://www.iana.org/assignments/uri-schemes/prov/redis).
func DialURL(rawurl string, options ...DialOption) (Conn, error) {
func DialURLContext(ctx context.Context, rawurl string, options ...DialOption) (Conn, error) {
u, err := url.Parse(rawurl)
if err != nil {
return nil, err
Expand Down Expand Up @@ -381,7 +388,7 @@ func DialURL(rawurl string, options ...DialOption) (Conn, error) {

options = append(options, DialUseTLS(u.Scheme == "rediss"))

return Dial("tcp", address, options...)
return DialContext(ctx, "tcp", address, options...)
}

// NewConn returns a new Redigo connection for the given net connection.
Expand Down

0 comments on commit bf63cd5

Please sign in to comment.