Skip to content

Commit 47bd3f3

Browse files
fix: improve reliability of certificate refresh (#448)
1 parent ef6a39d commit 47bd3f3

File tree

2 files changed

+14
-10
lines changed

2 files changed

+14
-10
lines changed

internal/cloudsql/instance.go

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,10 @@ import (
2727
sqladmin "google.golang.org/api/sqladmin/v1beta4"
2828
)
2929

30+
// the refresh buffer is the amount of time before a refresh's result expires
31+
// that a new refresh operation begins.
32+
const refreshBuffer = 4 * time.Minute
33+
3034
var (
3135
// Instance connection name is the format <PROJECT>:<REGION>:<INSTANCE>
3236
// Additionally, we have to support legacy "domain-scoped" projects (e.g. "google.com:PROJECT")
@@ -115,8 +119,8 @@ type RefreshCfg struct {
115119
}
116120

117121
// Instance manages the information used to connect to the Cloud SQL instance by periodically calling
118-
// the Cloud SQL Admin API. It automatically refreshes the required information approximately 5 minutes
119-
// before the previous certificate expires (every 55 minutes).
122+
// the Cloud SQL Admin API. It automatically refreshes the required information approximately 4 minutes
123+
// before the previous certificate expires (every ~56 minutes).
120124
type Instance struct {
121125
// OpenConns is the number of open connections to the instance.
122126
OpenConns uint64
@@ -275,11 +279,11 @@ func refreshDuration(now, certExpiry time.Time) time.Duration {
275279
d := certExpiry.Sub(now)
276280
if d < time.Hour {
277281
// Something is wrong with the certificate, refresh now.
278-
if d < 5*time.Minute {
282+
if d < refreshBuffer {
279283
return 0
280284
}
281-
// Otherwise, wait five minutes before starting the refresh cycle.
282-
return 5 * time.Minute
285+
// Otherwise wait until 4 minutes before expiration for next refresh cycle.
286+
return d - refreshBuffer
283287
}
284288
return d / 2
285289
}

internal/cloudsql/instance_test.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -278,13 +278,13 @@ func TestRefreshDuration(t *testing.T) {
278278
want: 30 * time.Minute,
279279
},
280280
{
281-
desc: "when expiration is less than 1 hour, but greater than 5 minutes",
282-
expiry: now.Add(6 * time.Minute),
283-
want: 5 * time.Minute,
281+
desc: "when expiration is less than 1 hour, but greater than 4 minutes",
282+
expiry: now.Add(5 * time.Minute),
283+
want: time.Minute,
284284
},
285285
{
286-
desc: "when expiration is less than 5 minutes",
287-
expiry: now.Add(4 * time.Minute),
286+
desc: "when expiration is less than 4 minutes",
287+
expiry: now.Add(3 * time.Minute),
288288
want: 0,
289289
},
290290
{

0 commit comments

Comments
 (0)