cloudant / nodejs-cloudant

Cloudant Node.js client library
Apache License 2.0
255 stars 90 forks source link

Memory leak readme #420

Closed vmatyus closed 4 years ago

vmatyus commented 4 years ago

Checklist

Description

Under some circumstances there is a memory leak when the Cloudant client is instantiated more than one times during an application lifetime.

var Cloudant = require('./cloudant');
const http = require('http');

const server = http.createServer((req, res) => {
  var cloudantBasic = Cloudant({
    //account setting
    plugins: 'cookieauth'
  });

  async function getDbList() {
    return cloudantBasic.db.list();
  }

  getDbList().then(result => {
    res.statusCode = 200;
    res.end(JSON.stringify(result) + '\n');
    console.log('####test success');
  }).catch(err => {
    res.statusCode = 401;
    res.end(err.error + '\n');
    console.log('####test failed', err);
  });
});

server.listen(3000);
console.log('Server listening to port 3000. Press Ctrl+C to stop it.');

In this case when the server receives a request it will instantiate a client and after it is answered all the request that went out toward Cloudant will remain in the memory.

Approach

During the investigation I used Chrome inspector tool, package heapdump to create a snapshot about the memory usage and process.memoryUsage to see the size of the memory reservations. As the result of the investigation it was determined the default Agent keeps the network connections open for further usage thanks to setting keepAlive=false. This settings improves the response times and has performance advantages. This way it will be kept. We agreed not to change the agent storing, but extend the README.md with the supported client usage best practice. This way code was not change.

Schema & API Changes

No change

Security and Privacy

Correct vulnerability issue with package minimist: https://www.npmjs.com/advisories/1179

Testing

I created a script that measured the memory usage in of both of the token handling instances: Cookie and IAM. This script is not committed. No new test case added.

Monitoring and Logging

No change