jpodwys / cache-service-redis

A redis plugin for cache-service.
MIT License
7 stars 9 forks source link

Dangling Connections in AWS Lambda #18

Closed hottehead closed 5 years ago

hottehead commented 5 years ago

Hey,

after some days of production usage in AWS Lambda I notice a very high redis connection count. It seems the connections are not closed on handler termination. The setup is the same as documented in my earlier issue

Thanks in advance. If this requires coding work I'm happy to do it and make a PR. =)

jpodwys commented 5 years ago

cache.db is the underlying node_redis instance. You should be able to terminate it using some public method listed in their documentation. Just make sure you're reading the version of their docs that this library consumes.

jpodwys commented 5 years ago

Sorry, I was on my phone this morning when I responded so I was having a hard time providing more useful input. Here's a code snippet based on the original.

exports.handler = function (event, context, callback) {
  superagent
    .get(uri)
    .use(superagentCache)
    .end(function (err, response){
      /* handle response here */

      // terminate redis connection using `cache-service-redis`
      // instance directly
      redisCache.db.quit();

      // OR terminate redis connection using `cache-service-redis`
      // instance referenced through `superagent-cache-plugin`
      superagentCache.cache.db.quit();

      // terminate lambda process
      return context.succeed();
    }
  );
};

Please let me know how this works for you. Good luck!

jpodwys commented 5 years ago

Just following up on this issue.

hottehead commented 5 years ago

Hey,

It works by using redisCache.db.quit();directly. If using the referenced instance through superagentCache.cache.db.quit(); it complained about the cache instance being undefined. I could not resolve this.

Furthermore I think I need to promisify the quit method of the cache as there are still some connections open, though they are much less than before. But some of them dont get correctly closed I think. Probably due to the lambda handler exiting before the non-promised quit is fully executed.

At the moment I dont have time to investigate deeper. I could probably try this next week and give some more feedback.

jpodwys commented 5 years ago

Sorry, quit accepts a callback. I should have used that in my code snippet. On my phone again now or I would have updated it.

jpodwys commented 5 years ago

Closing due to inactivity.