Skip to content

Commit

Permalink
refactor: improve sendStatus a little when there is no metadata involved
Browse files Browse the repository at this point in the history
  • Loading branch information
AVVS committed Oct 19, 2022
1 parent d124214 commit c24c213
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 13 deletions.
6 changes: 5 additions & 1 deletion packages/grpc-js/src/call-interface.ts
Expand Up @@ -36,6 +36,10 @@ export interface StatusObject {
metadata: Metadata;
}

export type PartialStatusObject = Pick<StatusObject, 'code' | 'details'> & {
metadata: Metadata | null;
}

export const enum WriteFlags {
BufferHint = 1,
NoCompress = 2,
Expand Down Expand Up @@ -166,4 +170,4 @@ export interface Call {
halfClose(): void;
getCallNumber(): number;
setCredentials(credentials: CallCredentials): void;
}
}
29 changes: 18 additions & 11 deletions packages/grpc-js/src/server-call.ts
Expand Up @@ -33,7 +33,7 @@ import { StreamDecoder } from './stream-decoder';
import { ObjectReadable, ObjectWritable } from './object-stream';
import { ChannelOptions } from './channel-options';
import * as logging from './logging';
import { StatusObject } from './call-interface';
import { PartialStatusObject, StatusObject } from './call-interface';
import { Deadline } from './deadline';

const TRACER_NAME = 'server_call';
Expand Down Expand Up @@ -441,9 +441,12 @@ export class Http2ServerCallStream<
this.sendStatus({
code: Status.CANCELLED,
details: 'Cancelled by client',
metadata: new Metadata(),
metadata: null,
});
}

// to compensate for a fact that cancelled is not always called
this.emit('close');
});

this.stream.on('drain', () => {
Expand Down Expand Up @@ -497,7 +500,7 @@ export class Http2ServerCallStream<
this.metadataSent = true;
const custom = customMetadata ? customMetadata.toHttp2Headers() : null;
// TODO(cjihrig): Include compression headers.
const headers = Object.assign({}, defaultResponseHeaders, custom);
const headers = { ...defaultResponseHeaders, ...custom };
this.stream.respond(headers, defaultResponseOptions);
}

Expand Down Expand Up @@ -654,18 +657,19 @@ export class Http2ServerCallStream<
async sendUnaryMessage(
err: ServerErrorResponse | ServerStatusResponse | null,
value?: ResponseType | null,
metadata?: Metadata,
metadata?: Metadata | null,
flags?: number
) {
if (this.checkCancelled()) {
return;
}
if (!metadata) {
metadata = new Metadata();

if (metadata === undefined) {
metadata = null;
}

if (err) {
if (!Object.prototype.hasOwnProperty.call(err, 'metadata')) {
if (!Object.prototype.hasOwnProperty.call(err, 'metadata') && metadata) {
err.metadata = metadata;
}
this.sendError(err);
Expand All @@ -683,7 +687,7 @@ export class Http2ServerCallStream<
}
}

sendStatus(statusObj: StatusObject) {
sendStatus(statusObj: PartialStatusObject) {
this.emit('callEnd', statusObj.code);
this.emit('streamEnd', statusObj.code === Status.OK);
if (this.checkCancelled()) {
Expand All @@ -707,7 +711,7 @@ export class Http2ServerCallStream<
const trailersToSend = {
[GRPC_STATUS_HEADER]: statusObj.code,
[GRPC_MESSAGE_HEADER]: encodeURI(statusObj.details),
...statusObj.metadata.toHttp2Headers(),
...statusObj.metadata?.toHttp2Headers(),
};

this.stream.sendTrailers(trailersToSend);
Expand All @@ -719,13 +723,13 @@ export class Http2ServerCallStream<
}

sendError(error: ServerErrorResponse | ServerStatusResponse) {
const status: StatusObject = {
const status: PartialStatusObject = {
code: Status.UNKNOWN,
details: 'message' in error ? error.message : 'Unknown Error',
metadata:
'metadata' in error && error.metadata !== undefined
? error.metadata
: new Metadata(),
: null,
};

if (
Expand Down Expand Up @@ -774,6 +778,9 @@ export class Http2ServerCallStream<
call.emit('cancelled', reason);
});

// to compensate for the fact that cancelled is no longer always called
this.once('close', () => call.emit('close'))

this.once('callEnd', (status) => call.emit('callEnd', status));
}

Expand Down
1 change: 0 additions & 1 deletion packages/grpc-js/src/server.ts
Expand Up @@ -81,7 +81,6 @@ function getUnimplementedStatusResponse(
return {
code: Status.UNIMPLEMENTED,
details: `The server does not implement the method ${methodName}`,
metadata: new Metadata(),
};
}

Expand Down

0 comments on commit c24c213

Please sign in to comment.