Skip to content

Commit f748bb3

Browse files
Merge remote-tracking branch 'upstream/master'
2 parents 538879d + 1e456b1 commit f748bb3

File tree

5 files changed

+38
-1
lines changed

5 files changed

+38
-1
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ cloud_sql_proxy takes a few arguments to configure:
2020
directory indicated by `-dir` is mounted.
2121
* `-instances="project1:region:instance1,project3:region:instance1"`: A comma-separated list
2222
of instances to open inside `-dir`. Also supports exposing a tcp port instead of using Unix Domain Sockets; see examples below.
23+
Same list can be provided via INSTANCES environment variable, in case when both are provided - proxy will use command line flag.
2324
* `-instances_metadata=metadata_key`: Usable on [GCE](https://cloud.google.com/compute/docs/quickstart) only. The given [GCE metadata](https://cloud.google.com/compute/docs/metadata) key will be
2425
polled for a list of instances to open in `-dir`. The format for the value is the same as the 'instances' flag. A hanging-poll strategy is used, meaning that changes to
2526
the metadata value will be reflected in the `-dir` even while the proxy is

cmd/cloud_sql_proxy/cloud_sql_proxy.go

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,8 @@ Unix socket-based connections.`)
6363
instances = flag.String("instances", "", `Comma-separated list of fully qualified instances (project:region:name)
6464
to connect to. If the name has the suffix '=tcp:port', a TCP server is opened
6565
on the specified port to proxy to that instance. Otherwise, one socket file per
66-
instance is opened in 'dir'. Not compatible with -fuse`)
66+
instance is opened in 'dir'. You may use INSTANCES environment variable
67+
for the same effect. Using both will use value from flag, Not compatible with -fuse`)
6768
instanceSrc = flag.String("instances_metadata", "", `If provided, it is treated as a path to a metadata value which
6869
is polled for a comma-separated list of instances to connect to. For example,
6970
to use the instance metadata value named 'cloud-sql-instances' you would
@@ -138,6 +139,9 @@ Connection:
138139
139140
When connecting over TCP, the -instances parameter is required.
140141
142+
Supplying INSTANCES environment variable achieves the same effect. One can
143+
use that to keep k8s manifest files constant across multiple environments
144+
141145
-instances_metadata
142146
When running on GCE (Google Compute Engine) you can avoid the need to
143147
specify the list of instances on the command line by using the Metadata
@@ -388,6 +392,12 @@ func main() {
388392
log.SetOutput(ioutil.Discard)
389393
}
390394

395+
// TODO: needs a better place for consolidation
396+
// if instances is blank and env var INSTANCES is supplied use it
397+
if envInstances := os.Getenv("INSTANCES"); *instances == "" && envInstances != "" {
398+
*instances = envInstances
399+
}
400+
391401
instList := stringList(*instances)
392402
projList := stringList(*projects)
393403
// TODO: it'd be really great to consolidate flag verification in one place.

cmd/cloud_sql_proxy/proxy.go

Lines changed: 9 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,9 +140,17 @@ 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)
147+
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+
}
152+
continue
153+
}
145154
l.Close()
146155
return
147156
}

proxy/fuse/fuse.go

Lines changed: 9 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,9 +282,17 @@ 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)
289+
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+
}
294+
continue
295+
}
287296
break
288297
}
289298
r.newConn(instance, c)

proxy/proxy/client.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -295,9 +295,17 @@ func NewConnSrc(instance string, l net.Listener) <-chan Conn {
295295
ch := make(chan Conn)
296296
go func() {
297297
for {
298+
start := time.Now()
298299
c, err := l.Accept()
299300
if err != nil {
300301
logging.Errorf("listener (%#v) had error: %v", l, err)
302+
if nerr, ok := err.(net.Error); ok && nerr.Temporary() {
303+
d := 10*time.Millisecond - time.Since(start)
304+
if d > 0 {
305+
time.Sleep(d)
306+
}
307+
continue
308+
}
301309
l.Close()
302310
close(ch)
303311
return

0 commit comments

Comments
 (0)