-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
14 changed files
with
345 additions
and
170 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
export enum ReconnectMsg { | ||
NULL = '', | ||
ALREADY_RECONNECTED = 'Recent reconnection. Not attempting for a few seconds.', | ||
RECONNECTED = 'Reconnected!', | ||
MAX_ATTEMPTS = 'Max attempts reached. Stopped TimeNode.', | ||
RECONNECTING = 'Reconnecting in progress.', | ||
FAIL = 'Reconnection failed! Trying again...' | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,8 @@ | ||
export { Networks } from './Networks'; | ||
export { TxStatus } from './TxStatus'; | ||
export { FnSignatures } from './FnSignatures'; | ||
export { ExecuteStatus } from './ExecuteStatus'; | ||
export { AbortReason } from './AbortReason'; | ||
export { ClaimStatus } from './ClaimStatus'; | ||
export { EconomicStrategyStatus } from './EconomicStrategyStatus'; | ||
export { AbortReason } from './AbortReason'; | ||
export { ExecuteStatus } from './ExecuteStatus'; | ||
export { FnSignatures } from './FnSignatures'; | ||
export { Networks } from './Networks'; | ||
export { ReconnectMsg } from './ReconnectMsg'; | ||
export { TxStatus } from './TxStatus'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
import TimeNode from '../TimeNode'; | ||
import { ReconnectMsg } from '../Enum'; | ||
import W3Util from '../Util'; | ||
|
||
declare const setTimeout: any; | ||
|
||
export default class WsReconnect { | ||
private timenode: TimeNode; | ||
private reconnectTries: number = 0; | ||
private reconnecting: boolean = false; | ||
private reconnected: boolean = false; | ||
|
||
constructor(timenode: TimeNode) { | ||
this.timenode = timenode; | ||
} | ||
|
||
public setup(): void { | ||
const { | ||
logger, | ||
web3: { currentProvider } | ||
} = this.timenode.config; | ||
|
||
currentProvider.on('error', (err: any) => { | ||
logger.debug(`[WS ERROR] ${err}`); | ||
setTimeout(async () => { | ||
const msg: ReconnectMsg = await this.handleWsDisconnect(); | ||
logger.debug(`[WS RECONNECT] ${msg}`); | ||
}, this.reconnectTries * 1000); | ||
}); | ||
|
||
currentProvider.on('end', (err: any) => { | ||
logger.debug(`[WS END] Type= ${err.type} Reason= ${err.reason}`); | ||
setTimeout(async () => { | ||
const msg = await this.handleWsDisconnect(); | ||
logger.debug(`[WS RECONNECT] ${msg}`); | ||
}, this.reconnectTries * 1000); | ||
}); | ||
} | ||
|
||
private async handleWsDisconnect(): Promise<ReconnectMsg> { | ||
if (this.reconnected) { | ||
return ReconnectMsg.ALREADY_RECONNECTED; | ||
} | ||
if (this.reconnectTries >= this.timenode.config.maxRetries) { | ||
this.timenode.stopScanning(); | ||
return ReconnectMsg.MAX_ATTEMPTS; | ||
} | ||
if (this.reconnecting) { | ||
return ReconnectMsg.RECONNECTING; | ||
} | ||
|
||
// Try to reconnect. | ||
this.reconnecting = true; | ||
if (await this.wsReconnect()) { | ||
await this.timenode.startScanning(); | ||
this.reconnectTries = 0; | ||
this.setup(); | ||
this.reconnected = true; | ||
this.reconnecting = false; | ||
setTimeout(() => { | ||
this.reconnected = false; | ||
}, 10000); | ||
return ReconnectMsg.RECONNECTED; | ||
} | ||
|
||
this.reconnecting = false; | ||
this.reconnectTries++; | ||
setTimeout(() => { | ||
this.handleWsDisconnect(); | ||
}, this.reconnectTries * 1000); | ||
|
||
return ReconnectMsg.FAIL; | ||
} | ||
|
||
private async wsReconnect(): Promise<boolean> { | ||
const { | ||
config: { logger, providerUrls } | ||
} = this.timenode; | ||
logger.debug('Attempting WS Reconnect.'); | ||
try { | ||
const providerUrl = providerUrls[this.reconnectTries % providerUrls.length]; | ||
this.timenode.config.web3 = W3Util.getWeb3FromProviderUrl(providerUrl); | ||
|
||
this.timenode.config.util = new W3Util(this.timenode.config.web3); | ||
this.timenode.scanner.util = this.timenode.config.util; | ||
if (await this.timenode.config.util.isWatchingEnabled()) { | ||
return true; | ||
} else { | ||
throw new Error('Invalid providerUrl! eth_getFilterLogs not enabled.'); | ||
} | ||
} catch (err) { | ||
logger.error(err.message); | ||
logger.info(`Reconnect tries: ${this.reconnectTries}`); | ||
return false; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.