add getTransaction retry and retry-timeout
This commit is contained in:
+1
-1
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "xrpio",
|
||||
"version": "0.1.4",
|
||||
"version": "0.1.6",
|
||||
"repository": {
|
||||
"type": "git",
|
||||
"url": "https://gitea.nitowa.xyz/npm-packages/xrpio.git"
|
||||
|
||||
+5
-2
@@ -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
@@ -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)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user