Compare View
Commits (5)
Changes
Showing 2 changed files Inline Diff
gateway/resend-delay.js
1 | "use strict"; | 1 | "use strict"; |
2 | 2 | ||
3 | var LRU = require('lru-cache'); | 3 | var LRU = require('lru-cache'); |
4 | 4 | ||
5 | const config = require('./config'); | 5 | const config = require('./config'); |
6 | const logger = require('./logger'); | 6 | const logger = require('./logger'); |
7 | 7 | ||
8 | const resendHandlers = LRU({max: 2000, maxAge: 1000 * 3600 * 36}); | 8 | const resendHandlers = LRU({max: 2000, maxAge: 1000 * 3600 * 36}); |
9 | |||
10 | function cancel(task) { | ||
11 | const trx_id = ( typeof task === 'string' ) ? task : task.trx_id; | ||
12 | if (!trx_id) { return; } | ||
13 | |||
14 | const oldHandler = resendHandlers.get(trx_id); | ||
15 | if (!oldHandler) { return; } | ||
16 | |||
17 | const task = oldHandler.task; | ||
18 | logger.verbose('Canceling resend delay', {trx_id: task.trx_id, destination: task.destination, product: task.product, remote_product: task.remote_product}); | ||
19 | 9 | ||
20 | if (oldHandler.handler) { clearTimeout(oldHandler.handler); } | 10 | function cancel(task) { |
21 | resendHandlers.del(trx_id); | 11 | const trx_id = ( typeof task === 'string' ) ? task : task.trx_id; |
22 | } | 12 | if (!trx_id) { return; } |
23 | |||
24 | function register(task, request) { | ||
25 | if (!task.trx_id) { | ||
26 | logger.warn('Invalid task on resendDelay') | ||
27 | return; | ||
28 | } | ||
29 | |||
30 | if (!request || !config || !config.auto_resend || !Number(config.auto_resend.delay_ms) || !Number(config.auto_resend.max_retry)) { | ||
31 | return; | ||
32 | } | ||
33 | |||
34 | let retry = config.auto_resend.max_retry; | ||
35 | const oldHandler = resendHandlers.get(task.trx_id); | ||
36 | if (oldHandler) { | 13 | |
37 | retry = oldHandler.retry - 1; | 14 | const oldHandler = resendHandlers.get(trx_id); |
15 | if (!oldHandler) { return; } | ||
38 | cancel(task); | 16 | |
39 | } | 17 | const task = oldHandler.task; |
40 | 18 | logger.verbose('Canceling resend delay', {trx_id: task.trx_id, destination: task.destination, product: task.product, remote_product: task.remote_product}); | |
41 | if (retry <= 0) { | ||
42 | logger.verbose('Resend delay retry exceeded', {trx_id: task.trx_id, destination: task.destination, product: task.product, remote_product: task.remote_product}); | ||
43 | cancel(task); | ||
44 | return; | ||
45 | } | 19 | |
46 | 20 | if (oldHandler.handler) { clearTimeout(oldHandler.handler); } | |
47 | logger.verbose('Registering resend delay task request', {trx_id: task.trx_id, destination: task.destination, product: task.product, remote_product: task.remote_product, delay_ms: config.auto_resend.delay_ms, retry: retry}); | 21 | resendHandlers.del(trx_id); |
48 | const handlerData = { | ||
49 | handler: setTimeout(request, config.auto_resend.delay_ms, task), | ||
50 | task: task, | 22 | } |
51 | retry: retry | 23 | |
52 | } | 24 | function register(task, request) { |
53 | 25 | if (!task.trx_id) { | |
54 | resendHandlers.set(task.trx_id, handlerData); | 26 | logger.warn('Invalid task on resendDelay') |
55 | } | 27 | return; |
56 | 28 | } | |
57 | exports.cancel = cancel; | 29 | |
58 | exports.register = register; | 30 | if (!request || !config || !config.auto_resend || !Number(config.auto_resend.delay_ms) || !Number(config.auto_resend.max_retry)) { |
package.json
1 | { | 1 | { |
2 | "name": "komodo-sdk", | 2 | "name": "komodo-sdk", |
3 | "version": "1.20.3", | 3 | "version": "1.21.0", |
4 | "description": "SDK for Komodo", | 4 | "description": "SDK for Komodo", |
5 | "main": "index.js", | 5 | "main": "index.js", |
6 | "scripts": { | 6 | "scripts": { |
7 | "test": "mocha", | 7 | "test": "mocha", |
8 | "postversion": "git push && git push --tags" | 8 | "postversion": "git push && git push --tags" |
9 | }, | 9 | }, |
10 | "repository": { | 10 | "repository": { |
11 | "type": "git", | 11 | "type": "git", |
12 | "url": "git@gitlab.kodesumber.com:komodo/komodo-sdk.git" | 12 | "url": "git@gitlab.kodesumber.com:komodo/komodo-sdk.git" |
13 | }, | 13 | }, |
14 | "keywords": [ | 14 | "keywords": [ |
15 | "ppob", | 15 | "ppob", |
16 | "payment", | 16 | "payment", |
17 | "komodo" | 17 | "komodo" |
18 | ], | 18 | ], |
19 | "author": "Adhidarma Hadiwinoto <gua@adhisimon.org>", | 19 | "author": "Adhidarma Hadiwinoto <gua@adhisimon.org>", |
20 | "license": "ISC", | 20 | "license": "ISC", |
21 | "dependencies": { | 21 | "dependencies": { |
22 | "basic-auth": "^2.0.0", | 22 | "basic-auth": "^2.0.0", |
23 | "body-parser": "^1.18.2", | 23 | "body-parser": "^1.18.2", |
24 | "express": "^4.16.3", | 24 | "express": "^4.16.3", |
25 | "express-session": "^1.15.6", | 25 | "express-session": "^1.15.6", |
26 | "lru-cache": "^4.1.1", | 26 | "lru-cache": "^4.1.1", |
27 | "macaddress": "^0.2.8", | 27 | "macaddress": "^0.2.8", |
28 | "moment": "^2.19.1", | 28 | "moment": "^2.19.1", |
29 | "node-machine-id": "^1.1.10", | 29 | "node-machine-id": "^1.1.10", |
30 | "node-natural-sort": "^0.8.6", | 30 | "node-natural-sort": "^0.8.6", |
31 | "numeral": "^2.0.6", | 31 | "numeral": "^2.0.6", |
32 | "nunjucks": "^3.0.1", | 32 | "nunjucks": "^3.0.1", |
33 | "redis": "^2.8.0", | 33 | "redis": "^2.8.0", |
34 | "request": "^2.81.0", | 34 | "request": "^2.81.0", |
35 | "sha1": "^1.1.1", | 35 | "sha1": "^1.1.1", |
36 | "simple-git": "^1.80.1", | 36 | "simple-git": "^1.80.1", |
37 | "strftime": "^0.10.0", | 37 | "strftime": "^0.10.0", |
38 | "uniqid": "^4.1.1", | 38 | "uniqid": "^4.1.1", |
39 | "uuid": "^3.1.0", | 39 | "uuid": "^3.1.0", |
40 | "winston": "^2.3.1", | 40 | "winston": "^2.3.1", |
41 | "winston-circular-buffer": "^1.0.0", | 41 | "winston-circular-buffer": "^1.0.0", |
42 | "winston-daily-rotate-file": "^1.4.6" | 42 | "winston-daily-rotate-file": "^1.4.6" |
43 | } | 43 | } |
44 | } | 44 | } |
45 | 45 |