Skip to content

Commit 1ed7803

Browse files
Merge pull request DataDog#323 from DataDog/tobz/graceful-reconnect-agent-restart
Attempt to gracefully reconnect when UDS connection is severed. Co-authored-by: tobz <[email protected]>
2 parents bae3560 + 75670b9 commit 1ed7803

File tree

1 file changed

+27
-4
lines changed

1 file changed

+27
-4
lines changed

statsd/uds.go

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -113,11 +113,34 @@ func (w *udsWriter) tryToDial(network string) (net.Conn, error) {
113113
if err != nil {
114114
return nil, err
115115
}
116-
newConn, err := net.DialTimeout(udsAddr.Network(), udsAddr.String(), w.connectTimeout)
117-
if err != nil {
118-
return nil, err
116+
117+
// Try to gracefully reconnect to the socket when we encounter "connection refused", as it's likely that the Agent
118+
// is restarting and the socket is not yet available.
119+
connectAttemptsLeft := 3
120+
connectDeadline := time.Now().Add(w.connectTimeout)
121+
122+
// Calculate the backoff time for connection refused errors, but don't exceed one second: this means we won't waste
123+
// longer than 1 seconds worth of time if the socket becomes available immediately after our last connect attempt
124+
connRefusedBackoff := w.connectTimeout / time.Duration(connectAttemptsLeft+1)
125+
if connRefusedBackoff > time.Second {
126+
connRefusedBackoff = time.Second
127+
}
128+
129+
for {
130+
connectAttemptsLeft--
131+
132+
perCallTimeout := time.Until(connectDeadline)
133+
newConn, err := net.DialTimeout(udsAddr.Network(), udsAddr.String(), perCallTimeout)
134+
if err != nil {
135+
if strings.HasSuffix(err.Error(), "connection refused") && connectAttemptsLeft > 0 {
136+
// If we get a connection refused error, we need to wait a bit before trying again.
137+
time.Sleep(connRefusedBackoff)
138+
continue
139+
}
140+
return nil, err
141+
}
142+
return newConn, nil
119143
}
120-
return newConn, nil
121144
}
122145

123146
func (w *udsWriter) ensureConnection() (net.Conn, error) {

0 commit comments

Comments
 (0)