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
 {
1
 {
2
   "name": "xrpio",
2
   "name": "xrpio",
3
-  "version": "0.1.4",
3
+  "version": "0.1.6",
4
   "repository": {
4
   "repository": {
5
     "type": "git",
5
     "type": "git",
6
     "url": "https://gitea.nitowa.xyz/npm-packages/xrpio.git"
6
     "url": "https://gitea.nitowa.xyz/npm-packages/xrpio.git"

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

16
 export type Options = {
16
 export type Options = {
17
     debug?: boolean
17
     debug?: boolean
18
     connectionTimeout?: number
18
     connectionTimeout?: number
19
-    readFreshApi?: boolean
19
+    readMaxRetry?: number
20
+    readRetryTimeout?: number
20
 }
21
 }
21
 
22
 
22
 export const defaultOptions = {
23
 export const defaultOptions = {
23
     debug: false,
24
     debug: false,
24
     connectionTimeout: 100000,
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
     private options: Options = defaultOptions
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
     this.api = new Client(server, {
29
     this.api = new Client(server, {
29
       connectionTimeout: this.options.connectionTimeout
30
       connectionTimeout: this.options.connectionTimeout
101
     return tx.result.hash
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
     this.dbg("Getting Tx", hash)
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
       return await this.api.request({
108
       return await this.api.request({
125
         command: 'tx',
109
         command: 'tx',
126
         transaction: hash,
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