Skip to content

Commit dc68b9f

Browse files
Tucskyadeacetis
authored andcommitted
remove trade queue
1 parent dbf57b8 commit dc68b9f

File tree

4 files changed

+27
-405
lines changed

4 files changed

+27
-405
lines changed

src/config.js

Lines changed: 3 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -85,27 +85,11 @@ const defaultConfig = {
8585
// bypass origin restriction for given ips (comma separated)
8686
whitelist: [],
8787

88-
// enable websocket server (if you only use this for storing trade data set to false)
89-
broadcast: false,
90-
91-
// separate the broadcasts by n ms (0 = broadcast instantly)
92-
broadcastDebounce: 0,
93-
94-
// aggregate trades that came within same millisecond before broadcast
95-
// (note) saving to storage is NOT impacted
96-
// (warning) will add +50ms delay for confirmation that trade actually came on same ms
97-
broadcastAggr: true,
98-
99-
// will only broadcast trades >= broadcastThreshold
100-
// expressed in base currency (ex: BTC)
101-
// default 0
102-
broadcastThreshold: 0,
103-
10488
// enable api (historical/{from in ms}/{to in ms}/{timesfame in ms}/{markets separated by +})
10589
api: true,
10690

10791
// storage solution, either
108-
// false | null (no storage, everything is wiped out after broadcast)
92+
// false | null (no storage)
10993
// "files" (periodical text file),
11094
// "influx" (timeserie database),
11195

@@ -359,12 +343,6 @@ if (config.exchanges && typeof config.exchanges === 'string') {
359343
.filter(a => a.length)
360344
}
361345

362-
if (!config.api && config.broadcast) {
363-
console.warn(
364-
`[warning!] websocket is enabled but api is set to ${config.api}\n\t(ws server require an http server for the initial upgrade handshake)`
365-
)
366-
}
367-
368346
if (!config.storage && config.collect) {
369347
console.warn(
370348
`[warning!] server will not persist any of the data it is receiving`
@@ -375,21 +353,9 @@ if (!config.collect && !config.api) {
375353
console.warn(`[warning!] server has no purpose`)
376354
}
377355

378-
if (!config.storage && !config.collect && (config.broadcast || config.api)) {
379-
console.warn(
380-
`[warning!] ${
381-
config.broadcast && config.api
382-
? 'ws and api are'
383-
: config.broadcast
384-
? 'ws is'
385-
: 'api is'
386-
} enabled but neither storage or collect is enabled (may be useless)`
387-
)
388-
}
389-
390-
if (config.broadcast && !config.collect) {
356+
if (!config.storage && !config.collect && config.api) {
391357
console.warn(
392-
`[warning!] collect is disabled but broadcast is set to ${config.broadcast} (may be useless)`
358+
`[warning!] api is enabled but neither storage or collect is enabled (may be useless)`
393359
)
394360
}
395361

src/exchange.js

Lines changed: 20 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -73,23 +73,11 @@ class Exchange extends EventEmitter {
7373
*/
7474
this.maxConnectionsPerApi = 50
7575

76-
/**
77-
* Define if the incoming trades should be queued
78-
* @type {boolean}
79-
*/
80-
this.shouldQueueTrades = false
81-
8276
/**
8377
* Pending recovery ranges
8478
* @type {{pair: string, from: number, to: number}[]}
8579
*/
8680
this.recoveryRanges = []
87-
88-
/**
89-
* Trades goes theres while we wait for historical response
90-
* @type {Trade[]}
91-
*/
92-
this.queuedTrades = []
9381
}
9482

9583
/**
@@ -262,7 +250,7 @@ class Exchange extends EventEmitter {
262250
return
263251
}
264252

265-
if (!/ping|pong/.test(data)) {
253+
if (!/ping|pong/i.test(data)) {
266254
console.debug(
267255
`[${this.id}.createWs] sending ${data.substr(0, 64)}${
268256
data.length > 64 ? '...' : ''
@@ -547,12 +535,24 @@ class Exchange extends EventEmitter {
547535
this.recoveryRanges.push(range)
548536

549537
if (!recovering[this.id]) {
538+
console.log(`[${this.id}.registerRangeForRecovery] exchange isn't recovering yet -> start recovering`)
550539
this.recoverNextRange()
551540
}
552541
}
553542

554543
async recoverNextRange(sequencial) {
555544
if (!this.recoveryRanges.length || (recovering[this.id] && !sequencial)) {
545+
if (recovering[this.id] && !sequencial) {
546+
console.log(`[${this.id}] attempted to start manual recovery while already recovering`)
547+
}
548+
if (!this.recoveryRanges.length) {
549+
console.log(`[${this.id}] no more range to recover`)
550+
if (sequencial) {
551+
console.log(`[${this.id}] set recovering[this.id] to false`)
552+
delete recovering[this.id]
553+
554+
}
555+
}
556556
return
557557
}
558558

@@ -609,7 +609,7 @@ class Exchange extends EventEmitter {
609609
+range.to
610610
).toISOString()})`
611611
}
612-
connection.timestamp = range.to
612+
connection.timestamp = Math.max(connection.timestamp, range.to)
613613
}
614614

615615
// in rare case of slow recovery and fast reconnection happening, propagate to pending ranges for that pair
@@ -637,31 +637,9 @@ class Exchange extends EventEmitter {
637637
}
638638

639639
if (!this.recoveryRanges.length) {
640-
console.log(`[${this.id}] no more ranges to recover`)
640+
console.log(`[${this.id}] no more ranges to recover (delete recovering[this.id])`)
641641

642642
delete recovering[this.id]
643-
644-
if (this.queuedTrades.length) {
645-
const sortedQueuedTrades = this.queuedTrades.sort(
646-
(a, b) => a.timestamp - b.timestamp
647-
)
648-
649-
console.log(
650-
`[${this.id}] release trades queue (${
651-
sortedQueuedTrades.length
652-
} trades, ${new Date(+sortedQueuedTrades[0].timestamp)
653-
.toISOString()
654-
.split('T')
655-
.pop()} to ${new Date(
656-
+sortedQueuedTrades[sortedQueuedTrades.length - 1].timestamp
657-
)
658-
.toISOString()
659-
.split('T')
660-
.pop()})`
661-
)
662-
this.emit('trades', sortedQueuedTrades)
663-
this.queuedTrades = []
664-
}
665643
} else {
666644
return this.waitBeforeContinueRecovery().then(() =>
667645
this.recoverNextRange(true)
@@ -830,8 +808,6 @@ class Exchange extends EventEmitter {
830808
* @param {string[]} pairs pairs attached to ws at opening
831809
*/
832810
async onOpen(event, api) {
833-
this.queueNextTrades()
834-
835811
const pairs = [...api._pending, ...api._connected]
836812

837813
console.debug(
@@ -956,11 +932,11 @@ class Exchange extends EventEmitter {
956932
* @param {Trade[]} trades
957933
*/
958934
emitTrades(source, trades) {
959-
if (source && this.promisesOfApiReconnections[source]) {
960-
return
935+
if (!trades || !trades.length) {
936+
return null
961937
}
962938

963-
this.queueControl(source, trades, 'trades')
939+
this.emit('trades', trades, source)
964940

965941
return true
966942
}
@@ -971,26 +947,13 @@ class Exchange extends EventEmitter {
971947
* @param {Trade[]} trades
972948
*/
973949
emitLiquidations(source, trades) {
974-
if (source && this.promisesOfApiReconnections[source]) {
975-
return
976-
}
977-
978-
this.queueControl(source, trades, 'liquidations')
979-
980-
return true
981-
}
982-
983-
queueControl(source, trades, type) {
984950
if (!trades || !trades.length) {
985951
return null
986952
}
987953

988-
if (this.shouldQueueTrades || recovering[this.id]) {
989-
Array.prototype.push.apply(this.queuedTrades, trades)
990-
return null
991-
}
954+
this.emit('liquidations', trades, source)
992955

993-
this.emit(type, trades, source)
956+
return true
994957
}
995958

996959
startKeepAlive(api, payload = { event: 'ping' }, every = 30000) {
@@ -1110,20 +1073,6 @@ class Exchange extends EventEmitter {
11101073
return true
11111074
}
11121075

1113-
queueNextTrades(duration = 100) {
1114-
this.shouldQueueTrades = true
1115-
1116-
if (this._unlockTimeout) {
1117-
clearTimeout(this._unlockTimeout)
1118-
}
1119-
1120-
this._unlockTimeout = setTimeout(() => {
1121-
this._unlockTimeout = null
1122-
1123-
this.shouldQueueTrades = false
1124-
}, duration)
1125-
}
1126-
11271076
/**
11281077
* Wait between requests to prevent 429 HTTP errors
11291078
* @returns {Promise<void>}

src/exchanges/binance.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ class Binance extends Exchange {
88

99
this.id = 'BINANCE'
1010
this.lastSubscriptionId = 0
11+
this.maxConnectionsPerApi = 16
1112
this.subscriptions = {}
1213

1314
this.endpoints = {

0 commit comments

Comments
 (0)