@icgio/icg-utils 1.9.51 → 1.9.52
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/lib/rate-limiter.js
CHANGED
|
@@ -4,10 +4,18 @@ class RateLimiter {
|
|
|
4
4
|
this.interval_concurrent = settings.interval_concurrent ? settings.interval_concurrent : null
|
|
5
5
|
this.abandon_tx = settings.abandon_tx ? settings.abandon_tx : false
|
|
6
6
|
this.only_same_args = settings.only_same_args ? settings.only_same_args : false
|
|
7
|
+
this.priority = null
|
|
8
|
+
this.queue = []
|
|
9
|
+
this._resetTimer = null
|
|
7
10
|
this.count = 0
|
|
8
11
|
this.keyToState = new Map()
|
|
9
12
|
}
|
|
10
13
|
process(func, ...args) {
|
|
14
|
+
let priority = Infinity
|
|
15
|
+
if (!this.only_same_args) {
|
|
16
|
+
priority = this.priority != null ? this.priority : Infinity
|
|
17
|
+
this.priority = null
|
|
18
|
+
}
|
|
11
19
|
if (!this.max_concurrent || !this.interval_concurrent) {
|
|
12
20
|
func(...args)
|
|
13
21
|
return
|
|
@@ -36,21 +44,48 @@ class RateLimiter {
|
|
|
36
44
|
}, this.interval_concurrent)
|
|
37
45
|
}
|
|
38
46
|
} else {
|
|
39
|
-
if (this.count === 0) {
|
|
40
|
-
setTimeout(() => {
|
|
41
|
-
this.count = 0
|
|
42
|
-
}, this.interval_concurrent)
|
|
43
|
-
}
|
|
44
47
|
if (this.count < this.max_concurrent) {
|
|
45
48
|
this.count++
|
|
49
|
+
this._scheduleResetTimer()
|
|
46
50
|
func(...args)
|
|
47
51
|
} else if (!this.abandon_tx) {
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
52
|
+
this._enqueue({ func, args, priority })
|
|
53
|
+
if (!this._resetTimer) {
|
|
54
|
+
if (this.count === 0) {
|
|
55
|
+
this._drainQueue()
|
|
56
|
+
} else {
|
|
57
|
+
this._scheduleResetTimer()
|
|
58
|
+
}
|
|
59
|
+
}
|
|
51
60
|
}
|
|
52
61
|
}
|
|
53
62
|
}
|
|
63
|
+
_enqueue(item) {
|
|
64
|
+
const index = this.queue.findIndex((queuedItem) => queuedItem.priority > item.priority)
|
|
65
|
+
if (index === -1) {
|
|
66
|
+
this.queue.push(item)
|
|
67
|
+
} else {
|
|
68
|
+
this.queue.splice(index, 0, item)
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
_scheduleResetTimer() {
|
|
72
|
+
if (this._resetTimer || this.count === 0) {
|
|
73
|
+
return
|
|
74
|
+
}
|
|
75
|
+
this._resetTimer = setTimeout(() => {
|
|
76
|
+
this.count = 0
|
|
77
|
+
this._resetTimer = null
|
|
78
|
+
this._drainQueue()
|
|
79
|
+
}, this.interval_concurrent)
|
|
80
|
+
}
|
|
81
|
+
_drainQueue() {
|
|
82
|
+
while (this.count < this.max_concurrent && this.queue.length) {
|
|
83
|
+
const { func, args } = this.queue.shift()
|
|
84
|
+
this.count++
|
|
85
|
+
func(...args)
|
|
86
|
+
}
|
|
87
|
+
this._scheduleResetTimer()
|
|
88
|
+
}
|
|
54
89
|
_serializeArgs(args) {
|
|
55
90
|
try {
|
|
56
91
|
return JSON.stringify(args)
|
package/package.json
CHANGED
|
@@ -1,13 +1,13 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@icgio/icg-utils",
|
|
3
|
-
"version": "1.9.
|
|
3
|
+
"version": "1.9.52",
|
|
4
4
|
"description": "Utils package.",
|
|
5
5
|
"main": "index.js",
|
|
6
6
|
"directories": {
|
|
7
7
|
"lib": "lib"
|
|
8
8
|
},
|
|
9
9
|
"scripts": {
|
|
10
|
-
"test": "
|
|
10
|
+
"test": "node test/rate-limiter.test.js"
|
|
11
11
|
},
|
|
12
12
|
"repository": {
|
|
13
13
|
"type": "git",
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
These patches target sibling repos that are read-only in this session.
|
|
2
|
+
|
|
3
|
+
Apply them from the repo they target:
|
|
4
|
+
|
|
5
|
+
```bash
|
|
6
|
+
cd /Users/lm/icg-exchanges-wrapper
|
|
7
|
+
git apply /Users/lm/icg-utils/patches/icg-exchanges-wrapper-strategy-priority.patch
|
|
8
|
+
|
|
9
|
+
cd /Users/lm/icg-market-maker
|
|
10
|
+
git apply /Users/lm/icg-utils/patches/icg-market-maker-dependency-bump.patch
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
After publishing `@icgio/icg-utils@1.9.52` and `@icgio/icg-exchanges-wrapper@1.15.36`, regenerate the market maker lockfile with:
|
|
14
|
+
|
|
15
|
+
```bash
|
|
16
|
+
cd /Users/lm/icg-market-maker
|
|
17
|
+
npm install
|
|
18
|
+
```
|
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
diff --git a/middleware/orders.js b/middleware/orders.js
|
|
2
|
+
--- a/middleware/orders.js
|
|
3
|
+
+++ b/middleware/orders.js
|
|
4
|
+
@@ -73,7 +73,23 @@
|
|
5
|
+
const TRADES_DEFAULT_MS = 7 * 24 * 60 * 60 * 1000
|
|
6
|
+
const NO_CALLBACK_TIMEOUT_MS = 2 * 60 * 1000
|
|
7
|
+
const PROCESS_RATES_THROTTLE_MS = 75
|
|
8
|
+
+const ORDER_TYPE_PRIORITY = {
|
|
9
|
+
+ arb: 1,
|
|
10
|
+
+ self: 2,
|
|
11
|
+
+ maker_1: 3,
|
|
12
|
+
+ maker_1_taker: 3,
|
|
13
|
+
+ maker_2: 4,
|
|
14
|
+
+ maker_3: 5,
|
|
15
|
+
+}
|
|
16
|
+
|
|
17
|
+
+const getOrderPriority = (order_type, is_cancel = false) => {
|
|
18
|
+
+ const priority = ORDER_TYPE_PRIORITY[order_type]
|
|
19
|
+
+ if (priority == null) {
|
|
20
|
+
+ return Infinity
|
|
21
|
+
+ }
|
|
22
|
+
+ return is_cancel ? priority - 0.5 : priority
|
|
23
|
+
+}
|
|
24
|
+
+
|
|
25
|
+
module.exports = class Orders {
|
|
26
|
+
constructor(exchange_cur_quote_cur_dict, cd, settings) {
|
|
27
|
+
let { exchange_cur_quote_cur, exchange_cur_quote_cur_rates_only, pair_map_rates_only } = exchange_cur_quote_cur_dict
|
|
28
|
+
@@ -332,11 +348,17 @@
|
|
29
|
+
// }
|
|
30
|
+
}
|
|
31
|
+
if (routes) {
|
|
32
|
+
+ if (this.Exchanges[exchange].private_limiter) {
|
|
33
|
+
+ this.Exchanges[exchange].private_limiter.priority = getOrderPriority(order_type)
|
|
34
|
+
+ }
|
|
35
|
+
this.Exchanges[exchange].limit_trade_with_routes(pair, method, price, amount, routes, cb)
|
|
36
|
+
} else {
|
|
37
|
+
console.error('dex no routes:', rates_with_routes, price)
|
|
38
|
+
}
|
|
39
|
+
} else if (limiter_on) {
|
|
40
|
+
+ if (this.Exchanges[exchange].private_limiter) {
|
|
41
|
+
+ this.Exchanges[exchange].private_limiter.priority = getOrderPriority(order_type)
|
|
42
|
+
+ }
|
|
43
|
+
this.Exchanges[exchange].limit_trade(pair, method, price, amount, cb)
|
|
44
|
+
} else {
|
|
45
|
+
this.Exchanges[exchange].limit_trade_base(pair, method, price, amount, cb)
|
|
46
|
+
@@ -403,6 +425,9 @@
|
|
47
|
+
order.order_type + ' canceling',
|
|
48
|
+
)
|
|
49
|
+
;((order.exchange = exchange), (order.pair = pair))
|
|
50
|
+
+ if (this.Exchanges[exchange].private_limiter) {
|
|
51
|
+
+ this.Exchanges[exchange].private_limiter.priority = getOrderPriority(order.order_type, true)
|
|
52
|
+
+ }
|
|
53
|
+
this.Exchanges[exchange].cancel_order_by_order(order, (res) => {
|
|
54
|
+
if (res.success || (!res.success && res.error === 'order-not-open' && original_status === 'CANCELING')) {
|
|
55
|
+
this.update_order_info(
|
|
56
|
+
diff --git a/package.json b/package.json
|
|
57
|
+
--- a/package.json
|
|
58
|
+
+++ b/package.json
|
|
59
|
+
@@ -1,6 +1,6 @@
|
|
60
|
+
{
|
|
61
|
+
"name": "@icgio/icg-exchanges-wrapper",
|
|
62
|
+
- "version": "1.15.35",
|
|
63
|
+
+ "version": "1.15.36",
|
|
64
|
+
"description": "",
|
|
65
|
+
"main": "index.js",
|
|
66
|
+
"scripts": {
|
|
67
|
+
@@ -18,7 +18,7 @@
|
|
68
|
+
"homepage": "https://github.com/icgio/icg-exchanges-wrapper#readme",
|
|
69
|
+
"dependencies": {
|
|
70
|
+
"@icgio/icg-exchanges": "^1.32.187",
|
|
71
|
+
- "@icgio/icg-utils": "^1.9.51",
|
|
72
|
+
+ "@icgio/icg-utils": "^1.9.52",
|
|
73
|
+
"lodash": "^4.17.23",
|
|
74
|
+
"pg": "^8.17.2"
|
|
75
|
+
}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
diff --git a/package.json b/package.json
|
|
2
|
+
--- a/package.json
|
|
3
|
+
+++ b/package.json
|
|
4
|
+
@@ -3,8 +3,8 @@
|
|
5
|
+
"version": "1.0.0",
|
|
6
|
+
"description": "ICG Market Maker",
|
|
7
|
+
"dependencies": {
|
|
8
|
+
- "@icgio/icg-exchanges-wrapper": "^1.15.35",
|
|
9
|
+
- "@icgio/icg-utils": "^1.9.51",
|
|
10
|
+
+ "@icgio/icg-exchanges-wrapper": "^1.15.36",
|
|
11
|
+
+ "@icgio/icg-utils": "^1.9.52",
|
|
12
|
+
"form-data": "^4.0.5",
|
|
13
|
+
"lodash": "^4.17.23",
|
|
14
|
+
"mailgun.js": "^12.7.0",
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
const assert = require('node:assert/strict')
|
|
2
|
+
const RateLimiter = require('../lib/rate-limiter')
|
|
3
|
+
|
|
4
|
+
const wait = (ms) => new Promise((resolve) => setTimeout(resolve, ms))
|
|
5
|
+
|
|
6
|
+
async function testPriorityDrainOrder() {
|
|
7
|
+
const limiter = new RateLimiter({ max_concurrent: 2, interval_concurrent: 25 })
|
|
8
|
+
const executed = []
|
|
9
|
+
|
|
10
|
+
for (const priority of [5, 4, 3, 2, 1]) {
|
|
11
|
+
limiter.priority = priority
|
|
12
|
+
limiter.process((value) => {
|
|
13
|
+
executed.push(value)
|
|
14
|
+
}, priority)
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
await wait(10)
|
|
18
|
+
assert.deepStrictEqual(executed, [5, 4], 'first window should execute immediately in submission order')
|
|
19
|
+
|
|
20
|
+
await wait(30)
|
|
21
|
+
assert.deepStrictEqual(executed, [5, 4, 1, 2], 'queued work should drain by ascending priority')
|
|
22
|
+
|
|
23
|
+
await wait(30)
|
|
24
|
+
assert.deepStrictEqual(executed, [5, 4, 1, 2, 3], 'remaining queued work should continue draining in priority order')
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
async function testDefaultPriorityStaysFifo() {
|
|
28
|
+
const limiter = new RateLimiter({ max_concurrent: 1, interval_concurrent: 20 })
|
|
29
|
+
const executed = []
|
|
30
|
+
|
|
31
|
+
limiter.process((value) => {
|
|
32
|
+
executed.push(value)
|
|
33
|
+
}, 'first')
|
|
34
|
+
limiter.process((value) => {
|
|
35
|
+
executed.push(value)
|
|
36
|
+
}, 'second')
|
|
37
|
+
limiter.process((value) => {
|
|
38
|
+
executed.push(value)
|
|
39
|
+
}, 'third')
|
|
40
|
+
|
|
41
|
+
await wait(10)
|
|
42
|
+
assert.deepStrictEqual(executed, ['first'], 'first call should execute immediately')
|
|
43
|
+
|
|
44
|
+
await wait(25)
|
|
45
|
+
assert.deepStrictEqual(executed, ['first', 'second'], 'default priority should keep queued calls FIFO')
|
|
46
|
+
|
|
47
|
+
await wait(25)
|
|
48
|
+
assert.deepStrictEqual(executed, ['first', 'second', 'third'], 'later FIFO work should continue in order')
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
async function main() {
|
|
52
|
+
await testPriorityDrainOrder()
|
|
53
|
+
await testDefaultPriorityStaysFifo()
|
|
54
|
+
console.log('rate-limiter tests passed')
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
main().catch((err) => {
|
|
58
|
+
console.error(err)
|
|
59
|
+
process.exit(1)
|
|
60
|
+
})
|