Skip to content
This repository has been archived by the owner on Jul 18, 2024. It is now read-only.

Commit

Permalink
Add ability to configure connection timeout (#136)
Browse files Browse the repository at this point in the history
The connection timeout is currently hardcoded to 2 seconds. It is likely that this is not going to work in all situations, so I'm adding a configuration knob to allow the user to set whatever timeout they'd like.

Depending on feedback, the default timeout may be increased as well. The balance is that if the extension is attempting to discover the database, increases in timeouts will result in slower extension startup.

This PR is related to #135
  • Loading branch information
mctavish authored May 10, 2023
1 parent 81af6f4 commit a1056c5
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 3 deletions.
4 changes: 4 additions & 0 deletions snapshot_dbg_extension/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,10 @@
"debugOutput": {
"type": "boolean",
"description": "If enabled, will output extension debug messages to the console."
},
"connectionTimeoutMs": {
"type": "number",
"description": "Timeout for initial firebase database connection, in milliseconds."
}
}
}
Expand Down
13 changes: 10 additions & 3 deletions snapshot_dbg_extension/src/adapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import { debugLog, setDebugLogEnabled } from './debugUtil';
import { pickDebuggeeId } from './debuggeePicker';
import { BreakpointManager } from './breakpointManager';
import { GcloudCredential } from './gcloudCredential';
import { debuglog } from 'util';

const FIREBASE_APP_NAME = 'snapshotdbg';
const INITIALIZE_TIME_ALLOWANCE_MS = 2 * 1000; // 2 seconds
Expand All @@ -45,6 +46,9 @@ interface IAttachRequestArguments extends DebugProtocol.AttachRequestArguments {

/** Whether to output debug messages to console. */
debugOutput: boolean;

/** Timeout for initial firebase database connection, in milliseconds. */
connectionTimeoutMs: number | undefined;
}


Expand Down Expand Up @@ -122,8 +126,9 @@ export class SnapshotDebuggerSession extends DebugSession {
}
}

private async connectToFirebase(credential: GcloudCredential, progress: vscode.Progress<{ message: string, increment: number }>, configuredDatabaseUrl?: string): Promise<Database> {
private async connectToFirebase(credential: GcloudCredential, progress: vscode.Progress<{ message: string, increment: number }>, args?: IAttachRequestArguments): Promise<Database> {
// Build the database URL.
const configuredDatabaseUrl = args?.databaseUrl;
const databaseUrls = [];
if (configuredDatabaseUrl) {
databaseUrls.push(configuredDatabaseUrl);
Expand All @@ -132,6 +137,8 @@ export class SnapshotDebuggerSession extends DebugSession {
databaseUrls.push(`https://${this.projectId}-default-rtdb.firebaseio.com`);
}

const timeout = args?.connectionTimeoutMs ?? 2000;
debugLog(`Timeout set to ${timeout}`);
for (const databaseUrl of databaseUrls) {
progress.report({ message: `Connecting to ${databaseUrl}`, increment: 20 });
this.app = initializeApp({
Expand All @@ -144,7 +151,7 @@ export class SnapshotDebuggerSession extends DebugSession {
// Test the connection by reading the schema version.
try {
const version_snapshot = await withTimeout(
2000,
timeout,
db.ref('cdbg/schema_version').get());
if (version_snapshot) {
const version = version_snapshot.val();
Expand Down Expand Up @@ -200,7 +207,7 @@ export class SnapshotDebuggerSession extends DebugSession {
}

try {
this.db = await this.connectToFirebase(credential, progress, args.databaseUrl);
this.db = await this.connectToFirebase(credential, progress, args);
} catch (err) {
this.sendErrorResponse(response, 2,
'Cannot connect to Firebase.\n\n' +
Expand Down

0 comments on commit a1056c5

Please sign in to comment.