Skip to content

Commit

Permalink
fix: avoid scheduling an instance refresh if the context is done
Browse files Browse the repository at this point in the history
Fixes #493
  • Loading branch information
jomaresch authored and enocom committed Jan 24, 2024
1 parent ff5ca35 commit 8429586
Showing 1 changed file with 7 additions and 6 deletions.
13 changes: 7 additions & 6 deletions internal/alloydb/instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,7 @@ func (i *Instance) OpenConns() *uint64 {
// making additional calls to the AlloyDB Admin API.
func (i *Instance) Close() error {
i.cancel()
i.next.cancel()
return nil
}

Expand Down Expand Up @@ -260,6 +261,11 @@ func (i *Instance) scheduleRefresh(d time.Duration) *refreshOperation {
r := &refreshOperation{}
r.ready = make(chan struct{})
r.timer = time.AfterFunc(d, func() {
// instance has been closed, don't schedule anything
if i.ctx.Err() != nil {
return
}

ctx, cancel := context.WithTimeout(i.ctx, i.refreshTimeout)
defer cancel()

Expand All @@ -280,6 +286,7 @@ func (i *Instance) scheduleRefresh(d time.Duration) *refreshOperation {
// result and schedule a new refresh
i.resultGuard.Lock()
defer i.resultGuard.Unlock()

// if failed, scheduled the next refresh immediately
if r.err != nil {
i.next = i.scheduleRefresh(0)
Expand All @@ -297,12 +304,6 @@ func (i *Instance) scheduleRefresh(d time.Duration) *refreshOperation {
// Update the current results, and schedule the next refresh in
// the future
i.cur = r
select {
case <-i.ctx.Done():
// instance has been closed, don't schedule anything
return
default:
}
t := refreshDuration(time.Now(), i.cur.result.expiry)
i.next = i.scheduleRefresh(t)
})
Expand Down

0 comments on commit 8429586

Please sign in to comment.