@@ -69,6 +69,7 @@ export class CloudSQLInstance {
6969 private scheduledRefreshID ?: ReturnType < typeof setTimeout > | null = undefined ;
7070 /* eslint-disable-next-line @typescript-eslint/no-explicit-any */
7171 private throttle ?: any ;
72+ private closed = false ;
7273 public readonly instanceInfo : InstanceConnectionInfo ;
7374 public ephemeralCert ?: SslCert ;
7475 public host ?: string ;
@@ -106,7 +107,7 @@ export class CloudSQLInstance {
106107 } ) as ReturnType < typeof pThrottle > ;
107108 }
108109
109- forceRefresh ( ) {
110+ forceRefresh ( ) {
110111 // if a refresh is already ongoing, just await for its promise to fulfill
111112 // so that a new instance info is available before reconnecting
112113 if ( this . next ) {
@@ -120,23 +121,29 @@ export class CloudSQLInstance {
120121 // cycle has completed, either with success or failure. If no refresh is
121122 // in progress, the promise will resolve immediately.
122123 refreshComplete ( ) : Promise < void > {
123- return new Promise ( ( resolve ) => {
124+ return new Promise ( resolve => {
124125 // setTimeout() to yield execution to allow other refresh background
125126 // tasks to start.
126- setTimeout ( ( ) => {
127- if ( this . next ) {
127+ setTimeout ( ( ) => {
128+ if ( this . next ) {
128129 // If there is a refresh promise in progress, resolve this promise
129130 // when the refresh is complete.
130- this . next . finally ( resolve )
131+ this . next . finally ( resolve ) ;
131132 } else {
132133 // Else resolve immediately.
133- resolve ( )
134+ resolve ( ) ;
134135 }
135136 } , 0 ) ;
136137 } ) ;
137138 }
138139
139140 refresh ( ) : Promise < RefreshResult > {
141+ if ( this . closed ) {
142+ this . scheduledRefreshID = undefined ;
143+ this . next = undefined ;
144+ return Promise . reject ( 'closed' ) ;
145+ }
146+
140147 const currentRefreshId = this . scheduledRefreshID ;
141148
142149 // Since forceRefresh might be invoked during an ongoing refresh
@@ -203,6 +210,10 @@ export class CloudSQLInstance {
203210 // used to create new connections to a Cloud SQL instance. It throws in
204211 // case any of the internal steps fails.
205212 private async performRefresh ( ) : Promise < RefreshResult > {
213+ if ( this . closed ) {
214+ return Promise . reject ( 'closed' ) ;
215+ }
216+
206217 const rsaKeys : RSAKeys = await generateKeys ( ) ;
207218 const metadata : InstanceMetadata =
208219 await this . sqlAdminFetcher . getInstanceMetadata ( this . instanceInfo ) ;
@@ -264,6 +275,9 @@ export class CloudSQLInstance {
264275 }
265276
266277 private scheduleRefresh ( delay : number ) : void {
278+ if ( this . closed ) {
279+ return ;
280+ }
267281 this . scheduledRefreshID = setTimeout ( ( ) => this . refresh ( ) , delay ) ;
268282 }
269283
@@ -280,4 +294,11 @@ export class CloudSQLInstance {
280294 setEstablishedConnection ( ) : void {
281295 this . establishedConnection = true ;
282296 }
297+
298+ // close stops any refresh process in progress and prevents future refresh
299+ // connections.
300+ close ( ) : void {
301+ this . closed = true ;
302+ this . cancelRefresh ( ) ;
303+ }
283304}
0 commit comments