Skip to content
This repository has been archived by the owner on Dec 28, 2024. It is now read-only.

Commit

Permalink
feat: support passwords for Redis sentinels
Browse files Browse the repository at this point in the history
Closes #105
  • Loading branch information
palkan committed Jul 6, 2020
1 parent 75eb5e4 commit 91cab75
Showing 1 changed file with 19 additions and 3 deletions.
22 changes: 19 additions & 3 deletions pubsub/redis.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package pubsub
import (
"context"
"errors"
"fmt"
"math/rand"
"net/url"
"strings"
Expand Down Expand Up @@ -89,12 +90,27 @@ func (s *RedisSubscriber) Start() error {
Dial: func(addr string) (redis.Conn, error) {
timeout := 500 * time.Millisecond

c, err := redis.Dial(
"tcp",
addr,
sentinelHost := addr
dialOptions := []redis.DialOption{
redis.DialConnectTimeout(timeout),
redis.DialReadTimeout(timeout),
redis.DialReadTimeout(timeout),
}

sentinelURI, err := url.Parse(fmt.Sprintf("redis://%s", addr))

if err == nil {
sentinelHost = sentinelURI.Host
password, hasPassword := sentinelURI.User.Password()
if hasPassword {
dialOptions = append(dialOptions, redis.DialPassword(password))
}
}

c, err := redis.Dial(
"tcp",
sentinelHost,
dialOptions...,
)
if err != nil {
s.log.Debugf("Failed to connect to sentinel %s", addr)
Expand Down

0 comments on commit 91cab75

Please sign in to comment.