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
Clean up store error event listener to prevent memory leak #82
Clean up store error event listener to prevent memory leak #82
Conversation
c9fb9f9
to
07e333f
Compare
Thanks so much for submitting this @clement-buchart. This is technically a breaking change. The It is for both request errors and underlying DB errors. See: https://github.com/lukechilds/cacheable-request#onerror-error So removing the event after each request means DB errors will not get passed through. However the memory leak is obviously a big issue. I am definitely ok with making breaking changes to get it resolved. |
src/index.js
Outdated
@@ -186,7 +186,10 @@ class CacheableRequest { | |||
} | |||
}; | |||
|
|||
this.cache.on('error', error => ee.emit('error', new CacheableRequest.CacheError(error))); | |||
const storeErrorHandler = error => ee.emit('error', new CacheableRequest.CacheError(error)); | |||
this.cache.on('error', storeErrorHandler); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This can be changed to this.cache.once
. Then you don't need to manually remove it after it's fired. (Still needs to be manually removed after response
though)
src/index.js
Outdated
@@ -186,7 +186,10 @@ class CacheableRequest { | |||
} | |||
}; | |||
|
|||
this.cache.on('error', error => ee.emit('error', new CacheableRequest.CacheError(error))); | |||
const storeErrorHandler = error => ee.emit('error', new CacheableRequest.CacheError(error)); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we just call this errorHandler
as opposed to storeErrorHandler
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done
d41ff8a
to
7df14e0
Compare
I'm not sure I fully understand. Sorry if I'm missing something obvious. |
7df14e0
to
ccf0a68
Compare
@clement-buchart Sorry, you are correct. I was thinking Apologies for the delay on this, it's taken me a while to get round to review it due to a few crazy things in my personal life. You mentioned in the related issue this cleared up your memory leak issues. Do you have any stats you can share for that? |
Hello, Here is what I can easily share (the project being a commercial one) : I simply generated load via ApacheBenchmark. Sample A (before fix) : The app quickly grows to use more than 1Gi RAM. Then a memory leak is detected by node-memwatch, and the app shuts down. When taking a Heapdump, a single Keyv Instance keep a trace of all request in memory : Sample B (after fix) :
The app's memory footprint quickly stabilize (under same load), and Keyv holds almost no memory Here are two dump after similar load generated, before and after fix : Cheers. |
@clement-buchart wow, thank you so much, this is great. |
Published as |
@clement-buchart You can submit your PR here to claim a bounty: https://issuehunt.io/r/lukechilds/cacheable-request/issues/30 |
Fixes #30