-
Notifications
You must be signed in to change notification settings - Fork 83
feat(datafile manager): Add request timeouts #268
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
Changes from all commits
5d37245
674cd9e
da378b3
0ebe801
f3dea83
e36b9af
bb91931
fd0d321
64b9aaf
aaa1caa
c3dcb25
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -25,12 +25,12 @@ | |
}, | ||
"devDependencies": { | ||
"@types/jest": "^24.0.9", | ||
"@types/nise": "^1.4.0", | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nise! |
||
"@types/nock": "^9.3.1", | ||
"@types/node": "^11.11.7", | ||
"@types/sinon": "^7.0.10", | ||
"jest": "^24.1.0", | ||
"nise": "^1.4.10", | ||
"nock": "^10.0.6", | ||
"sinon": "^7.2.7", | ||
"ts-jest": "^24.0.0", | ||
"typescript": "^3.3.3333" | ||
}, | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -21,3 +21,5 @@ export const MIN_UPDATE_INTERVAL = 1000 | |
export const DEFAULT_URL_TEMPLATE = `https://cdn.optimizely.com/datafiles/%s.json` | ||
|
||
export const BACKOFF_BASE_WAIT_SECONDS_BY_ERROR_COUNT = [0, 8, 16, 32, 64, 128, 256, 512] | ||
|
||
export const REQUEST_TIMEOUT_MS = 60 * 1000 // 1 minute | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: DEFAULT_REQUEST_TIME_MS? |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -18,6 +18,7 @@ import http from 'http' | |
import https from 'https' | ||
import url from 'url' | ||
import { Headers, AbortableRequest, Response } from './http' | ||
import { REQUEST_TIMEOUT_MS } from './config'; | ||
|
||
// Shared signature between http.request and https.request | ||
type ClientRequestCreator = (options: http.RequestOptions) => http.ClientRequest | ||
|
@@ -64,6 +65,11 @@ function getResponseFromRequest(request: http.ClientRequest): Promise<Response> | |
// TODO: When we drop support for Node 6, consider using util.promisify instead of | ||
// constructing own Promise | ||
return new Promise((resolve, reject) => { | ||
const timeout = setTimeout(() => { | ||
request.abort() | ||
reject(new Error('Request timed out')) | ||
}, REQUEST_TIMEOUT_MS) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think we should make this configurable and have it injected in by the datafile manager. That we this doesn't have to rely directly on the config file There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Chatted offline about doing this as a later refactor to keep the scope of this small. |
||
|
||
request.once('response', (incomingMessage: http.IncomingMessage) => { | ||
if (request.aborted) { | ||
return | ||
|
@@ -83,6 +89,8 @@ function getResponseFromRequest(request: http.ClientRequest): Promise<Response> | |
return | ||
} | ||
|
||
clearTimeout(timeout) | ||
|
||
resolve({ | ||
statusCode: incomingMessage.statusCode, | ||
body: responseData, | ||
|
@@ -92,6 +100,8 @@ function getResponseFromRequest(request: http.ClientRequest): Promise<Response> | |
}) | ||
|
||
request.on('error', (err: any) => { | ||
clearTimeout(timeout) | ||
|
||
if (err instanceof Error) { | ||
reject(err) | ||
} else if (typeof err === 'string') { | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: do you actually need to reply if it's gunna just time out
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not sure how to implement this test without calling
reply
. From what I can tell, the'request'
event is not emitted on the scope object when I don't callreply
.