add setting which allows us to set a timeout on HTTP requests (#6364)

Co-authored-by: repo-ranger[bot] <39074581+repo-ranger[bot]@users.noreply.github.com>
This commit is contained in:
chris48s
2021-04-05 18:56:32 +01:00
committed by GitHub
parent 4a5f6a453a
commit b1fc492592
4 changed files with 57 additions and 3 deletions

View File

@@ -18,6 +18,7 @@ const { makeSend } = require('../base-service/legacy-result-sender')
const { handleRequest } = require('../base-service/legacy-request-handler')
const { clearRegularUpdateCache } = require('../legacy/regular-update')
const { rasterRedirectUrl } = require('../badge-urls/make-badge-url')
const { nonNegativeInteger } = require('../../services/validators')
const log = require('./log')
const sysMonitor = require('./monitor')
const PrometheusMetrics = require('./prometheus-metrics')
@@ -137,12 +138,11 @@ const publicConfigSchema = Joi.object({
teamcity: defaultService,
trace: Joi.boolean().required(),
}).required(),
cacheHeaders: {
defaultCacheLengthSeconds: Joi.number().integer().required(),
},
cacheHeaders: { defaultCacheLengthSeconds: nonNegativeInteger },
rateLimit: Joi.boolean().required(),
handleInternalErrors: Joi.boolean().required(),
fetchLimit: Joi.string().regex(/^[0-9]+(b|kb|mb|gb|tb)$/i),
requestTimeoutSeconds: nonNegativeInteger,
documentRoot: Joi.string().default(
path.resolve(__dirname, '..', '..', 'public')
),
@@ -476,6 +476,7 @@ class Server {
this.registerRedirects()
this.registerServices()
camp.timeout = this.config.public.requestTimeoutSeconds * 1000
camp.listenAsConfigured()
await new Promise(resolve => camp.on('listening', () => resolve()))

View File

@@ -207,6 +207,55 @@ describe('The server', function () {
})
})
describe('`requestTimeoutSeconds` setting', function () {
let server
beforeEach(async function () {
this.timeout(10000)
// configure server to time out requests that take >2 seconds
server = await createTestServer({ public: { requestTimeoutSeconds: 2 } })
await server.start()
// /fast returns a 200 OK after a 1 second delay
server.camp.route(/^\/fast$/, (data, match, end, ask) => {
setTimeout(() => {
ask.res.statusCode = 200
ask.res.end()
}, 1000)
})
// /slow returns a 200 OK after a 3 second delay
server.camp.route(/^\/slow$/, (data, match, end, ask) => {
setTimeout(() => {
ask.res.statusCode = 200
ask.res.end()
}, 3000)
})
})
afterEach(async function () {
if (server) {
server.stop()
}
server = undefined
})
it('should time out slow requests', async function () {
this.timeout(10000)
return expect(got(`${server.baseUrl}slow`)).to.be.rejectedWith(
got.RequestError
)
})
it('should not time out fast requests', async function () {
this.timeout(10000)
const { statusCode, body } = await got(`${server.baseUrl}fast`)
expect(statusCode).to.be.equal(200)
expect(body).to.equal('')
})
})
describe('configuration', function () {
let server
afterEach(async function () {