Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Channel#watchConnectivityState: handle infinite deadlines correctly #1553

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
40 changes: 23 additions & 17 deletions packages/grpc-js/src/channel.ts
Expand Up @@ -120,7 +120,7 @@ export interface Channel {

interface ConnectivityStateWatcher {
currentState: ConnectivityState;
timer: NodeJS.Timeout;
timer: NodeJS.Timeout | null;
callback: (error?: Error) => void;
}

Expand Down Expand Up @@ -417,7 +417,9 @@ export class ChannelImplementation implements Channel {
const watchersCopy = this.connectivityStateWatchers.slice();
for (const watcherObject of watchersCopy) {
if (newState !== watcherObject.currentState) {
clearTimeout(watcherObject.timer);
if(watcherObject.timer) {
clearTimeout(watcherObject.timer);
}
this.removeConnectivityStateWatcher(watcherObject);
watcherObject.callback();
}
Expand Down Expand Up @@ -452,25 +454,29 @@ export class ChannelImplementation implements Channel {
deadline: Date | number,
callback: (error?: Error) => void
): void {
const deadlineDate: Date =
deadline instanceof Date ? deadline : new Date(deadline);
const now = new Date();
if (deadlineDate <= now) {
process.nextTick(
callback,
new Error('Deadline passed without connectivity state change')
);
return;
}
const watcherObject = {
currentState,
callback,
timer: setTimeout(() => {
let timer = null;
if(deadline !== Infinity) {
const deadlineDate: Date =
deadline instanceof Date ? deadline : new Date(deadline);
const now = new Date();
if (deadline === -Infinity || deadlineDate <= now) {
process.nextTick(
callback,
new Error('Deadline passed without connectivity state change')
);
return;
}
timer = setTimeout(() => {
this.removeConnectivityStateWatcher(watcherObject);
callback(
new Error('Deadline passed without connectivity state change')
);
}, deadlineDate.getTime() - now.getTime()),
}, deadlineDate.getTime() - now.getTime())
}
const watcherObject = {
currentState,
callback,
timer
};
this.connectivityStateWatchers.push(watcherObject);
}
Expand Down