File size: 675 Bytes
1e92f2d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
// @flow
import toobusy from 'toobusy-js';

// Middleware which blocks requests when the Node server is too busy
// now automatically retries the request at another instance of the server if it's too busy
export default (
  req: express$Request | http$IncomingMessage,
  res: express$Response | http$ServerResponse,
  next: express$NextFunction | (() => void)
) => {
  // // Don't send 503s in testing, that's dumb, just wait it out
  if (process.env.NODE_ENV !== 'testing' && !process.env.TEST_DB && toobusy()) {
    res.statusCode = 503;
    res.end(
      'It looks like Spectrum is very busy right now, please try again in a minute.'
    );
  } else {
    next();
  }
};