Skip to content

Commit

Permalink
Merge pull request #2521 from murgatroid99/v1.8.x_upmerge_3
Browse files Browse the repository at this point in the history
Merge 1.8.x into master
  • Loading branch information
murgatroid99 authored Jul 31, 2023
2 parents 426768d + 1d38cc3 commit 96213d1
Show file tree
Hide file tree
Showing 4 changed files with 120 additions and 38 deletions.
2 changes: 1 addition & 1 deletion packages/grpc-js/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@grpc/grpc-js",
"version": "1.8.19",
"version": "1.8.21",
"description": "gRPC Library for Node - pure JS implementation",
"homepage": "https://grpc.io/",
"repository": "https://github.com/grpc/grpc-node/tree/master/packages/grpc-js",
Expand Down
35 changes: 12 additions & 23 deletions packages/grpc-js/src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,6 @@ import {
unregisterChannelzRef,
} from './channelz';
import { CipherNameAndProtocol, TLSSocket } from 'tls';
import { getErrorCode, getErrorMessage } from './error';

const UNLIMITED_CONNECTION_AGE_MS = ~(1 << 31);
const KEEPALIVE_MAX_TIME_MS = ~(1 << 31);
Expand Down Expand Up @@ -846,11 +845,7 @@ export class Server {
return true;
}

private _retrieveHandler(
headers: http2.IncomingHttpHeaders
): Handler<any, any> {
const path = headers[HTTP2_HEADER_PATH] as string;

private _retrieveHandler(path: string): Handler<any, any> | null {
this.trace(
'Received call to method ' +
path +
Expand All @@ -866,7 +861,7 @@ export class Server {
path +
'. Sending UNIMPLEMENTED status.'
);
throw getUnimplementedStatusResponse(path);
return null;
}

return handler;
Expand Down Expand Up @@ -908,15 +903,12 @@ export class Server {
return;
}

let handler: Handler<any, any>;
try {
handler = this._retrieveHandler(headers);
} catch (err) {
const path = headers[HTTP2_HEADER_PATH] as string;

const handler = this._retrieveHandler(path);
if (!handler) {
this._respondWithError(
{
details: getErrorMessage(err),
code: getErrorCode(err) ?? undefined,
},
getUnimplementedStatusResponse(path),
stream,
channelzSessionInfo
);
Expand Down Expand Up @@ -970,15 +962,12 @@ export class Server {
return;
}

let handler: Handler<any, any>;
try {
handler = this._retrieveHandler(headers);
} catch (err) {
const path = headers[HTTP2_HEADER_PATH] as string;

const handler = this._retrieveHandler(path);
if (!handler) {
this._respondWithError(
{
details: getErrorMessage(err),
code: getErrorCode(err) ?? undefined,
},
getUnimplementedStatusResponse(path),
stream,
null
);
Expand Down
31 changes: 17 additions & 14 deletions packages/grpc-js/src/transport.ts
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,18 @@ class Http2Transport implements Transport {
*/
private remoteName: string | null
) {
/* Populate subchannelAddressString and channelzRef before doing anything
* else, because they are used in the trace methods. */
this.subchannelAddressString = subchannelAddressToString(subchannelAddress);

if (options['grpc.enable_channelz'] === 0) {
this.channelzEnabled = false;
}
this.channelzRef = registerChannelzSocket(
this.subchannelAddressString,
() => this.getChannelzInfo(),
this.channelzEnabled
);
// Build user-agent string.
this.userAgent = [
options['grpc.primary_user_agent'],
Expand All @@ -174,20 +186,6 @@ class Http2Transport implements Transport {
} else {
this.keepaliveWithoutCalls = false;
}
if (this.keepaliveWithoutCalls) {
this.maybeStartKeepalivePingTimer();
}

this.subchannelAddressString = subchannelAddressToString(subchannelAddress);

if (options['grpc.enable_channelz'] === 0) {
this.channelzEnabled = false;
}
this.channelzRef = registerChannelzSocket(
this.subchannelAddressString,
() => this.getChannelzInfo(),
this.channelzEnabled
);

session.once('close', () => {
this.trace('session closed');
Expand Down Expand Up @@ -233,6 +231,11 @@ class Http2Transport implements Transport {
);
});
}
/* Start the keepalive timer last, because this can trigger trace logs,
* which should only happen after everything else is set up. */
if (this.keepaliveWithoutCalls) {
this.maybeStartKeepalivePingTimer();
}
}

private getChannelzInfo(): SocketInfo {
Expand Down
90 changes: 90 additions & 0 deletions packages/grpc-js/test/test-server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -430,6 +430,7 @@ describe('Server', () => {
(error: ServiceError, response: any) => {
assert(error);
assert.strictEqual(error.code, grpc.status.UNIMPLEMENTED);
assert.match(error.details, /does not implement the method.*Div/);
done();
}
);
Expand All @@ -439,6 +440,7 @@ describe('Server', () => {
const call = client.sum((error: ServiceError, response: any) => {
assert(error);
assert.strictEqual(error.code, grpc.status.UNIMPLEMENTED);
assert.match(error.details, /does not implement the method.*Sum/);
done();
});

Expand All @@ -455,6 +457,7 @@ describe('Server', () => {
call.on('error', (err: ServiceError) => {
assert(err);
assert.strictEqual(err.code, grpc.status.UNIMPLEMENTED);
assert.match(err.details, /does not implement the method.*Fib/);
done();
});
});
Expand All @@ -469,6 +472,93 @@ describe('Server', () => {
call.on('error', (err: ServiceError) => {
assert(err);
assert.strictEqual(err.code, grpc.status.UNIMPLEMENTED);
assert.match(err.details, /does not implement the method.*DivMany/);
done();
});

call.end();
});
});

describe('Unregistered service', () => {
let server: Server;
let client: ServiceClient;

const mathProtoFile = path.join(__dirname, 'fixtures', 'math.proto');
const mathClient = (loadProtoFile(mathProtoFile).math as any).Math;

before(done => {
server = new Server();
// Don't register a service at all
server.bindAsync(
'localhost:0',
ServerCredentials.createInsecure(),
(err, port) => {
assert.ifError(err);
client = new mathClient(
`localhost:${port}`,
grpc.credentials.createInsecure()
);
server.start();
done();
}
);
});

after(done => {
client.close();
server.tryShutdown(done);
});

it('should respond to a unary call with UNIMPLEMENTED', done => {
client.div(
{ divisor: 4, dividend: 3 },
(error: ServiceError, response: any) => {
assert(error);
assert.strictEqual(error.code, grpc.status.UNIMPLEMENTED);
assert.match(error.details, /does not implement the method.*Div/);
done();
}
);
});

it('should respond to a client stream with UNIMPLEMENTED', done => {
const call = client.sum((error: ServiceError, response: any) => {
assert(error);
assert.strictEqual(error.code, grpc.status.UNIMPLEMENTED);
assert.match(error.details, /does not implement the method.*Sum/);
done();
});

call.end();
});

it('should respond to a server stream with UNIMPLEMENTED', done => {
const call = client.fib({ limit: 5 });

call.on('data', (value: any) => {
assert.fail('No messages expected');
});

call.on('error', (err: ServiceError) => {
assert(err);
assert.strictEqual(err.code, grpc.status.UNIMPLEMENTED);
assert.match(err.details, /does not implement the method.*Fib/);
done();
});
});

it('should respond to a bidi call with UNIMPLEMENTED', done => {
const call = client.divMany();

call.on('data', (value: any) => {
assert.fail('No messages expected');
});

call.on('error', (err: ServiceError) => {
assert(err);
assert.strictEqual(err.code, grpc.status.UNIMPLEMENTED);
assert.match(err.details, /does not implement the method.*DivMany/);
done();
});

Expand Down

0 comments on commit 96213d1

Please sign in to comment.