@icgio/icg-utils 1.9.35 → 1.9.37

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.
@@ -3,25 +3,67 @@ class RateLimiter {
3
3
  this.max_concurrent = settings.max_concurrent ? settings.max_concurrent : null
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
+ this.only_same_args = settings.only_same_args ? settings.only_same_args : false
6
7
  this.count = 0
8
+ this.keyToState = new Map()
7
9
  }
8
10
  process(func, ...args) {
9
11
  if (!this.max_concurrent || !this.interval_concurrent) {
10
12
  func(...args)
11
13
  return
12
14
  }
13
- if (this.count === 0) {
14
- setTimeout(() => {
15
- this.count = 0
16
- }, this.interval_concurrent)
15
+ if (this.only_same_args) {
16
+ const key = this._serializeArgs(args)
17
+ let state = this.keyToState.get(key)
18
+ if (!state) {
19
+ state = { count: 0, timerId: null }
20
+ this.keyToState.set(key, state)
21
+ }
22
+ if (state.count === 0) {
23
+ state.timerId = setTimeout(() => {
24
+ state.count = 0
25
+ state.timerId = null
26
+ // cleanup to avoid unbounded growth
27
+ this.keyToState.delete(key)
28
+ }, this.interval_concurrent)
29
+ }
30
+ if (state.count < this.max_concurrent) {
31
+ state.count++
32
+ func(...args)
33
+ } else if (!this.abandon_tx) {
34
+ setTimeout(() => {
35
+ this.process(func, ...args)
36
+ }, this.interval_concurrent)
37
+ }
38
+ } else {
39
+ if (this.count === 0) {
40
+ setTimeout(() => {
41
+ this.count = 0
42
+ }, this.interval_concurrent)
43
+ }
44
+ if (this.count < this.max_concurrent) {
45
+ this.count++
46
+ func(...args)
47
+ } else if (!this.abandon_tx) {
48
+ setTimeout(() => {
49
+ this.process(func, ...args)
50
+ }, this.interval_concurrent)
51
+ }
17
52
  }
18
- if (this.count < this.max_concurrent) {
19
- this.count++
20
- func(...args)
21
- } else if (!this.abandon_tx) {
22
- setTimeout(() => {
23
- this.process(func, ...args)
24
- }, this.interval_concurrent)
53
+ }
54
+ _serializeArgs(args) {
55
+ try {
56
+ return JSON.stringify(args)
57
+ } catch (e) {
58
+ return args
59
+ .map((a) => {
60
+ try {
61
+ return JSON.stringify(a)
62
+ } catch (_) {
63
+ return String(a)
64
+ }
65
+ })
66
+ .join('|')
25
67
  }
26
68
  }
27
69
  }
package/lib/utils.js CHANGED
@@ -828,13 +828,31 @@ function number_with_commas(x) {
828
828
 
829
829
  function exchange_cur_quote_cur_converter(exchange_cur_quote_cur) {
830
830
  let exchange_pairs = {},
831
- exchange_curs = {}
831
+ exchange_curs = {},
832
+ exchange_cur_pairs = {}
832
833
  for (let exchange in exchange_cur_quote_cur) {
833
834
  _.set(exchange_pairs, [exchange], [])
834
835
  _.set(exchange_curs, [exchange], [])
836
+ _.set(exchange_cur_pairs, [exchange], {})
835
837
  for (let quote_cur in exchange_cur_quote_cur[exchange]) {
836
- exchange_pairs[exchange] = exchange_pairs[exchange].concat(_.map(exchange_cur_quote_cur[exchange][quote_cur], (cur) => cur + quote_cur))
838
+ const pairs_for_quote_cur = _.map(exchange_cur_quote_cur[exchange][quote_cur], (cur) => cur + quote_cur)
839
+ exchange_pairs[exchange] = exchange_pairs[exchange].concat(pairs_for_quote_cur)
837
840
  exchange_curs[exchange] = _.union(exchange_curs[exchange], _.union(exchange_cur_quote_cur[exchange][quote_cur], [quote_cur]))
841
+
842
+ // Build exchange_cur_pairs: map each currency to pairs containing it
843
+ for (let pair of pairs_for_quote_cur) {
844
+ const [cur] = parse_pair(pair)
845
+ // Add pair to cur (base currency)
846
+ if (!exchange_cur_pairs[exchange][cur]) {
847
+ exchange_cur_pairs[exchange][cur] = []
848
+ }
849
+ exchange_cur_pairs[exchange][cur].push(pair)
850
+ // Add pair to quote_cur
851
+ if (!exchange_cur_pairs[exchange][quote_cur]) {
852
+ exchange_cur_pairs[exchange][quote_cur] = []
853
+ }
854
+ exchange_cur_pairs[exchange][quote_cur].push(pair)
855
+ }
838
856
  }
839
857
  }
840
858
  let pair_exchanges = {}
@@ -885,6 +903,7 @@ function exchange_cur_quote_cur_converter(exchange_cur_quote_cur) {
885
903
  exchange_pairs_without_affiliates,
886
904
  exchange_curs,
887
905
  exchange_curs_without_affiliates,
906
+ exchange_cur_pairs,
888
907
  pair_exchanges,
889
908
  pair_exchanges_without_affiliates,
890
909
  exchange_affiliates,
@@ -956,5 +975,5 @@ module.exports = {
956
975
  number_with_commas,
957
976
  exchange_cur_quote_cur_converter,
958
977
  sort_keys,
959
- convert_deposit_withdrawal_chain
978
+ convert_deposit_withdrawal_chain,
960
979
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@icgio/icg-utils",
3
- "version": "1.9.35",
3
+ "version": "1.9.37",
4
4
  "description": "Utils package.",
5
5
  "main": "index.js",
6
6
  "directories": {