Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: connection timeout multiplier #1838

Merged
merged 2 commits into from
Jan 8, 2024
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
feat: timeout multiplier
  • Loading branch information
Ross Bulat committed Jan 8, 2024
commit 7cc33ecd993c7b5f82cd8eca226593fd35f67c27
19 changes: 15 additions & 4 deletions src/static/APController/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ export class APIController {
// Class members.
// ------------------------------------------------------

// How long in ms to wait for a connection before trying again.
static CONNECT_TIMEOUT = 10000;
// The bas time in ms to wait for a connection before trying again. Increases up to 6x.
static CONNECT_TIMEOUT_BASE = 10000;

// How many blocks to wait before verifying the connection is online.
static MIN_EXPECTED_BLOCKS_PER_VERIFY = 4;
Expand Down Expand Up @@ -123,9 +123,20 @@ export class APIController {

// Start connection attempt.
this.onMonitorConnect(config);
await withTimeout(this.CONNECT_TIMEOUT, this.connect(config));
await withTimeout(this.getTimeout(), this.connect(config));
}

// Calculate connection timeout. First attempt = base, second = 3x, 6x thereafter.
static getTimeout = () => {
if (this._connectAttempts === 1) {
return this.CONNECT_TIMEOUT_BASE;
} else if (this._connectAttempts === 2) {
return this.CONNECT_TIMEOUT_BASE * 3;
} else {
return this.CONNECT_TIMEOUT_BASE * 6;
}
};

// Check if API is connected after a ser period, and try again if it has not.
static onMonitorConnect = async (config: APIConfig) => {
setTimeout(() => {
Expand All @@ -134,7 +145,7 @@ export class APIController {
// Atempt api connection again.
this.initialize(config.network, config.type, config.rpcEndpoint);
}
}, this.CONNECT_TIMEOUT);
}, this.getTimeout());
};

// Instantiates provider and connects to an api instance.
Expand Down
Loading