Throw an error if users try to initialise LocalStorage or global instance more than once #150
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This change throws an error if a single instance of
LocalStorage
or the global instance is initialised more than once. This helps users avoid accidentally initialising multiple times in both cases, which I believe is not intended to be supported and, for the global instance, causes a resource leak.Since this change means node-persist will throw exceptions in places where it previously didn't I think this is a breaking change.
We had a problem in our app where CPU usage would slowly climb over time and eventually lock up the server. We found the global node-persist
init
was being called in a function that was itself being called many times per request. WhileLocalStorage
seems to be ok withinit
being called multiple times (but IMO shouldn't silently allow it), the globalinit
creates a newLocalStorage
instance every time it's called. If either write queues or expiry are enabled (they are by default), the new instances keep registering new interval callbacks and the old ones are not cancelled, preventing the old instances from being garbage collected and forcing node to process an increasing number of callbacks over time until eventually all CPU is consumed.