Skip to content

Commit

Permalink
Redo grpc#1063 - Also set timeout on HTTP request if deadline for grp…
Browse files Browse the repository at this point in the history
…c call is set.

Redoing grpc#1063 since it was recently overwritten by an internal code sync.
  • Loading branch information
sampajano committed Sep 28, 2021
1 parent f1fe574 commit 47d5138
Showing 1 changed file with 7 additions and 1 deletion.
8 changes: 7 additions & 1 deletion javascript/net/grpc/web/grpcwebclientbase.js
Original file line number Diff line number Diff line change
Expand Up @@ -317,14 +317,20 @@ class GrpcWebClientBase {
if (xhr.headers.has('deadline')) {
const deadline = Number(xhr.headers.get('deadline')); // in ms
const currentTime = (new Date()).getTime();
let timeout = Math.round(deadline - currentTime);
let timeout = Math.ceil(deadline - currentTime);
xhr.headers.delete('deadline');
if (timeout === Infinity) {
// grpc-timeout header defaults to infinity if not set.
timeout = 0;
}
if (timeout > 0) {
xhr.headers.set('grpc-timeout', timeout + 'm');
// Also set timeout on the xhr request to terminate the HTTP request
// if the server doesn't respond within the deadline. We use 110% of
// grpc-timeout for this to allow the server to terminate the connection
// with DEADLINE_EXCEEDED rather than terminating it in the Browser, but
// at least 1 second in case the user is on a high-latency network.
xhr.setTimeoutInterval(Math.max(1000, Math.ceil(timeout * 1.1)));
}
}
}
Expand Down

0 comments on commit 47d5138

Please sign in to comment.