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

Commit

Permalink
Fix CCXT controller memory leak
Browse files Browse the repository at this point in the history
  • Loading branch information
valamidev committed Apr 12, 2020
1 parent 60197fd commit d696d68
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 10 deletions.
3 changes: 3 additions & 0 deletions src/__test__/ccxt_conctroller.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,10 @@ describe('CCXT Controller Test', () => {
// Add Binance exhcange
test('Add valid exchange', async () => {
const exchange = CCXT_API.initNewExchanges('binance');
CCXT_API.initNewExchanges('binance');
CCXT_API.initNewExchanges('binance');

expect(CCXT_API.exchanges).toHaveLength(1);
expect(exchange.api.tokenBucket).toBeDefined();
});

Expand Down
29 changes: 22 additions & 7 deletions src/exchange/ccxt_controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,13 @@ import _ from 'lodash';
import * as ccxt from 'ccxt';
import { logger } from '../logger';

type CcxtInstance = {
exchangeName: string;
api: any;
};

class ExchangeAPI {
exchanges: any[];
exchanges: CcxtInstance[];
constructor() {
this.exchanges = [];
}
Expand Down Expand Up @@ -61,20 +66,28 @@ class ExchangeAPI {
}

/* CCXT API STUFF */
_isExchangeLoaded(exchange: string): boolean {
const exchangeName = exchange.toLowerCase();

if (this.exchanges.find((e) => e.exchangeName === exchangeName)) {
return true;
}

return false;
}

loadExchangeAPI(exchange: string): any {
try {
const exchangeName = exchange.toLowerCase();

// Check if CCXT API already loaded
let exchangeData = this.exchanges.find((e) => e.exchange === exchangeName);
const exchangeData = this.exchanges.find((e) => e.exchangeName === exchangeName);

// CCTX API load from buffer or add to the buffer
if (!exchangeData) {
exchangeData = this.initNewExchanges(exchangeName);
if (exchangeData?.api) {
return exchangeData.api;
}

return exchangeData.api;
return this.initNewExchanges(exchangeName).api;
} catch (e) {
logger.error('CCXT load API error ', e);
}
Expand All @@ -86,7 +99,9 @@ class ExchangeAPI {
if (_.isObject(ccxt[exchangeName])) {
const api = new ccxt[exchangeName]();

this.exchanges.push({ exchangeName, api });
if (!this._isExchangeLoaded(exchange)) {
this.exchanges.push({ exchangeName, api });
}

return { exchangeName, api };
}
Expand Down
6 changes: 3 additions & 3 deletions src/pricetickers/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,11 @@ class PriceTickers {
}

if (updatePromises.length > 0) {
logger.verbose('Marketdata Update loop');
logger.verbose('PriceTickers Update loop');
await Promise.all(updatePromises);
}
} catch (e) {
logger.error('Marketdata Update ', e);
logger.error('PriceTickers Update loop', e);
} finally {
setTimeout(() => {
this.updateLoop();
Expand Down Expand Up @@ -80,7 +80,7 @@ class PriceTickers {

return;
} catch (e) {
logger.error('Update_tradepairs ', e);
logger.error('PriceTickers Update ', e);
}
}

Expand Down

0 comments on commit d696d68

Please sign in to comment.