@uswitch/koa-timeout

ā° A Koa middleware to handle timeouts correctly

Downloads in past

Stats

StarsIssuesVersionUpdatedCreatedSize
@uswitch/koa-timeout
16641.6.26 months ago5 years agoMinified + gzip package size for @uswitch/koa-timeout in KB

Readme

ā° Koa Timeout

A <b><a href="http://koajs.com">Koa</a></b> middleware to handle
timeouts correctly


How it works | Usage


Contributors License() type language test style

How it works

koa-timeout uses Promise.race to race a setTimeout against your Koa middlewares/response.

Timeout example

Middlewares
              A 1s     B 2s       C 3s      D 4s
         ā•­ā”€ā”€ā”€ā”€ā”€ā”“ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”“ā”€ā”€ā”€ā”€ā”€ā•³ā•Œā•Œā•Œā•Œā”“ā•Œā•Œā•Œā•Œā•Œā•Œā•Œā•Œā•Œā”“ā•Œā•Œā•Œā•Œā•Œ
Req ā”€ā”€ā”€ā”€ā”€ā”¤
         ā•°ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”¬ā”€ā”€ā†’ 408 - 2500ms
                           Timeout
                             2.5s
In this example, a request has come in and the timeout is racing middlewares, A, B, C & D. However, the timeout is triggered causing a 408 response after 2500ms.
The ā•³ signifys a short circuit and prevents middlewares C & D from running.

Successful example

Middlewares
              A 1s     B 2s      C 3s      D 4s
         ā•­ā”€ā”€ā”€ā”€ā”€ā”“ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”“ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”“ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”“ā”€ā”€ā”€ā”€ā”€ā”€ā•®
         ā”‚                                         ā”‚
Req ā”€ā”€ā”€ā”€ā”€ā”¤                                         ā•°ā”€ā”€ā†’ 200 - 4500ms
         ā”‚
         ā•°ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā•³ā•Œā•Œā•Œā•Œā•Œā•Œā•Œā•Œā•Œā•Œā•Œā•Œā•Œā•Œā•Œā•Œā•Œā•Œā•Œā•Œā•®
                                                                     Timeout
                                                                        5s
In this example, all 4 middlewares - A, B, C & D - have finished, resulting in a 200 response after ~4500ms. The timeout is then cleared, signified by the ā•³.

Usage

The middleware can be configured with a custom timeout time and status code.
import Koa from 'koa'
import timeout from '@uswitch/koa-timeout'

const app = new Koa()

app.use(timeout(1000))                     // 1s timeout
app.use(timeout(1000, { status: 499 }))    // 1s timeout with 499 status

Short circuiting

N.B. By default, koa-timeout does not short circuit the other middlewares. This means that the in-flight request will continue to be processed even though a response has already been sent.
This behaviour can be useful in development to see where the slow parts of the request are, however, in production you probably want to kill the request as soon as possible.
To enable short circuiting
This will prevent the next middleware after a timeout from triggering.
import { shortCircuit } from '@uswitch/koa-timeout'

app.use(timeout(5000))

app.get('*', shortCircuit(handleInitialState))
app.get('*', shortCircuit(handleRendering))
app.get('*', shortCircuit(handleReturn))

// Or

app.get('*', ...shortCircuit(handleInitialState, handleRendering, handleReturn))

N.B. when calling with multiple middlewares, make sure to spread the result!
i.e. ...shortCircuit(a, b, c)