File tree Expand file tree Collapse file tree 1 file changed +27
-4
lines changed Expand file tree Collapse file tree 1 file changed +27
-4
lines changed Original file line number Diff line number Diff line change @@ -113,11 +113,34 @@ func (w *udsWriter) tryToDial(network string) (net.Conn, error) {
113
113
if err != nil {
114
114
return nil , err
115
115
}
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
119
143
}
120
- return newConn , nil
121
144
}
122
145
123
146
func (w * udsWriter ) ensureConnection () (net.Conn , error ) {
You can’t perform that action at this time.
0 commit comments