Skip to content

Commit 1fd416a

Browse files
committed
Introduce a small sleep after handling a (temporarily) failed Accept
1 parent 4e97b9a commit 1fd416a

File tree

3 files changed

+17
-0
lines changed

3 files changed

+17
-0
lines changed

cmd/cloud_sql_proxy/proxy.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import (
2626
"path/filepath"
2727
"runtime"
2828
"strings"
29+
"time"
2930

3031
"github.com/GoogleCloudPlatform/cloudsql-proxy/logging"
3132
"github.com/GoogleCloudPlatform/cloudsql-proxy/proxy/fuse"
@@ -139,10 +140,15 @@ func listenInstance(dst chan<- proxy.Conn, cfg instanceConfig) (net.Listener, er
139140

140141
go func() {
141142
for {
143+
start := time.Now()
142144
c, err := l.Accept()
143145
if err != nil {
144146
logging.Errorf("Error in accept for %q on %v: %v", cfg, cfg.Address, err)
145147
if nerr, ok := err.(net.Error); ok && nerr.Temporary() {
148+
d := 10*time.Millisecond - time.Since(start)
149+
if d > 0 {
150+
time.Sleep(d)
151+
}
146152
continue
147153
}
148154
l.Close()

proxy/fuse/fuse.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ import (
4242
"os"
4343
"path/filepath"
4444
"sync"
45+
"time"
4546

4647
"bazil.org/fuse"
4748
"bazil.org/fuse/fs"
@@ -281,10 +282,15 @@ func (r *fsRoot) removeListener(instance, path string) {
281282
// r.newConn is called. After the Listener returns an error it is removed.
282283
func (r *fsRoot) listenerLifecycle(l net.Listener, instance, path string) {
283284
for {
285+
start := time.Now()
284286
c, err := l.Accept()
285287
if err != nil {
286288
logging.Errorf("error in Accept for %q: %v", instance, err)
287289
if nerr, ok := err.(net.Error); ok && nerr.Temporary() {
290+
d := 10*time.Millisecond - time.Since(start)
291+
if d > 0 {
292+
time.Sleep(d)
293+
}
288294
continue
289295
}
290296
break

proxy/proxy/client.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -268,10 +268,15 @@ func NewConnSrc(instance string, l net.Listener) <-chan Conn {
268268
ch := make(chan Conn)
269269
go func() {
270270
for {
271+
start := time.Now()
271272
c, err := l.Accept()
272273
if err != nil {
273274
logging.Errorf("listener (%#v) had error: %v", l, err)
274275
if nerr, ok := err.(net.Error); ok && nerr.Temporary() {
276+
d := 10*time.Millisecond - time.Since(start)
277+
if d > 0 {
278+
time.Sleep(d)
279+
}
275280
continue
276281
}
277282
l.Close()

0 commit comments

Comments
 (0)