Friday, 26 May 2017

In expressjs how would I limit the number of users who can make a request at the same time?

My code is just a regular app:

app
  .use(sassMiddleware({
      src: __dirname + '/sass',
      dest: __dirname + '/',
      // This line controls sass log output
      debug: false,
      outputStyle: 'compressed'
  }))

  // More libraries
  ...

  .get('/', auth.protected, function (req, res) {
    res.sendfile(__dirname + '/views/index.html');
  })

.post('/dostuff', auth.protected, function (req, res) {
    console.log(req.body)
    res.redirect('back')

    child = require('child_process').spawn(
        './script.rb',
        ['arguments'],
        { stdio: ['ignore', 'pipe', 'pipe'] }
    );
    child.stdout.pipe(process.stdout);
    child.stderr.pipe(process.stdout);
  })

My original goal is to limit the number of spawns that /dostuff can spawn to a single instance. I was thinking that there might be a simple way to limit the number of users on the entire app, but can't seem to find any.

I was trying to look for some session limiting mechanism but can't find one either, only various rate limiters but I don't think that's what I want.

Since the app is running in docker I limit the number of tcp connections on the port using iptalbes but this has proven to be less then ideal since the app retains some connections in established state which prevents efficient hand off from one user to another.

So... any programmatic way of doing this?



via user3081519

No comments:

Post a Comment