* The `inflightRequestThrottle` module allows you to specify an upper limit to * the maximum number of inflight requests your server is able to handle. This * is a simple heuristic for protecting against event loop contention between * requests causing unacceptable latencies. * * The custom err
(opts)
| 36 | * server.pre(restify.plugins.inflightRequestThrottle(options)); |
| 37 | */ |
| 38 | function inflightRequestThrottle(opts) { |
| 39 | // Scrub input and populate our configuration |
| 40 | assert.object(opts, 'opts'); |
| 41 | assert.number(opts.limit, 'opts.limit'); |
| 42 | assert.object(opts.server, 'opts.server'); |
| 43 | assert.func(opts.server.inflightRequests, 'opts.server.inflightRequests'); |
| 44 | |
| 45 | if (opts.err !== undefined && opts.err !== null) { |
| 46 | assert.ok(opts.err instanceof Error, 'opts.err must be an error'); |
| 47 | assert.optionalNumber(opts.err.statusCode, 'opts.err.statusCode'); |
| 48 | } |
| 49 | |
| 50 | var plugin = {}; |
| 51 | plugin._err = opts.err || new ServiceUnavailableError('resource exhausted'); |
| 52 | plugin._limit = opts.limit; |
| 53 | plugin._server = opts.server; |
| 54 | |
| 55 | function onRequest(req, res, next) { |
| 56 | var inflightRequests = plugin._server.inflightRequests(); |
| 57 | |
| 58 | if (inflightRequests > plugin._limit) { |
| 59 | req.log.trace( |
| 60 | { |
| 61 | plugin: 'inflightRequestThrottle', |
| 62 | inflightRequests: inflightRequests, |
| 63 | limit: plugin._limit |
| 64 | }, |
| 65 | 'maximum inflight requests exceeded, rejecting request' |
| 66 | ); |
| 67 | return next(plugin._err); |
| 68 | } |
| 69 | |
| 70 | return next(); |
| 71 | } |
| 72 | |
| 73 | return onRequest; |
| 74 | } |
| 75 | |
| 76 | module.exports = inflightRequestThrottle; |
no outgoing calls
no test coverage detected