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
+1 -1
View File
@@ -1,6 +1,6 @@
{ {
"name": "xrpio", "name": "xrpio",
"version": "0.1.4", "version": "0.1.6",
"repository": { "repository": {
"type": "git", "type": "git",
"url": "https://gitea.nitowa.xyz/npm-packages/xrpio.git" "url": "https://gitea.nitowa.xyz/npm-packages/xrpio.git"
+5 -2
View File
@@ -16,11 +16,14 @@ export type TxHash = string
export type Options = { export type Options = {
debug?: boolean debug?: boolean
connectionTimeout?: number connectionTimeout?: number
readFreshApi?: boolean readMaxRetry?: number
readRetryTimeout?: number
} }
export const defaultOptions = { export const defaultOptions = {
debug: false, debug: false,
connectionTimeout: 100000, 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 private options: Options = defaultOptions
) { ) {
this.options.debug = this.options.debug ? Boolean(this.options.debug) : defaultOptions.debug this.options.debug = options.debug ? Boolean(options.debug) : defaultOptions.debug
this.options.connectionTimeout = this.options.connectionTimeout ? Number(this.options.connectionTimeout) : defaultOptions.connectionTimeout this.options.connectionTimeout = options.connectionTimeout ? Number(options.connectionTimeout) : defaultOptions.connectionTimeout
this.options.readFreshApi = this.options.readFreshApi ? Boolean(this.options.readFreshApi) : defaultOptions.readFreshApi 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, { this.api = new Client(server, {
connectionTimeout: this.options.connectionTimeout connectionTimeout: this.options.connectionTimeout
@@ -101,30 +102,22 @@ export class xrpIO {
return tx.result.hash 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) this.dbg("Getting Tx", hash)
try{
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{
return await this.api.request({ return await this.api.request({
command: 'tx', command: 'tx',
transaction: hash, 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)
} }
} }