mirror of https://github.com/grpc/grpc-node.git
Merge pull request #2352 from murgatroid99/grpc-js_deadline_refinement
grpc-js: Improve timeout handling and deadline logging
This commit is contained in:
commit
ba08267516
|
@ -51,8 +51,45 @@ export function getDeadlineTimeoutString(deadline: Deadline) {
|
|||
throw new Error('Deadline is too far in the future')
|
||||
}
|
||||
|
||||
/**
|
||||
* See https://nodejs.org/api/timers.html#settimeoutcallback-delay-args
|
||||
* In particular, "When delay is larger than 2147483647 or less than 1, the
|
||||
* delay will be set to 1. Non-integer delays are truncated to an integer."
|
||||
* This number of milliseconds is almost 25 days.
|
||||
*/
|
||||
const MAX_TIMEOUT_TIME = 2147483647;
|
||||
|
||||
/**
|
||||
* Get the timeout value that should be passed to setTimeout now for the timer
|
||||
* to end at the deadline. For any deadline before now, the timer should end
|
||||
* immediately, represented by a value of 0. For any deadline more than
|
||||
* MAX_TIMEOUT_TIME milliseconds in the future, a timer cannot be set that will
|
||||
* end at that time, so it is treated as infinitely far in the future.
|
||||
* @param deadline
|
||||
* @returns
|
||||
*/
|
||||
export function getRelativeTimeout(deadline: Deadline) {
|
||||
const deadlineMs = deadline instanceof Date ? deadline.getTime() : deadline;
|
||||
const now = new Date().getTime();
|
||||
return deadlineMs - now;
|
||||
const timeout = deadlineMs - now;
|
||||
if (timeout < 0) {
|
||||
return 0;
|
||||
} else if (timeout > MAX_TIMEOUT_TIME) {
|
||||
return Infinity
|
||||
} else {
|
||||
return timeout;
|
||||
}
|
||||
}
|
||||
|
||||
export function deadlineToString(deadline: Deadline): string {
|
||||
if (deadline instanceof Date) {
|
||||
return deadline.toISOString();
|
||||
} else {
|
||||
const dateDeadline = new Date(deadline);
|
||||
if (Number.isNaN(dateDeadline.getTime())) {
|
||||
return '' + deadline;
|
||||
} else {
|
||||
return dateDeadline.toISOString();
|
||||
}
|
||||
}
|
||||
}
|
|
@ -46,7 +46,7 @@ import { LoadBalancingCall } from './load-balancing-call';
|
|||
import { CallCredentials } from './call-credentials';
|
||||
import { Call, CallStreamOptions, InterceptingListener, MessageContext, StatusObject } from './call-interface';
|
||||
import { SubchannelCall } from './subchannel-call';
|
||||
import { Deadline, getDeadlineTimeoutString } from './deadline';
|
||||
import { Deadline, deadlineToString, getDeadlineTimeoutString } from './deadline';
|
||||
import { ResolvingCall } from './resolving-call';
|
||||
import { getNextCallNumber } from './call-number';
|
||||
import { restrictControlPlaneStatusCode } from './control-plane-status';
|
||||
|
@ -469,7 +469,7 @@ export class InternalChannel {
|
|||
'] method="' +
|
||||
method +
|
||||
'", deadline=' +
|
||||
deadline
|
||||
deadlineToString(deadline)
|
||||
);
|
||||
const finalOptions: CallStreamOptions = {
|
||||
deadline: deadline,
|
||||
|
|
|
@ -18,7 +18,7 @@
|
|||
import { CallCredentials } from "./call-credentials";
|
||||
import { Call, CallStreamOptions, InterceptingListener, MessageContext, StatusObject } from "./call-interface";
|
||||
import { LogVerbosity, Propagate, Status } from "./constants";
|
||||
import { Deadline, getDeadlineTimeoutString, getRelativeTimeout, minDeadline } from "./deadline";
|
||||
import { Deadline, deadlineToString, getDeadlineTimeoutString, getRelativeTimeout, minDeadline } from "./deadline";
|
||||
import { FilterStack, FilterStackFactory } from "./filter-stack";
|
||||
import { InternalChannel } from "./internal-channel";
|
||||
import { Metadata } from "./metadata";
|
||||
|
@ -79,9 +79,9 @@ export class ResolvingCall implements Call {
|
|||
|
||||
private runDeadlineTimer() {
|
||||
clearTimeout(this.deadlineTimer);
|
||||
this.trace('Deadline: ' + this.deadline);
|
||||
if (this.deadline !== Infinity) {
|
||||
const timeout = getRelativeTimeout(this.deadline);
|
||||
this.trace('Deadline: ' + deadlineToString(this.deadline));
|
||||
const timeout = getRelativeTimeout(this.deadline);
|
||||
if (timeout !== Infinity) {
|
||||
this.trace('Deadline will be reached in ' + timeout + 'ms');
|
||||
const handleDeadline = () => {
|
||||
this.cancelWithStatus(
|
||||
|
|
Loading…
Reference in New Issue