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

Also set timeout on HTTP request if deadline for grpc call is set #1063

Merged
merged 2 commits into from
May 10, 2021
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 9 additions & 3 deletions javascript/net/grpc/web/grpcwebclientbase.js
Original file line number Diff line number Diff line change
Expand Up @@ -325,16 +325,22 @@ class GrpcWebClientBase {
xhr.headers.set('X-User-Agent', 'grpc-web-javascript/0.1');
xhr.headers.set('X-Grpc-Web', '1');
if (xhr.headers.containsKey('deadline')) {
var deadline = xhr.headers.get('deadline'); // in ms
var currentTime = (new Date()).getTime();
var timeout = Math.round(deadline - currentTime);
const deadline = xhr.headers.get('deadline'); // in ms
const currentTime = (new Date()).getTime();
let timeout = Math.ceil(deadline - currentTime);
xhr.headers.remove('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