Skip to content

Commit 867caad

Browse files
authored
Merge pull request #2516 from murgatroid99/v1.8.x_upmerge_again
Merge v1.8.x into master
2 parents af31ef0 + 71d035b commit 867caad

File tree

3 files changed

+60
-28
lines changed

3 files changed

+60
-28
lines changed

packages/grpc-js/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@grpc/grpc-js",
3-
"version": "1.8.18",
3+
"version": "1.8.19",
44
"description": "gRPC Library for Node - pure JS implementation",
55
"homepage": "https://grpc.io/",
66
"repository": "https://github.com/grpc/grpc-node/tree/master/packages/grpc-js",

packages/grpc-js/src/server-call.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -962,8 +962,8 @@ export class Http2ServerCallStream<
962962
}
963963

964964
getPeer(): string {
965-
const socket = this.stream.session.socket;
966-
if (socket.remoteAddress) {
965+
const socket = this.stream.session?.socket;
966+
if (socket?.remoteAddress) {
967967
if (socket.remotePort) {
968968
return `${socket.remoteAddress}:${socket.remotePort}`;
969969
} else {

packages/grpc-js/src/transport.ts

Lines changed: 57 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,12 @@ class Http2Transport implements Transport {
108108
/**
109109
* Timer reference for timeout that indicates when to send the next ping
110110
*/
111-
private keepaliveIntervalId: NodeJS.Timer;
111+
private keepaliveTimerId: NodeJS.Timer | null = null;
112+
/**
113+
* Indicates that the keepalive timer ran out while there were no active
114+
* calls, and a ping should be sent the next time a call starts.
115+
*/
116+
private pendingSendKeepalivePing = false;
112117
/**
113118
* Timer reference tracking when the most recent ping will be considered lost
114119
*/
@@ -169,10 +174,8 @@ class Http2Transport implements Transport {
169174
} else {
170175
this.keepaliveWithoutCalls = false;
171176
}
172-
this.keepaliveIntervalId = setTimeout(() => {}, 0);
173-
clearTimeout(this.keepaliveIntervalId);
174177
if (this.keepaliveWithoutCalls) {
175-
this.startKeepalivePings();
178+
this.maybeStartKeepalivePingTimer();
176179
}
177180

178181
this.subchannelAddressString = subchannelAddressToString(subchannelAddress);
@@ -375,6 +378,14 @@ class Http2Transport implements Transport {
375378
this.disconnectListeners.push(listener);
376379
}
377380

381+
private clearKeepaliveTimer() {
382+
if (!this.keepaliveTimerId) {
383+
return;
384+
}
385+
clearTimeout(this.keepaliveTimerId);
386+
this.keepaliveTimerId = null;
387+
}
388+
378389
private clearKeepaliveTimeout() {
379390
if (!this.keepaliveTimeoutId) {
380391
return;
@@ -383,7 +394,19 @@ class Http2Transport implements Transport {
383394
this.keepaliveTimeoutId = null;
384395
}
385396

386-
private sendPing() {
397+
private canSendPing() {
398+
return (
399+
this.keepaliveTimeMs > 0 &&
400+
(this.keepaliveWithoutCalls || this.activeCalls.size > 0)
401+
);
402+
}
403+
404+
private maybeSendPing() {
405+
this.clearKeepaliveTimer();
406+
if (!this.canSendPing()) {
407+
this.pendingSendKeepalivePing = true;
408+
return;
409+
}
387410
if (this.channelzEnabled) {
388411
this.keepalivesSent += 1;
389412
}
@@ -402,6 +425,7 @@ class Http2Transport implements Transport {
402425
(err: Error | null, duration: number, payload: Buffer) => {
403426
this.keepaliveTrace('Received ping response');
404427
this.clearKeepaliveTimeout();
428+
this.maybeStartKeepalivePingTimer();
405429
}
406430
);
407431
} catch (e) {
@@ -411,46 +435,54 @@ class Http2Transport implements Transport {
411435
}
412436
}
413437

414-
private startKeepalivePings() {
415-
if (this.keepaliveTimeMs < 0) {
438+
/**
439+
* Starts the keepalive ping timer if appropriate. If the timer already ran
440+
* out while there were no active requests, instead send a ping immediately.
441+
* If the ping timer is already running or a ping is currently in flight,
442+
* instead do nothing and wait for them to resolve.
443+
*/
444+
private maybeStartKeepalivePingTimer() {
445+
if (!this.canSendPing()) {
416446
return;
417447
}
418-
this.keepaliveIntervalId = setInterval(() => {
419-
this.sendPing();
420-
}, this.keepaliveTimeMs);
421-
this.keepaliveIntervalId.unref?.();
422-
/* Don't send a ping immediately because whatever caused us to start
423-
* sending pings should also involve some network activity. */
448+
if (this.pendingSendKeepalivePing) {
449+
this.pendingSendKeepalivePing = false;
450+
this.maybeSendPing();
451+
} else if (!this.keepaliveTimerId && !this.keepaliveTimeoutId) {
452+
this.keepaliveTrace(
453+
'Starting keepalive timer for ' + this.keepaliveTimeMs + 'ms'
454+
);
455+
this.keepaliveTimerId = setTimeout(() => {
456+
this.maybeSendPing();
457+
}, this.keepaliveTimeMs).unref?.();
458+
}
459+
/* Otherwise, there is already either a keepalive timer or a ping pending,
460+
* wait for those to resolve. */
424461
}
425462

426-
/**
427-
* Stop keepalive pings when terminating a connection. This discards the
428-
* outstanding ping timeout, so it should not be called if the same
429-
* connection will still be used.
430-
*/
431463
private stopKeepalivePings() {
432-
clearInterval(this.keepaliveIntervalId);
464+
if (this.keepaliveTimerId) {
465+
clearTimeout(this.keepaliveTimerId);
466+
this.keepaliveTimerId = null;
467+
}
433468
this.clearKeepaliveTimeout();
434469
}
435470

436471
private removeActiveCall(call: Http2SubchannelCall) {
437472
this.activeCalls.delete(call);
438473
if (this.activeCalls.size === 0) {
439474
this.session.unref();
440-
if (!this.keepaliveWithoutCalls) {
441-
this.stopKeepalivePings();
442-
}
443475
}
444476
}
445477

446478
private addActiveCall(call: Http2SubchannelCall) {
447-
if (this.activeCalls.size === 0) {
479+
this.activeCalls.add(call);
480+
if (this.activeCalls.size === 1) {
448481
this.session.ref();
449482
if (!this.keepaliveWithoutCalls) {
450-
this.startKeepalivePings();
483+
this.maybeStartKeepalivePingTimer();
451484
}
452485
}
453-
this.activeCalls.add(call);
454486
}
455487

456488
createCall(

0 commit comments

Comments
 (0)