Skip to content

Commit f8c584a

Browse files
authored
chore: cleanup instance.go (#227)
This is a port of GoogleCloudPlatform/cloud-sql-go-connector#449.
1 parent 0170d37 commit f8c584a

File tree

1 file changed

+44
-37
lines changed

1 file changed

+44
-37
lines changed

internal/alloydb/instance.go

Lines changed: 44 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,8 @@ const refreshBuffer = 4 * time.Minute
3434
var (
3535
// Instance URI is in the format:
3636
// '/projects/<PROJECT>/locations/<REGION>/clusters/<CLUSTER>/instances/<INSTANCE>'
37-
// Additionally, we have to support legacy "domain-scoped" projects (e.g. "google.com:PROJECT")
37+
// Additionally, we have to support legacy "domain-scoped" projects
38+
// (e.g. "google.com:PROJECT")
3839
instURIRegex = regexp.MustCompile("projects/([^:]+(:[^:]+)?)/locations/([^:]+)/clusters/([^:]+)/instances/([^:]+)")
3940
)
4041

@@ -71,8 +72,9 @@ func parseInstURI(cn string) (instanceURI, error) {
7172
return c, nil
7273
}
7374

74-
// refreshOperation is a pending result of a refresh operation of data used to connect securely. It should
75-
// only be initialized by the Instance struct as part of a refresh cycle.
75+
// refreshOperation is a pending result of a refresh operation of data used to
76+
// connect securely. It should only be initialized by the Instance struct as
77+
// part of a refresh cycle.
7678
type refreshOperation struct {
7779
result refreshResult
7880
err error
@@ -83,24 +85,16 @@ type refreshOperation struct {
8385
ready chan struct{}
8486
}
8587

86-
// Cancel prevents the instanceInfo from starting, if it hasn't already started. Returns true if timer
87-
// was stopped successfully, or false if it has already started.
88-
func (r *refreshOperation) Cancel() bool {
88+
// Cancel prevents the instanceInfo from starting, if it hasn't already
89+
// started. Returns true if timer was stopped successfully, or false if it has
90+
// already started.
91+
func (r *refreshOperation) cancel() bool {
8992
return r.timer.Stop()
9093
}
9194

92-
// Wait blocks until the refreshOperation attempt is completed.
93-
func (r *refreshOperation) Wait(ctx context.Context) error {
94-
select {
95-
case <-r.ready:
96-
return r.err
97-
case <-ctx.Done():
98-
return ctx.Err()
99-
}
100-
}
101-
102-
// IsValid returns true if this result is complete, successful, and is still valid.
103-
func (r *refreshOperation) IsValid() bool {
95+
// IsValid returns true if this result is complete, successful, and is still
96+
// valid.
97+
func (r *refreshOperation) isValid() bool {
10498
// verify the result has finished running
10599
select {
106100
default:
@@ -126,15 +120,16 @@ type Instance struct {
126120
r refresher
127121

128122
resultGuard sync.RWMutex
129-
// cur represents the current refreshOperation that will be used to create connections. If a valid complete
130-
// refreshOperation isn't available it's possible for cur to be equal to next.
123+
// cur represents the current refreshOperation that will be used to
124+
// create connections. If a valid complete refreshOperation isn't
125+
// available it's possible for cur to be equal to next.
131126
cur *refreshOperation
132-
// next represents a future or ongoing refreshOperation. Once complete, it will replace cur and schedule a
133-
// replacement to occur.
127+
// next represents a future or ongoing refreshOperation. Once complete,
128+
// it will replace cur and schedule a replacement to occur.
134129
next *refreshOperation
135130

136-
// ctx is the default ctx for refresh operations. Canceling it prevents new refresh
137-
// operations from being triggered.
131+
// ctx is the default ctx for refresh operations. Canceling it prevents
132+
// new refresh operations from being triggered.
138133
ctx context.Context
139134
cancel context.CancelFunc
140135
}
@@ -165,8 +160,8 @@ func NewInstance(
165160
ctx: ctx,
166161
cancel: cancel,
167162
}
168-
// For the initial refresh operation, set cur = next so that connection requests block
169-
// until the first refresh is complete.
163+
// For the initial refresh operation, set cur = next so that connection
164+
// requests block until the first refresh is complete.
170165
i.resultGuard.Lock()
171166
i.cur = i.scheduleRefresh(0)
172167
i.next = i.cur
@@ -189,24 +184,32 @@ func (i *Instance) ConnectInfo(ctx context.Context) (string, *tls.Config, error)
189184
return res.result.instanceIPAddr, res.result.conf, nil
190185
}
191186

192-
// ForceRefresh triggers an immediate refresh operation to be scheduled and used for future connection attempts.
187+
// ForceRefresh triggers an immediate refresh operation to be scheduled and
188+
// used for future connection attempts.
193189
func (i *Instance) ForceRefresh() {
194190
i.resultGuard.Lock()
195191
defer i.resultGuard.Unlock()
196192
// If the next refresh hasn't started yet, we can cancel it and start an immediate one
197-
if i.next.Cancel() {
193+
if i.next.cancel() {
198194
i.next = i.scheduleRefresh(0)
199195
}
200196
// block all sequential connection attempts on the next refresh result
201197
i.cur = i.next
202198
}
203199

204-
// result returns the most recent refresh result (waiting for it to complete if necessary)
200+
// result returns the most recent refresh result (waiting for it to complete if
201+
// necessary)
205202
func (i *Instance) result(ctx context.Context) (*refreshOperation, error) {
206203
i.resultGuard.RLock()
207204
res := i.cur
208205
i.resultGuard.RUnlock()
209-
err := res.Wait(ctx)
206+
var err error
207+
select {
208+
case <-res.ready:
209+
err = res.err
210+
case <-ctx.Done():
211+
err = ctx.Err()
212+
}
210213
if err != nil {
211214
return nil, err
212215
}
@@ -239,22 +242,26 @@ func (i *Instance) scheduleRefresh(d time.Duration) *refreshOperation {
239242
res.result, res.err = i.r.performRefresh(i.ctx, i.instanceURI, i.key)
240243
close(res.ready)
241244

242-
// Once the refresh is complete, update "current" with working result and schedule a new refresh
245+
// Once the refresh is complete, update "current" with working
246+
// result and schedule a new refresh
243247
i.resultGuard.Lock()
244248
defer i.resultGuard.Unlock()
245249
// if failed, scheduled the next refresh immediately
246250
if res.err != nil {
247251
i.next = i.scheduleRefresh(0)
248-
// If the latest result is bad, avoid replacing the used result while it's
249-
// still valid and potentially able to provide successful connections.
250-
// TODO: This means that errors while the current result is still valid are
251-
// surpressed. We should try to surface errors in a more meaningful way.
252-
if !i.cur.IsValid() {
252+
// If the latest result is bad, avoid replacing the
253+
// used result while it's still valid and potentially
254+
// able to provide successful connections. TODO: This
255+
// means that errors while the current result is still
256+
// valid are surpressed. We should try to surface
257+
// errors in a more meaningful way.
258+
if !i.cur.isValid() {
253259
i.cur = res
254260
}
255261
return
256262
}
257-
// Update the current results, and schedule the next refresh in the future
263+
// Update the current results, and schedule the next refresh in
264+
// the future
258265
i.cur = res
259266
select {
260267
case <-i.ctx.Done():

0 commit comments

Comments
 (0)