-
Notifications
You must be signed in to change notification settings - Fork 684
fix: implement backoff retry policy for websocket handler #3600
base: develop
Are you sure you want to change the base?
Changes from 2 commits
911adb5
9d7e351
8611d3c
633aabe
1cf558c
cdb5368
95dc5d3
12f9759
926e213
7cdc04e
848bf46
c9f24bc
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -20,6 +20,12 @@ export class WsHandler extends BaseHandler implements Handler { | |
| }> | ||
| >(); | ||
|
|
||
| // retry configuration | ||
| private retryIntervalBase: number = 3; | ||
| private retryCounter: number = 5; | ||
| private initialRetryCounter = this.retryCounter; | ||
| private retryTimeoutId: NodeJS.Timeout; | ||
|
|
||
| constructor(options: EthereumInternalOptions, abortSignal: AbortSignal) { | ||
| super(options, abortSignal); | ||
|
|
||
|
|
@@ -46,10 +52,21 @@ export class WsHandler extends BaseHandler implements Handler { | |
| this.open = this.connect(this.connection, logging); | ||
| this.connection.onclose = () => { | ||
satyajeetkolhapure marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| // try to connect again... | ||
| // Issue: https://github.com/trufflesuite/ganache/issues/3476 | ||
| // TODO: backoff and eventually fail | ||
| // Issue: https://github.com/trufflesuite/ganache/issues/3477 | ||
| this.open = this.connect(this.connection, logging); | ||
| // backoff and eventually fail | ||
| if( this.retryCounter > 0 || this.retryCounter == -1 ) { | ||
satyajeetkolhapure marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| if( this.retryCounter !== -1 ) this.retryCounter--; | ||
| clearTimeout( this.retryTimeoutId ); | ||
| this.retryTimeoutId = setTimeout( () => { | ||
| const onCloseEvent = this.connection.onclose; | ||
| this.connection = new WebSocket(url.toString(), { | ||
satyajeetkolhapure marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| origin, | ||
| headers: this.headers | ||
| }); | ||
| this.connection.binaryType = "nodebuffer"; | ||
| this.connection.onclose = onCloseEvent; | ||
| this.open = this.connect(this.connection, logging); | ||
|
||
| }, Math.pow( this.retryIntervalBase, this.initialRetryCounter - this.retryCounter ) * 1000 ); | ||
| } | ||
| }; | ||
| this.abortSignal.addEventListener("abort", () => { | ||
| this.connection.onclose = null; | ||
|
|
@@ -117,6 +134,8 @@ export class WsHandler extends BaseHandler implements Handler { | |
| () => { | ||
| connection.onopen = null; | ||
| connection.onerror = null; | ||
| // reset the retry counter | ||
| this.retryCounter = this.initialRetryCounter; | ||
| }, | ||
| err => { | ||
| logging.logger.log(err); | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.