Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions backend/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions backend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@
"showdown": "^2.1.0",
"swagger-ui-express": "^5.0.1",
"tar-stream": "^3.2.0",
"toobusy-js": "^0.5.1",
"undici": "^8.2.0",
"utility-types": "^3.11.0",
"uuid": "^14.0.0",
Expand Down
3 changes: 2 additions & 1 deletion backend/src/routes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,15 @@ import { fileURLToPath } from 'url'
import authentication from './connectors/authentication/index.js'
import internalRouter from './routes/internal/routes.js'
import { expressErrorHandler } from './routes/middleware/expressErrorHandler.js'
import { tooBusy } from './routes/middleware/tooBusy.js'
import v1Router from './routes/v1/routes.js'
import v2Router from './routes/v2/routes.js'
import v3Router from './routes/v3/routes.js'
import { httpLog } from './services/log.js'

export const server = express()

server.use([bodyParser.json(), httpLog])
server.use([bodyParser.json(), httpLog, tooBusy])
const middlewareConfigs = authentication.authenticationMiddleware()
for (const middlewareConf of middlewareConfigs) {
server.use(middlewareConf?.path || '/', middlewareConf.middleware)
Expand Down
12 changes: 12 additions & 0 deletions backend/src/routes/middleware/tooBusy.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import toobusy from 'toobusy-js'

import log from '../../services/log.js'

export function tooBusy(req, res, next) {
if (toobusy()) {
log.warn('Server response time too long, preventing more requests from being handled.')
res.status(503).send('Server Too Busy')

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

throw a ConfigurationError

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I naively assumed this library was only limiting requests from the same origin. Also, is ConfigurationError what we want? To me that would imply that there was a problem with the configuration, whereas this is a purposely thrown error.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think our naming for this error isn't correct. Really it should be ServiceUnavailable but we should let our error handler handle it so that error is consistent for the user, inline with all other errors within the app.

Have a look here for usage of a 503

Common causes are that a server is down for maintenance or overloaded

} else {
next()
}
}
Loading