forked from github/docs
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtimeout.js
More file actions
41 lines (33 loc) · 1.46 KB
/
Copy pathtimeout.js
File metadata and controls
41 lines (33 loc) · 1.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
import timeout from 'express-timeout-handler'
import statsd from '../lib/statsd.js'
// Heroku router requests timeout after 30 seconds. We should stop them earlier!
const maxRequestTimeout = parseInt(process.env.REQUEST_TIMEOUT, 10) || 10000
export default timeout.handler({
// Default timeout for all endpoints
// To override for a given router/endpoint, use `xExpressTimeoutHandler.set(...)`
timeout: maxRequestTimeout,
// IMPORTANT:
// We cannot allow the middleware to disable the `res` object's methods like
// it does by default if we want to use `next` in the `onTimeout` handler!
disable: [],
onTimeout: function (req, res, next) {
const incrementTags = []
// Be careful with depending on attributes set on the `req` because
// under certain conditions the contextualizers might not yet have
// had a chance to run.
if (req.pagePath) {
incrementTags.push(`path:${req.pagePath}`)
}
if (req.context?.currentCategory) {
incrementTags.push(`product:${req.context.currentCategory}`)
}
statsd.increment('middleware.timeout', 1, incrementTags)
// Create a custom timeout error
const timeoutError = new Error('Request timed out')
timeoutError.statusCode = 503
timeoutError.code = 'ETIMEDOUT'
// Pass the error to our Express error handler for consolidated processing
return next(timeoutError)
},
// Can also set an `onDelayedResponse` property IF AND ONLY IF you allow for disabling methods
})