-
Notifications
You must be signed in to change notification settings - Fork 72
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
The trilogy client eagerly allocate a 32kiB buffer, and grows it as needed. It's never freed not shrunk until the connection is closed. Since by default the MySQL `max_allowed_packet` is 16MiB, long living connections will progressively grow to that size. For basic usage it's not a big deal, but some applications may have dozens if not hundreds of connections that are mostly idle. A common case being multi-tenant applications with horizontal sharding. In such cases you only ever query one database but have open connections to many databases. This situation might lead to a lot of memory retained by trilogy connections and never really released, looking very much like a memory leak. This can be reproduced with a simple script: ```ruby require 'trilogy' connection_pool = [] 50.times do t = Trilogy.new(database: "test") t.query("select '#{"a" * 16_000_000}' as a") connection_pool << t end puts "#{`ps -o rss= -p #{$$}`} kiB" ``` ``` $ ruby /tmp/trilogy-leak.rb 927120 kiB ``` If we instead take over the buffer lifetime management, we can implement some pooling for the buffers, we can limit the total number of buffer to as many connections are actually in use concurrently. The same reproduction script with the current branch: ``` $ ruby -Ilib:ext /tmp/trilogy-leak.rb 108144 kiB ```
- Loading branch information
Showing
7 changed files
with
265 additions
and
12 deletions.
There are no files selected for viewing
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
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
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
Oops, something went wrong.