Skip to content

Commit

Permalink
add setting which allows us to set a timeout on HTTP requests (#6364)
Browse files Browse the repository at this point in the history
Co-authored-by: repo-ranger[bot] <39074581+repo-ranger[bot]@users.noreply.github.com>
  • Loading branch information
chris48s and repo-ranger[bot] authored Apr 5, 2021
1 parent 4a5f6a4 commit b1fc492
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 3 deletions.
2 changes: 2 additions & 0 deletions config/custom-environment-variables.yml
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,8 @@ public:

fetchLimit: 'FETCH_LIMIT'

requestTimeoutSeconds: 'REQUEST_TIMEOUT_SECONDS'

requireCloudflare: 'REQUIRE_CLOUDFLARE'

private:
Expand Down
2 changes: 2 additions & 0 deletions config/default.yml
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ public:

fetchLimit: '10MB'

requestTimeoutSeconds: 120

requireCloudflare: false

private: {}
7 changes: 4 additions & 3 deletions core/server/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down Expand Up @@ -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')
),
Expand Down Expand Up @@ -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()))
Expand Down
49 changes: 49 additions & 0 deletions core/server/server.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 () {
Expand Down

0 comments on commit b1fc492

Please sign in to comment.