Skip to content

Commit

Permalink
feat: gRPC to dump heap (#506)
Browse files Browse the repository at this point in the history
  • Loading branch information
michael1011 committed Mar 17, 2024
1 parent 6d3a1fc commit 283d650
Show file tree
Hide file tree
Showing 11 changed files with 454 additions and 4 deletions.
2 changes: 1 addition & 1 deletion lib/Boltz.ts
Expand Up @@ -150,7 +150,7 @@ class Boltz {
this.grpcServer = new GrpcServer(
this.logger,
this.config.grpc,
new GrpcService(this.service),
new GrpcService(this.logger, this.service),
);

this.prometheus = new Prometheus(
Expand Down
15 changes: 15 additions & 0 deletions lib/HeapDump.ts
@@ -0,0 +1,15 @@
import fs from 'fs';
import { getHeapSnapshot } from 'v8';

export const dumpHeap = async (filePath: string) =>
new Promise<void>((resolve, reject) => {
const snapshot = getHeapSnapshot();
const fileStream = fs.createWriteStream(filePath);
snapshot.pipe(fileStream);

fileStream.on('finish', () => {
fileStream.end();
resolve();
});
fileStream.on('error', (err) => reject(err));
});
21 changes: 21 additions & 0 deletions lib/cli/commands/DevHeapDump.ts
@@ -0,0 +1,21 @@
import { Arguments } from 'yargs';
import { DevHeapDumpRequest } from '../../proto/boltzrpc_pb';
import { callback, loadBoltzClient } from '../Command';

export const command = 'dev-heapdump [path]';

export const describe = 'dumps the heap of the daemon into a file';

export const builder = {
path: {
type: 'string',
describe: 'path to which the heap should be dumped',
},
};

export const handler = (argv: Arguments<any>): void => {
const request = new DevHeapDumpRequest();
request.setPath(argv.path);

loadBoltzClient(argv).devHeapDump(request, callback());
};
1 change: 1 addition & 0 deletions lib/grpc/GrpcServer.ts
Expand Up @@ -29,6 +29,7 @@ class GrpcServer {
sweepSwaps: grpcService.sweepSwaps,
rescan: grpcService.rescan,
setSwapStatus: grpcService.setSwapStatus,
devHeapDump: grpcService.devHeapDump,
});
}

Expand Down
24 changes: 22 additions & 2 deletions lib/grpc/GrpcService.ts
@@ -1,13 +1,18 @@
import { handleUnaryCall } from '@grpc/grpc-js';
import { Transaction as TransactionLiquid } from 'liquidjs-lib';
import { parseTransaction } from '../Core';
import { getHexString } from '../Utils';
import { dumpHeap } from '../HeapDump';
import Logger from '../Logger';
import { getHexString, getUnixTime } from '../Utils';
import { CurrencyType } from '../consts/Enums';
import * as boltzrpc from '../proto/boltzrpc_pb';
import Service from '../service/Service';

class GrpcService {
constructor(private service: Service) {}
constructor(
private readonly logger: Logger,
private readonly service: Service,
) {}

public getInfo: handleUnaryCall<
boltzrpc.GetInfoRequest,
Expand Down Expand Up @@ -233,6 +238,21 @@ class GrpcService {
});
};

public devHeapDump: handleUnaryCall<
boltzrpc.DevHeapDumpRequest,
boltzrpc.DevHeapDumpResponse
> = async (call, callback) => {
await this.handleCallback(call, callback, async () => {
const filePath =
call.request.getPath() || `${getUnixTime()}.heapsnapshot`;

this.logger.verbose(`Dumping heap at: ${filePath}`);
await dumpHeap(filePath);

return new boltzrpc.DevHeapDumpResponse();
});
};

private handleCallback = async <R, T>(
call: R,
callback: (error: any, res: T | null) => void,
Expand Down
17 changes: 17 additions & 0 deletions lib/proto/boltzrpc_grpc_pb.d.ts

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

33 changes: 33 additions & 0 deletions lib/proto/boltzrpc_grpc_pb.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

40 changes: 40 additions & 0 deletions lib/proto/boltzrpc_pb.d.ts

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 283d650

Please sign in to comment.