Skip to content

Commit 779e970

Browse files
authored
Merge pull request #2597 from murgatroid99/grpc-js_server_deprecate_start
grpc-js: Deprecate Server#start
2 parents 4342f60 + 9765673 commit 779e970

File tree

2 files changed

+20
-37
lines changed

2 files changed

+20
-37
lines changed

packages/grpc-js/src/server.ts

Lines changed: 20 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
*/
1717

1818
import * as http2 from 'http2';
19+
import * as util from 'util';
1920
import { AddressInfo } from 'net';
2021

2122
import { ServiceError } from './call';
@@ -89,6 +90,17 @@ interface BindResult {
8990

9091
function noop(): void {}
9192

93+
/**
94+
* Decorator to wrap a class method with util.deprecate
95+
* @param message The message to output if the deprecated method is called
96+
* @returns
97+
*/
98+
function deprecate(message: string) {
99+
return function <This, Args extends any[], Return>(target: (this: This, ...args: Args) => Return, context: ClassMethodDecoratorContext<This, (this: This, ...args: Args) => Return>) {
100+
return util.deprecate(target, message);
101+
}
102+
}
103+
92104
function getUnimplementedStatusResponse(
93105
methodName: string
94106
): Partial<ServiceError> {
@@ -160,6 +172,10 @@ export class Server {
160172
UntypedHandler
161173
>();
162174
private sessions = new Map<http2.ServerHttp2Session, ChannelzSessionInfo>();
175+
/**
176+
* This field only exists to ensure that the start method throws an error if
177+
* it is called twice, as it did previously.
178+
*/
163179
private started = false;
164180
private options: ChannelOptions;
165181
private serverAddressString = 'null';
@@ -371,10 +387,6 @@ export class Server {
371387
creds: ServerCredentials,
372388
callback: (error: Error | null, port: number) => void
373389
): void {
374-
if (this.started === true) {
375-
throw new Error('server is already started');
376-
}
377-
378390
if (typeof port !== 'string') {
379391
throw new TypeError('port must be a string');
380392
}
@@ -709,8 +721,6 @@ export class Server {
709721
}
710722
}
711723

712-
this.started = false;
713-
714724
// Always destroy any available sessions. It's possible that one or more
715725
// tryShutdown() calls are in progress. Don't wait on them to finish.
716726
this.sessions.forEach((channelzInfo, session) => {
@@ -750,6 +760,10 @@ export class Server {
750760
return this.handlers.delete(name);
751761
}
752762

763+
/**
764+
* @deprecated No longer needed as of version 1.10.x
765+
*/
766+
@deprecate('Calling start() is no longer necessary. It can be safely omitted.')
753767
start(): void {
754768
if (
755769
this.http2ServerList.length === 0 ||
@@ -763,9 +777,6 @@ export class Server {
763777
if (this.started === true) {
764778
throw new Error('server is already started');
765779
}
766-
if (this.channelzEnabled) {
767-
this.channelzTrace.addTrace('CT_INFO', 'Starting');
768-
}
769780
this.started = true;
770781
}
771782

@@ -786,9 +797,6 @@ export class Server {
786797
}
787798
}
788799

789-
// Close the server if necessary.
790-
this.started = false;
791-
792800
for (const { server: http2Server, channelzRef: ref } of this
793801
.http2ServerList) {
794802
if (http2Server.listening) {
@@ -1053,10 +1061,6 @@ export class Server {
10531061

10541062
http2Server.on('stream', handler.bind(this));
10551063
http2Server.on('session', session => {
1056-
if (!this.started) {
1057-
session.destroy();
1058-
return;
1059-
}
10601064

10611065
const channelzRef = registerChannelzSocket(
10621066
session.socket.remoteAddress ?? 'unknown',

packages/grpc-js/test/test-server.ts

Lines changed: 0 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -113,27 +113,6 @@ describe('Server', () => {
113113
});
114114
});
115115

116-
it('throws if bind is called after the server is started', done => {
117-
const server = new Server();
118-
119-
server.bindAsync(
120-
'localhost:0',
121-
ServerCredentials.createInsecure(),
122-
(err, port) => {
123-
assert.ifError(err);
124-
server.start();
125-
assert.throws(() => {
126-
server.bindAsync(
127-
'localhost:0',
128-
ServerCredentials.createInsecure(),
129-
noop
130-
);
131-
}, /server is already started/);
132-
server.tryShutdown(done);
133-
}
134-
);
135-
});
136-
137116
it('throws on invalid inputs', () => {
138117
const server = new Server();
139118

0 commit comments

Comments
 (0)