Skip to content

Commit e943672

Browse files
authoredFeb 26, 2021
Add read timeout (#1518)
1 parent 51d88a0 commit e943672

File tree

3 files changed

+50
-10
lines changed

3 files changed

+50
-10
lines changed
 

‎readme.md

+4-1
Original file line numberDiff line numberDiff line change
@@ -518,7 +518,10 @@ This also accepts an `object` with the following fields to constrain the duratio
518518
- `socket` starts when the socket is connected. See [request.setTimeout](https://nodejs.org/api/http.html#http_request_settimeout_timeout_callback).
519519
- `response` starts when the request has been written to the socket and ends when the response headers are received.
520520
- `send` starts when the socket is connected and ends with the request has been written to the socket.
521-
- `request` starts when the request is initiated and ends when the response's end event fires.
521+
- `request` starts when the request is initiated and ends when the response's `end` event fires.
522+
- ~~`read` starts when the `response` event is emitted and ends when the response's `end` event fires.~~
523+
524+
**Note:** The `read` timeout is blocked by https://github.com/nodejs/node/issues/35923
522525

523526
###### retry
524527

‎source/core/utils/timed-out.ts

+15-9
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,12 @@ interface TimedOutOptions {
1313

1414
export interface Delays {
1515
lookup?: number;
16+
socket?: number;
1617
connect?: number;
1718
secureConnect?: number;
18-
socket?: number;
19-
response?: number;
2019
send?: number;
20+
response?: number;
21+
read?: number;
2122
request?: number;
2223
}
2324

@@ -89,14 +90,12 @@ export default (request: ClientRequest, delays: Delays, options: TimedOutOptions
8990
}
9091
});
9192

92-
request.once('close', cancelTimeouts);
93-
94-
once(request, 'response', (response: IncomingMessage): void => {
95-
once(response, 'end', cancelTimeouts);
96-
});
97-
9893
if (typeof delays.request !== 'undefined') {
99-
addTimeout(delays.request, timeoutHandler, 'request');
94+
const cancelTimeout = addTimeout(delays.request, timeoutHandler, 'request');
95+
96+
once(request, 'response', (response: IncomingMessage): void => {
97+
once(response, 'end', cancelTimeout);
98+
});
10099
}
101100

102101
if (typeof delays.socket !== 'undefined') {
@@ -168,6 +167,13 @@ export default (request: ClientRequest, delays: Delays, options: TimedOutOptions
168167
});
169168
}
170169

170+
if (typeof delays.read !== 'undefined') {
171+
once(request, 'response', (response: IncomingMessage): void => {
172+
const cancelTimeout = addTimeout(delays.read!, timeoutHandler, 'read');
173+
once(response, 'end', cancelTimeout);
174+
});
175+
}
176+
171177
return cancelTimeouts;
172178
};
173179

‎test/timeout.ts

+31
Original file line numberDiff line numberDiff line change
@@ -642,6 +642,37 @@ test.serial('no unhandled `Premature close` error', withServer, async (t, server
642642
await delay(20);
643643
});
644644

645+
// TODO: use fakeTimers here
646+
test.serial('`read` timeout - promise', withServer, async (t, server, got) => {
647+
server.get('/', (_request, response) => {
648+
response.write('o');
649+
});
650+
651+
await t.throwsAsync(got({
652+
timeout: {
653+
read: 10
654+
},
655+
retry: 0
656+
}), {message: 'Timeout awaiting \'read\' for 10ms'});
657+
});
658+
659+
// TODO: use fakeTimers here
660+
test.serial.failing('`read` timeout - stream', withServer, async (t, server, got) => {
661+
t.timeout(100);
662+
663+
server.get('/', (_request, response) => {
664+
response.end('ok');
665+
});
666+
667+
const stream = got.stream({
668+
timeout: {
669+
read: 10
670+
}
671+
});
672+
673+
await t.throwsAsync(pEvent(stream, 'end'), {message: 'Timeout awaiting \'read\' for 10ms'});
674+
});
675+
645676
// TODO: use fakeTimers here
646677
test.serial('cancelling the request removes timeouts', withServer, async (t, server, got) => {
647678
server.get('/', (_request, response) => {

0 commit comments

Comments
 (0)
Please sign in to comment.