re-use existing connection on getData endpoint #130
Merged
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.
Currently, the getData endpoint establishes a new socket for every individual variable description + type. This results in a lot of TCP handshakes (and DNS queries if using a domain name for the NUT server -- see #126 ). When running PeaNUT in kubernetes, I was frequently experiencing "500 internal server error" messages in the client (as well as in the data API endpoints + InfluxDB writer failing). I never really found a "smoking gun" but maybe something about the flood of connections didn't play nicely with my CNI. I have been able to successfully run PeaNUT + InfluxDB in kubernetes with my changes and it has been running smoothly, it hasn't missed a beat
I re-worked the getData endpoint to establish a single connection and re-use it for each command it executes. Because of this re-work, I also needed to be more careful about cleaning up event listeners on the socket so as not to create a memory leak, so there are a few minor changes there as well.
This is my first time attempting to contribute to PeaNUT, I tried my best to follow the coding style, but happy to make any changes or implement this in a different way if needed. Really like the project, but have been having these connection issues with the 4.x releases when running in kubernetes, so I'm hoping something like this can be implemented!