Skip to content

Commit

Permalink
Do 3 attempts when checking for master, with higher and higher connec…
Browse files Browse the repository at this point in the history
…tion timeout
  • Loading branch information
Chupaka committed Jan 14, 2023
1 parent b4619f0 commit 3af7919
Showing 1 changed file with 6 additions and 3 deletions.
9 changes: 6 additions & 3 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,10 @@ func ServePort(port string) {

func followMaster(rp *RedisPort) {
for {
newAddr := getMasterAddr(rp.port)
var newAddr *net.TCPAddr
for attempt := 1; newAddr == nil && attempt <= 3; attempt++ {
newAddr = getMasterAddr(rp.port, attempt)
}

if newAddr == nil {
log.Printf("No masters found for port %s! Will not serve new connections until master is found...", rp.port)
Expand Down Expand Up @@ -189,9 +192,9 @@ func pipe(r io.Reader, w io.WriteCloser) {
atomic.AddUint32(&globalStats.pipesActive, ^uint32(0))
}

func getMasterAddr(port string) *net.TCPAddr {
func getMasterAddr(port string, timeout int) *net.TCPAddr {
for _, node := range config.Nodes {
d := net.Dialer{Timeout: 1 * time.Second}
d := net.Dialer{Timeout: time.Duration(timeout) * time.Second}
conn, err := d.Dial("tcp", node+":"+port)
if err != nil {
log.Printf("Can't connect to %s: %s\n", node, err)
Expand Down

0 comments on commit 3af7919

Please sign in to comment.