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
Disable Excon sockets thread cache #346
Merged
Merged
Conversation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Opened an issue for Excon: excon/excon#651 |
StupidCodeFactory
suggested changes
Oct 27, 2017
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.
same as in the corresponding PR in pvb2
alan
added a commit
to ministryofjustice/prison-visits-2
that referenced
this pull request
Oct 30, 2017
Current version of Excon has a memory leak when threads using an Excon connection get destroyed. Applies same fix as ministryofjustice/prison-visits-public#346 The memory leak in pvb2 is slower because Puma is configured to use less threads so they probably get recycled less often. Also sets a constant number of threads for Puma in production as we do in pvb-public.
StupidCodeFactory
pushed a commit
to ministryofjustice/prison-visits-2
that referenced
this pull request
Oct 30, 2017
Current version of Excon has a memory leak when threads using an Excon connection get destroyed. Applies same fix as ministryofjustice/prison-visits-public#346 The memory leak in pvb2 is slower because Puma is configured to use less threads so they probably get recycled less often. Also sets a constant number of threads for Puma in production as we do in pvb-public.
This is the cause of the memory leak that we experienced recently. Every Excon connection has a cache of sockets that ensures that a socket is only used by a specific thread during its lifetime. This means that 1 Excon connection might hold any number of open sockets to the same server. In our case for example if Puma is running with 4 threads, and a connection pool of 4 Excon connections to the PVB API it means that there will a maximum of 16 HTTP persistent connections. The issue is that the implementation of the cache doesn't clear old connections for threads that are no longer running (an issue when Puma is configured to autoscale threads). We inadevertedly solved the memory leak by configuring Puma to use a fixed number of threads. The implementation of this cache was changed just before we started having memory leaks to fix a use case of Excon that we don't use. The old implementation didn't have a memory leak with our use case. Although this makes the `PrisonVisits::Client` not thread safe it's fine in our use case because: - we use a connection pool of connections and a connection is only used by 1 thread at a time. - It means that we don't hold sockets open unnecessarily for each Excon connection. I've tested the value of this setting locally by load testing the server with concurrent requests: - with the cache enabled and Puma autoscaling threads it showed the memory leak when spacing the load testing by 1 minute between benchmarks - with the cache disabled and Puma autoscaling threads memory usage remained stable and there were no threading issues. Excon [discussion](excon/excon#640) about the recent change to the cache where it actually mentions that it has an memory leak issue.
alan
force-pushed
the
disable-connection-thread-cache
branch
from
October 30, 2017 11:11
ed5ea72
to
194d030
Compare
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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 is the cause of the memory leak that we experienced recently.
Every Excon connection has a cache of sockets that ensures that a socket is only
used by a specific thread during its lifetime. This means that 1 Excon
connection might hold any number of open sockets to the same server.
In our case for example if Puma is running with 4 threads, and a connection pool
of 4 Excon connections to the PVB API it means that there will a maximum of 16
HTTP persistent connections.
The issue is that the implementation of the cache doesn't clear old connections
for threads that are no longer running (an issue when Puma is configured to
autoscale threads). We inadevertedly solved the memory leak by configuring Puma
to use a fixed number of threads.
The implementation of this cache was changed just before we started having
memory leaks to fix a use case of Excon that we don't use. The old
implementation didn't have a memory leak with our use case.
Although this makes the
PrisonVisits::Client
not thread safe it's fine in ouruse case because:
thread at a time.
connection.
I've tested the value of this setting locally by load testing the server with
concurrent requests:
leak when spacing the load testing by 1 minute between benchmarks
stable and there were no threading issues.
Excon discussion about the recent
change to the cache where it actually mentions that it has an memory leak issue.