Skip to content
This repository was archived by the owner on Jul 17, 2021. It is now read-only.

Commit d696d68

Browse files
committed
Fix CCXT controller memory leak
1 parent 60197fd commit d696d68

File tree

3 files changed

+28
-10
lines changed

3 files changed

+28
-10
lines changed

src/__test__/ccxt_conctroller.test.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,10 @@ describe('CCXT Controller Test', () => {
88
// Add Binance exhcange
99
test('Add valid exchange', async () => {
1010
const exchange = CCXT_API.initNewExchanges('binance');
11+
CCXT_API.initNewExchanges('binance');
12+
CCXT_API.initNewExchanges('binance');
1113

14+
expect(CCXT_API.exchanges).toHaveLength(1);
1215
expect(exchange.api.tokenBucket).toBeDefined();
1316
});
1417

src/exchange/ccxt_controller.ts

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,13 @@ import _ from 'lodash';
33
import * as ccxt from 'ccxt';
44
import { logger } from '../logger';
55

6+
type CcxtInstance = {
7+
exchangeName: string;
8+
api: any;
9+
};
10+
611
class ExchangeAPI {
7-
exchanges: any[];
12+
exchanges: CcxtInstance[];
813
constructor() {
914
this.exchanges = [];
1015
}
@@ -61,20 +66,28 @@ class ExchangeAPI {
6166
}
6267

6368
/* CCXT API STUFF */
69+
_isExchangeLoaded(exchange: string): boolean {
70+
const exchangeName = exchange.toLowerCase();
71+
72+
if (this.exchanges.find((e) => e.exchangeName === exchangeName)) {
73+
return true;
74+
}
75+
76+
return false;
77+
}
6478

6579
loadExchangeAPI(exchange: string): any {
6680
try {
6781
const exchangeName = exchange.toLowerCase();
6882

6983
// Check if CCXT API already loaded
70-
let exchangeData = this.exchanges.find((e) => e.exchange === exchangeName);
84+
const exchangeData = this.exchanges.find((e) => e.exchangeName === exchangeName);
7185

72-
// CCTX API load from buffer or add to the buffer
73-
if (!exchangeData) {
74-
exchangeData = this.initNewExchanges(exchangeName);
86+
if (exchangeData?.api) {
87+
return exchangeData.api;
7588
}
7689

77-
return exchangeData.api;
90+
return this.initNewExchanges(exchangeName).api;
7891
} catch (e) {
7992
logger.error('CCXT load API error ', e);
8093
}
@@ -86,7 +99,9 @@ class ExchangeAPI {
8699
if (_.isObject(ccxt[exchangeName])) {
87100
const api = new ccxt[exchangeName]();
88101

89-
this.exchanges.push({ exchangeName, api });
102+
if (!this._isExchangeLoaded(exchange)) {
103+
this.exchanges.push({ exchangeName, api });
104+
}
90105

91106
return { exchangeName, api };
92107
}

src/pricetickers/index.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,11 @@ class PriceTickers {
3030
}
3131

3232
if (updatePromises.length > 0) {
33-
logger.verbose('Marketdata Update loop');
33+
logger.verbose('PriceTickers Update loop');
3434
await Promise.all(updatePromises);
3535
}
3636
} catch (e) {
37-
logger.error('Marketdata Update ', e);
37+
logger.error('PriceTickers Update loop', e);
3838
} finally {
3939
setTimeout(() => {
4040
this.updateLoop();
@@ -80,7 +80,7 @@ class PriceTickers {
8080

8181
return;
8282
} catch (e) {
83-
logger.error('Update_tradepairs ', e);
83+
logger.error('PriceTickers Update ', e);
8484
}
8585
}
8686

0 commit comments

Comments
 (0)