Skip to content

Commit 538879d

Browse files
Handle refreshCfg errors and a few variable tweaks.
1 parent 4c9bdd3 commit 538879d

File tree

1 file changed

+13
-8
lines changed

1 file changed

+13
-8
lines changed

proxy/proxy/client.go

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -83,10 +83,10 @@ type Client struct {
8383
// instance. Relevant functions are refreshCfg and cachedCfg. It is
8484
// protected by cacheL.
8585
cfgCache map[string]cacheEntry
86-
cacheL sync.RWMutex
86+
cacheL sync.Mutex
8787

88-
// cfgL prevents multiple goroutines from contacting the Cloud SQL API at once.
89-
cfgL sync.Mutex
88+
// refreshCfgL prevents multiple goroutines from contacting the Cloud SQL API at once.
89+
refreshCfgL sync.Mutex
9090

9191
// MaxConnections is the maximum number of connections to establish
9292
// before refusing new connections. 0 means no limit.
@@ -156,8 +156,8 @@ func (c *Client) handleConn(conn Conn) {
156156
// address as well as construct a new tls.Config to connect to the instance. It
157157
// caches the result.
158158
func (c *Client) refreshCfg(instance string) (addr string, cfg *tls.Config, err error) {
159-
c.cfgL.Lock()
160-
defer c.cfgL.Unlock()
159+
c.refreshCfgL.Lock()
160+
defer c.refreshCfgL.Unlock()
161161

162162
throttle := c.RefreshCfgThrottle
163163
if throttle == 0 {
@@ -178,6 +178,9 @@ func (c *Client) refreshCfg(instance string) (addr string, cfg *tls.Config, err
178178
}
179179

180180
defer func() {
181+
if err != nil && oldok {
182+
return
183+
}
181184
c.cacheL.Lock()
182185
c.cfgCache[instance] = cacheEntry{
183186
lastRefreshed: time.Now(),
@@ -212,17 +215,19 @@ func (c *Client) refreshCfg(instance string) (addr string, cfg *tls.Config, err
212215
go func() {
213216
<-time.After(timeToRefresh)
214217
logging.Verbosef("Cert for instance %s will expire soon, refreshing now.", instance)
215-
c.refreshCfg(instance)
218+
if _, _, err := c.refreshCfg(instance); err != nil {
219+
logging.Errorf("couldn't connect to %q: %v", instance, err)
220+
}
216221
}()
217222
}
218223

219224
return fmt.Sprintf("%s:%d", addr, c.Port), cfg, nil
220225
}
221226

222227
func (c *Client) cachedCfg(instance string) (string, *tls.Config) {
223-
c.cacheL.RLock()
228+
c.cacheL.Lock()
224229
ret, ok := c.cfgCache[instance]
225-
c.cacheL.RUnlock()
230+
c.cacheL.Unlock()
226231

227232
// Don't waste time returning an expired/invalid cert.
228233
if !ok || ret.err != nil || time.Now().After(ret.cfg.Certificates[0].Leaf.NotAfter) {

0 commit comments

Comments
 (0)