Skip to content

Commit

Permalink
fixup! fix: silent refresh errors on active connection
Browse files Browse the repository at this point in the history
  • Loading branch information
ruyadorno committed Sep 26, 2023
1 parent 8bdf41b commit 095ecc4
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 19 deletions.
29 changes: 15 additions & 14 deletions src/cloud-sql-instance.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ export class CloudSQLInstance {
private readonly authType: AuthTypes;
private readonly sqlAdminFetcher: Fetcher;
private readonly limitRateInterval: number;
private stablishedConnection: boolean = false;
private establishedConnection: boolean = false;
// The ongoing refresh promise is referenced by the `next` property
private next?: Promise<RefreshResult>;
private scheduledRefreshID?: ReturnType<typeof setTimeout> | null = undefined;
Expand Down Expand Up @@ -142,18 +142,24 @@ export class CloudSQLInstance {
// then we go ahead and update values
this.updateValues(nextValues);

this.scheduleRefresh();

const refreshInterval = getRefreshInterval(
/* c8 ignore next */
String(this.ephemeralCert?.expirationTime)
);
this.scheduleRefresh(refreshInterval);

// This is the end of the successful refresh chain, so now
// we release the reference to the next
this.next = undefined;
})
.catch((err: unknown) => {
// In case there's already an active connection we won't throw
// refresh errors to the final user, scheduling a new refresh instead.
if (this.stablishedConnection) {
// refresh errors to the final user, scheduling a new
// immediate refresh instead.
if (this.establishedConnection) {
if (currentRefreshId === this.scheduledRefreshID) {
this.scheduleRefresh();
this.scheduleRefresh(0);
}
} else {
throw err as Error;
Expand Down Expand Up @@ -234,13 +240,8 @@ export class CloudSQLInstance {
this.serverCaCert = serverCaCert;
}

private scheduleRefresh(): void {
const refreshInterval = getRefreshInterval(
/* c8 ignore next */
String(this.ephemeralCert?.expirationTime)
);

this.scheduledRefreshID = setTimeout(() => this.refresh(), refreshInterval);
private scheduleRefresh(delay: number): void {
this.scheduledRefreshID = setTimeout(() => this.refresh(), delay);
}

cancelRefresh(): void {
Expand All @@ -253,7 +254,7 @@ export class CloudSQLInstance {
// Mark this instance as having an active connection. This is important to
// ensure any possible errors thrown during a future refresh cycle should
// not be thrown to the final user.
setStablishedConnection(): void {
this.stablishedConnection = true;
setEstablishedConnection(): void {
this.establishedConnection = true;
}
}
2 changes: 1 addition & 1 deletion src/connector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,7 @@ export class Connector {
await cloudSqlInstance.forceRefresh();
});
tlsSocket.once('secureConnect', async () => {
cloudSqlInstance.setStablishedConnection();
cloudSqlInstance.setEstablishedConnection();
});
return tlsSocket;
}
Expand Down
8 changes: 4 additions & 4 deletions test/cloud-sql-instance.ts
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ t.test('CloudSQLInstance', async t => {
};
// starts out refresh logic
instance.refresh();
instance.setStablishedConnection();
instance.setEstablishedConnection();
}))();
}
);
Expand Down Expand Up @@ -239,7 +239,7 @@ t.test('CloudSQLInstance', async t => {
};
// starts out refresh logic
instance.refresh();
instance.setStablishedConnection();
instance.setEstablishedConnection();
}))();
}
);
Expand Down Expand Up @@ -470,7 +470,7 @@ t.test('CloudSQLInstance', async t => {
});

await instance.refresh();
instance.setStablishedConnection();
instance.setEstablishedConnection();

// starts a new refresh cycle but do not await on it
instance.refresh();
Expand Down Expand Up @@ -550,7 +550,7 @@ t.test('CloudSQLInstance', async t => {
};
// starts out refresh logic
instance.refresh();
instance.setStablishedConnection();
instance.setEstablishedConnection();
}))();
}
);
Expand Down

0 comments on commit 095ecc4

Please sign in to comment.