From 589a7c159b6e4a25cae91ba514afc12704c90d00 Mon Sep 17 00:00:00 2001 From: matst80 Date: Wed, 13 Nov 2024 10:19:56 +0100 Subject: [PATCH] better retry --- synced-pool.go | 4 ++++ tcp-client.go | 19 ++++++++++++++----- 2 files changed, 18 insertions(+), 5 deletions(-) diff --git a/synced-pool.go b/synced-pool.go index 163dada..46b4c78 100644 --- a/synced-pool.go +++ b/synced-pool.go @@ -399,6 +399,10 @@ func (p *SyncedPool) AddRemote(host string) error { }() go func() { for range time.Tick(time.Second * 3) { + if !remote.IsHealthy() { + return + } + log.Printf("Pinging remote %s\n", host) err := remote.Ping() for err != nil { diff --git a/tcp-client.go b/tcp-client.go index 47202b4..3ef0aad 100644 --- a/tcp-client.go +++ b/tcp-client.go @@ -54,16 +54,25 @@ func NewPersistentConnection(address string) (*PersistentConnection, error) { } func (m *PersistentConnection) Connect() error { - if !m.Dead { + fails := 0 + for { connection, err := net.Dial("tcp", m.address) if err != nil { log.Printf("Error connecting to %s: %v\n", m.address, err) - m.Died <- true - m.Dead = true - return err + fails++ + if fails > 15 { + m.Died <- true + m.Dead = true + return err + } + } else { + m.Conn = connection + break } - m.Conn = connection + time.Sleep(time.Millisecond * 300) + } + return nil }