Skip to content

Commit 21866d3

Browse files
author
Karl Ranna
authored
Merge pull request #123 from scorebreaker/memory-leak
fix: cleanup getXudClient$
2 parents fb4643a + c047526 commit 21866d3

File tree

3 files changed

+21
-17
lines changed

3 files changed

+21
-17
lines changed

src/opendex/xud/client.spec.ts

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,23 +2,30 @@ import { XudClient } from '../../proto/xudrpc_grpc_pb';
22
import { testConfig } from '../../test-utils';
33
import { errors } from '../errors';
44
import { getXudClient$ } from './client';
5+
import { take } from 'rxjs/operators';
56

67
jest.mock('../../proto/xudrpc_grpc_pb');
78
jest.mock('../../proto/xudrpc_pb');
89

910
describe('getXudClient$', () => {
1011
test('success', done => {
11-
expect.assertions(2);
12+
expect.assertions(3);
1213
const config = testConfig();
1314
const xudClient$ = getXudClient$(config);
14-
xudClient$.subscribe(() => {
15-
expect(XudClient).toHaveBeenCalledTimes(1);
16-
expect(XudClient).toHaveBeenCalledWith(
17-
`${config.OPENDEX_RPC_HOST}:${config.OPENDEX_RPC_PORT}`,
18-
expect.any(Object),
19-
expect.any(Object)
20-
);
21-
done();
15+
16+
xudClient$.pipe(take(1)).subscribe({
17+
next: client => {
18+
expect(XudClient).toHaveBeenCalledTimes(1);
19+
expect(XudClient).toHaveBeenCalledWith(
20+
`${config.OPENDEX_RPC_HOST}:${config.OPENDEX_RPC_PORT}`,
21+
expect.any(Object),
22+
expect.any(Object)
23+
);
24+
setImmediate(() => {
25+
expect(client.close).toHaveBeenCalledTimes(1);
26+
done();
27+
});
28+
},
2229
});
2330
});
2431

src/opendex/xud/client.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,9 @@ const getXudClient$ = (config: Config): Observable<XudClient> => {
2020
options
2121
);
2222
subscriber.next(client);
23-
subscriber.complete();
23+
return () => {
24+
client.close();
25+
};
2426
}).pipe(
2527
catchError(error => {
2628
if (error.code === 'ENOENT') {

src/utils.ts

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import moment from 'moment';
2-
import { Observable, Subject, timer, merge } from 'rxjs';
2+
import { Observable, Subject } from 'rxjs';
33
import { take, tap } from 'rxjs/operators';
44
import { curry } from 'ramda';
55

@@ -24,12 +24,7 @@ export const getStartShutdown$ = (): Observable<unknown> => {
2424
const shutdown$ = new Subject();
2525
process.on('SIGINT', () => shutdown$.next());
2626
process.on('SIGTERM', () => shutdown$.next());
27-
const ONE_MINUTE = 1000 * 60;
28-
const ONE_HOUR = ONE_MINUTE * 60;
29-
const restart$ = timer(ONE_HOUR).pipe(
30-
tap(() => console.log('Restarting Arby to reduce memory usage.'))
31-
);
32-
return merge(shutdown$.asObservable(), restart$).pipe(take(1));
27+
return shutdown$.asObservable().pipe(take(1));
3328
};
3429

3530
const debugObservable = (prefix: string, source: Observable<any>) => {

0 commit comments

Comments
 (0)