File tree Expand file tree Collapse file tree 3 files changed +31
-13
lines changed Expand file tree Collapse file tree 3 files changed +31
-13
lines changed Original file line number Diff line number Diff line change @@ -106,14 +106,34 @@ export class CloudSQLInstance {
106
106
} ) as ReturnType < typeof pThrottle > ;
107
107
}
108
108
109
- forceRefresh ( ) : Promise < RefreshResult > {
109
+ forceRefresh ( ) {
110
110
// if a refresh is already ongoing, just await for its promise to fulfill
111
111
// so that a new instance info is available before reconnecting
112
112
if ( this . next ) {
113
- return this . next ;
113
+ return ;
114
114
}
115
115
this . cancelRefresh ( ) ;
116
- return this . refresh ( ) ;
116
+ this . scheduleRefresh ( 0 ) ;
117
+ }
118
+
119
+ // refreshComplete Returns a promise that resolves when the current refresh
120
+ // cycle has completed, either with success or failure. If no refresh is
121
+ // in progress, the promise will resolve immediately.
122
+ refreshComplete ( ) : Promise < void > {
123
+ return new Promise ( ( resolve ) => {
124
+ // setTimeout() to yield execution to allow other refresh background
125
+ // tasks to start.
126
+ setTimeout ( ( ) => {
127
+ if ( this . next ) {
128
+ // If there is a refresh promise in progress, resolve this promise
129
+ // when the refresh is complete.
130
+ this . next . finally ( resolve )
131
+ } else {
132
+ // Else resolve immediately.
133
+ resolve ( )
134
+ }
135
+ } , 0 ) ;
136
+ } ) ;
117
137
}
118
138
119
139
refresh ( ) : Promise < RefreshResult > {
Original file line number Diff line number Diff line change @@ -230,8 +230,8 @@ export class Connector {
230
230
serverCaMode,
231
231
dnsName,
232
232
} ) ;
233
- tlsSocket . once ( 'error' , async ( ) => {
234
- await cloudSqlInstance . forceRefresh ( ) ;
233
+ tlsSocket . once ( 'error' , ( ) => {
234
+ cloudSqlInstance . forceRefresh ( ) ;
235
235
} ) ;
236
236
tlsSocket . once ( 'secureConnect' , async ( ) => {
237
237
cloudSqlInstance . setEstablishedConnection ( ) ;
Original file line number Diff line number Diff line change @@ -269,7 +269,10 @@ t.test('CloudSQLInstance', async t => {
269
269
await CloudSQLInstance . prototype . refresh . call ( instance ) ;
270
270
instance . refresh = CloudSQLInstance . prototype . refresh ;
271
271
} ;
272
- await instance . forceRefresh ( ) ;
272
+
273
+ instance . forceRefresh ( ) ;
274
+ await instance . refreshComplete ( ) ;
275
+
273
276
t . ok (
274
277
cancelRefreshCalled ,
275
278
'should cancelRefresh current refresh cycle on force refresh'
@@ -302,13 +305,8 @@ t.test('CloudSQLInstance', async t => {
302
305
return CloudSQLInstance . prototype . refresh . call ( instance ) ;
303
306
} ;
304
307
305
- const forceRefreshPromise = instance . forceRefresh ( ) ;
306
- t . strictSame (
307
- refreshPromise ,
308
- forceRefreshPromise ,
309
- 'forceRefresh should return same promise ref from initial refresh call'
310
- ) ;
311
- await forceRefreshPromise ;
308
+ instance . forceRefresh ( ) ;
309
+ await instance . refreshComplete ( )
312
310
313
311
t . ok (
314
312
! cancelRefreshCalled ,
You can’t perform that action at this time.
0 commit comments