Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

More advanced compression "filter" #29

Closed
ilanbiala opened this issue Jan 4, 2015 · 6 comments
Closed

More advanced compression "filter" #29

ilanbiala opened this issue Jan 4, 2015 · 6 comments
Labels

Comments

@ilanbiala
Copy link

Some compression libraries will lower the compression level and even stop compressing if the memory consumption gets too high, and then they will start compressing again once the system has calmed down. Is there something available like that here?

@Fishrock123
Copy link
Contributor

That is zlib's concern, I believe.

@dougwilson
Copy link
Contributor

The way you can do this is simply to wrap this middleware, just like any middleware.

Simple example:

var compression = require('compression')
var express = require('express')

express.use(variableCompression())

function variableCompression() {
  var compressors = Object.create(null)

  return function (req, res, next) {
    if (!should_compress) { // should_compress is up to you
      next()
      return
    }

    var level = get_compression_level() // get_compression_level is up to you
    var compressor = compressors[level] || (compressors[level] = compression({level: level}))

    compressor(req, res, next)
  }
}

@ilanbiala
Copy link
Author

@dougwilson thanks for the start, does zlib handle any of that, or I have to? Any ideas on how to get memory consumption percentage through node?

@dougwilson
Copy link
Contributor

does zlib handle any of that, or I have to?

I really don't know :/

Any ideas on how to get memory consumption percentage through node?

I've never tried to do that, so I'm not sure off-hand. There is an os.totalmem() and os.freemem() in the os module (http://nodejs.org/api/os.html) that you could probably use.

@ilanbiala
Copy link
Author

@dougwilson thanks for the help!

@dougwilson
Copy link
Contributor

No problem :) Feel free to continue to ask follow ups on this issue, btw.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants