@@ -189,7 +189,11 @@ func (i *Instance) OpenConns() *uint64 {
189
189
// Close closes the instance; it stops the refresh cycle and prevents it from
190
190
// making additional calls to the AlloyDB Admin API.
191
191
func (i * Instance ) Close () error {
192
+ i .resultGuard .Lock ()
193
+ defer i .resultGuard .Unlock ()
192
194
i .cancel ()
195
+ i .cur .cancel ()
196
+ i .next .cancel ()
193
197
return nil
194
198
}
195
199
@@ -230,6 +234,8 @@ func (i *Instance) result(ctx context.Context) (*refreshOperation, error) {
230
234
err = res .err
231
235
case <- ctx .Done ():
232
236
err = ctx .Err ()
237
+ case <- i .ctx .Done ():
238
+ err = i .ctx .Err ()
233
239
}
234
240
if err != nil {
235
241
return nil , err
@@ -260,6 +266,13 @@ func (i *Instance) scheduleRefresh(d time.Duration) *refreshOperation {
260
266
r := & refreshOperation {}
261
267
r .ready = make (chan struct {})
262
268
r .timer = time .AfterFunc (d , func () {
269
+ // instance has been closed, don't schedule anything
270
+ if err := i .ctx .Err (); err != nil {
271
+ r .err = err
272
+ close (r .ready )
273
+ return
274
+ }
275
+
263
276
ctx , cancel := context .WithTimeout (i .ctx , i .refreshTimeout )
264
277
defer cancel ()
265
278
@@ -280,6 +293,7 @@ func (i *Instance) scheduleRefresh(d time.Duration) *refreshOperation {
280
293
// result and schedule a new refresh
281
294
i .resultGuard .Lock ()
282
295
defer i .resultGuard .Unlock ()
296
+
283
297
// if failed, scheduled the next refresh immediately
284
298
if r .err != nil {
285
299
i .next = i .scheduleRefresh (0 )
@@ -297,12 +311,6 @@ func (i *Instance) scheduleRefresh(d time.Duration) *refreshOperation {
297
311
// Update the current results, and schedule the next refresh in
298
312
// the future
299
313
i .cur = r
300
- select {
301
- case <- i .ctx .Done ():
302
- // instance has been closed, don't schedule anything
303
- return
304
- default :
305
- }
306
314
t := refreshDuration (time .Now (), i .cur .result .expiry )
307
315
i .next = i .scheduleRefresh (t )
308
316
})
0 commit comments