Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(rpc): close streaming calls on shutdown #717

Merged
merged 1 commit into from
Dec 2, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions lib/grpc/GrpcServer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import { exists, readFile, writeFile } from '../utils/fsUtils';

class GrpcServer {
private server: Server;
private grpcService: GrpcService;

constructor(private logger: Logger, service: Service) {
this.server = new grpc.Server();
Expand Down Expand Up @@ -45,6 +46,8 @@ class GrpcServer {
this.server.addService(HashResolverService, {
resolveHash: grpcService.resolveHash,
});

this.grpcService = grpcService;
}

/**
Expand Down Expand Up @@ -90,6 +93,7 @@ class GrpcServer {
* Stop listening for requests
*/
public close = (): Promise<void> => {
this.grpcService.closeStreams();
return new Promise((resolve) => {
this.server.tryShutdown(() => {
this.logger.info('GRPC server completed shutdown');
Expand Down
22 changes: 22 additions & 0 deletions lib/grpc/GrpcService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,9 @@ const createPlaceOrderEvent = (e: PlaceOrderEvent) => {

/** Class containing the available RPC methods for XUD */
class GrpcService {
/** The set of active streaming calls. */
private streams: Set<grpc.ServerWriteableStream<any>> = new Set<grpc.ServerWriteableStream<any>>();

/** Create an instance of available RPC methods and bind all exposed functions. */
constructor(private logger: Logger, private service: Service) {}

Expand Down Expand Up @@ -152,6 +155,22 @@ class GrpcService {
return grpcError;
}

/** Closes and removes all active streaming calls. */
public closeStreams = () => {
this.streams.forEach((stream) => {
stream.end();
sangaman marked this conversation as resolved.
Show resolved Hide resolved
});
this.streams.clear();
}

/** Adds an active streaming call and adds a listener to remove it if it is cancelled. */
private addStream = (stream: grpc.ServerWriteableStream<any>) => {
this.streams.add(stream);
stream.once('cancelled', () => {
this.streams.delete(stream);
});
}

/**
* See [[Service.addCurrency]]
*/
Expand Down Expand Up @@ -508,6 +527,7 @@ class GrpcService {
this.service.subscribeAddedOrders((order: Order) => {
call.write(createOrder(order));
});
this.addStream(call);
}

/*
Expand All @@ -523,6 +543,7 @@ class GrpcService {
orderRemoval.setIsOwnOrder(order.localId !== undefined);
call.write(orderRemoval);
});
this.addStream(call);
}

/*
Expand All @@ -532,6 +553,7 @@ class GrpcService {
this.service.subscribeSwaps((result: SwapResult) => {
call.write(createSwapResult(result));
});
this.addStream(call);
}
}

Expand Down