add getTransaction retry and retry-timeout

This commit is contained in:
nitowa
2022-03-28 06:09:11 +02:00
parent d1d754ae51
commit 2b340fbeaa
3 changed files with 21 additions and 25 deletions
+5 -2
View File
@@ -16,11 +16,14 @@ export type TxHash = string
export type Options = {
debug?: boolean
connectionTimeout?: number
readFreshApi?: boolean
readMaxRetry?: number
readRetryTimeout?: number
}
export const defaultOptions = {
debug: false,
connectionTimeout: 100000,
readFreshApi: true
readFreshApi: true,
readMaxRetry: -1,
readRetryTimeout: 1000
}
+15 -22
View File
@@ -21,9 +21,10 @@ export class xrpIO {
private options: Options = defaultOptions
) {
this.options.debug = this.options.debug ? Boolean(this.options.debug) : defaultOptions.debug
this.options.connectionTimeout = this.options.connectionTimeout ? Number(this.options.connectionTimeout) : defaultOptions.connectionTimeout
this.options.readFreshApi = this.options.readFreshApi ? Boolean(this.options.readFreshApi) : defaultOptions.readFreshApi
this.options.debug = options.debug ? Boolean(options.debug) : defaultOptions.debug
this.options.connectionTimeout = options.connectionTimeout ? Number(options.connectionTimeout) : defaultOptions.connectionTimeout
this.options.readMaxRetry = options.readMaxRetry ? Number(options.readMaxRetry) : defaultOptions.readMaxRetry
this.options.readRetryTimeout = options.readRetryTimeout ? Number(options.readRetryTimeout) : defaultOptions.readRetryTimeout
this.api = new Client(server, {
connectionTimeout: this.options.connectionTimeout
@@ -101,30 +102,22 @@ export class xrpIO {
return tx.result.hash
}
private async getTransaction(hash: string): Promise<TxResponse> {
private async getTransaction(hash: string, retry=0): Promise<TxResponse> {
this.dbg("Getting Tx", hash)
if (this.options.readFreshApi) {
let _api = await this.cloneApi()
while (true) {
try {
const response = await _api.request({
command: 'tx',
transaction: hash,
})
await _api.disconnect()
return response
} catch (e) {
this.dbg("Retrying to get", hash)
await _api.disconnect()
_api = await this.cloneApi()
}
}
}else{
try{
return await this.api.request({
command: 'tx',
transaction: hash,
})
}catch(e){
this.dbg(e)
if(this.options.readMaxRetry != -1){
if(retry >= this.options.readMaxRetry)
console.error("Retry limit exceeded for", hash, ". this is an irrecoverable error")
throw e
}
await new Promise(res => setTimeout(res, this.options.readRetryTimeout))
return await this.getTransaction(hash, retry+1)
}
}