Browse Source

add getTransaction retry and retry-timeout

master
nitowa 2 years ago
parent
commit
2b340fbeaa
3 changed files with 21 additions and 25 deletions
  1. 1
    1
      package.json
  2. 5
    2
      src/util/types.ts
  3. 15
    22
      src/xrpIO/xrpl-binding.ts

+ 1
- 1
package.json View File

@@ -1,6 +1,6 @@
1 1
 {
2 2
   "name": "xrpio",
3
-  "version": "0.1.4",
3
+  "version": "0.1.6",
4 4
   "repository": {
5 5
     "type": "git",
6 6
     "url": "https://gitea.nitowa.xyz/npm-packages/xrpio.git"

+ 5
- 2
src/util/types.ts View File

@@ -16,11 +16,14 @@ export type TxHash = string
16 16
 export type Options = {
17 17
     debug?: boolean
18 18
     connectionTimeout?: number
19
-    readFreshApi?: boolean
19
+    readMaxRetry?: number
20
+    readRetryTimeout?: number
20 21
 }
21 22
 
22 23
 export const defaultOptions = {
23 24
     debug: false,
24 25
     connectionTimeout: 100000,
25
-    readFreshApi: true
26
+    readFreshApi: true,
27
+    readMaxRetry: -1,
28
+    readRetryTimeout: 1000
26 29
   }

+ 15
- 22
src/xrpIO/xrpl-binding.ts View File

@@ -21,9 +21,10 @@ export class xrpIO {
21 21
     private options: Options = defaultOptions
22 22
   ) {
23 23
 
24
-    this.options.debug = this.options.debug ? Boolean(this.options.debug) : defaultOptions.debug
25
-    this.options.connectionTimeout = this.options.connectionTimeout ? Number(this.options.connectionTimeout) : defaultOptions.connectionTimeout
26
-    this.options.readFreshApi = this.options.readFreshApi ? Boolean(this.options.readFreshApi) : defaultOptions.readFreshApi
24
+    this.options.debug = options.debug ? Boolean(options.debug) : defaultOptions.debug
25
+    this.options.connectionTimeout = options.connectionTimeout ? Number(options.connectionTimeout) : defaultOptions.connectionTimeout
26
+    this.options.readMaxRetry = options.readMaxRetry ? Number(options.readMaxRetry) : defaultOptions.readMaxRetry
27
+    this.options.readRetryTimeout = options.readRetryTimeout ? Number(options.readRetryTimeout) : defaultOptions.readRetryTimeout
27 28
 
28 29
     this.api = new Client(server, {
29 30
       connectionTimeout: this.options.connectionTimeout
@@ -101,30 +102,22 @@ export class xrpIO {
101 102
     return tx.result.hash
102 103
   }
103 104
 
104
-  private async getTransaction(hash: string): Promise<TxResponse> {
105
+  private async getTransaction(hash: string, retry=0): Promise<TxResponse> {
105 106
     this.dbg("Getting Tx", hash)
106
-
107
-    if (this.options.readFreshApi) {
108
-      let _api = await this.cloneApi()
109
-      while (true) {
110
-        try {
111
-          const response = await _api.request({
112
-            command: 'tx',
113
-            transaction: hash,
114
-          })
115
-          await _api.disconnect()
116
-          return response
117
-        } catch (e) {
118
-          this.dbg("Retrying to get", hash)
119
-          await _api.disconnect()
120
-          _api = await this.cloneApi()
121
-        }
122
-      }
123
-    }else{
107
+    try{
124 108
       return await this.api.request({
125 109
         command: 'tx',
126 110
         transaction: hash,
127 111
       })
112
+    }catch(e){
113
+      this.dbg(e)
114
+      if(this.options.readMaxRetry != -1){
115
+        if(retry >= this.options.readMaxRetry)
116
+          console.error("Retry limit exceeded for", hash, ". this is an irrecoverable error")
117
+          throw e
118
+      }
119
+      await new Promise(res => setTimeout(res, this.options.readRetryTimeout))
120
+      return await this.getTransaction(hash, retry+1)
128 121
     }
129 122
   }
130 123
 

Loading…
Cancel
Save