Skip to content

Commit

Permalink
fix: handle server-sent termination in RPC (#432)
Browse files Browse the repository at this point in the history
  • Loading branch information
MikeDombo committed Oct 23, 2023
1 parent 86f1134 commit 54b50ab
Showing 1 changed file with 21 additions and 0 deletions.
21 changes: 21 additions & 0 deletions lib/eventstream_rpc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -785,6 +785,13 @@ class OperationBase extends EventEmitter {
* @return the operation's underlying event stream binding object
*/
getStream() : eventstream.ClientStream { return this.stream; }

/**
* Set this operation state to be "Ended" so that closing the operation will not send a terminate message.
*/
setStateEnded() {
this.state = OperationState.Ended;
}
}

/**
Expand Down Expand Up @@ -837,6 +844,11 @@ export class RequestResponseOperation<RequestType, ResponseType> extends EventEm
await this.operation.activate(requestMessage);

let message : eventstream.Message = await responsePromise;

// If the server terminated the stream, then set the operation to be ended immediately
if ((message.flags ?? 0) & eventstream.MessageFlags.TerminateStream) {
this.operation.setStateEnded();
}
let response : ResponseType = deserializeResponse(this.serviceModel, this.operationConfig.name, message);

resolve(response);
Expand Down Expand Up @@ -928,6 +940,15 @@ export class StreamingOperation<RequestType, ResponseType, OutboundMessageType,
let message : eventstream.Message = await responsePromise;
let response : ResponseType = deserializeResponse(this.serviceModel, this.operationConfig.name, message);

// If the server terminated the stream, then set the operation to be ended immediately
if ((message.flags ?? 0) & eventstream.MessageFlags.TerminateStream) {
this.operation.setStateEnded();
// Server hung up on us. Immediately cleanup the operation state.
// Do this before resolving the promise so that any user-initiated
// requests will see the correct state, which is that the operation is closed.
await this.close();
}

resolve(response);
} catch (e) {
await this.close();
Expand Down

0 comments on commit 54b50ab

Please sign in to comment.