Skip to content

Do not use connection-pooling in single-threaded process. #3197

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

Open
wants to merge 2 commits into
base: version-3
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion gems/aws-sdk-core/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
Unreleased Changes
------------------

3.219.1 (2025-02-19)
------------------
* Issue - Do not use connection-pooling in single-threaded processes.

3.219.0 (2025-02-18)
------------------

Expand Down Expand Up @@ -41,7 +45,7 @@ Unreleased Changes

* Issue - Use epoch seconds instead of milliseconds in cbor encode/decode.

* Issue - Add handling of block in response delegation (#3169).
* Issue - Add handling of block in response delegation (#3169).

3.216.0 (2025-01-15)
------------------
Expand Down
56 changes: 38 additions & 18 deletions gems/aws-sdk-core/lib/seahorse/client/net_http/connection_pool.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,11 @@ module Client
module NetHttp

class ConnectionPool
if Thread.list.select {|thread| thread.status == "run"}.count > 1
@pools_mutex = Mutex.new
@pools = {}
end

@pools_mutex = Mutex.new
@pools = {}
@default_logger = Logger.new($stdout)

OPTIONS = {
Expand All @@ -45,8 +47,10 @@ def initialize(options = {})
value = options[opt_name].nil? ? default_value : options[opt_name]
instance_variable_set("@#{opt_name}", value)
end
@pool_mutex = Mutex.new
@pool = {}
if @pools_mutex
@pool_mutex = Mutex.new
@pool = {}
end
end

OPTIONS.keys.each do |attr_name|
Expand Down Expand Up @@ -91,10 +95,12 @@ def session_for(endpoint, &block)
session = nil

# attempt to recycle an already open session
@pool_mutex.synchronize do
_clean
if @pool.key?(endpoint)
session = @pool[endpoint].shift
if @pool_mutex
@pool_mutex.synchronize do
_clean
if @pool.key?(endpoint)
session = @pool[endpoint].shift
end
end
end

Expand All @@ -108,10 +114,14 @@ def session_for(endpoint, &block)
session.finish if session
raise
else
# No error raised? Good, check the session into the pool.
@pool_mutex.synchronize do
@pool[endpoint] = [] unless @pool.key?(endpoint)
@pool[endpoint] << session
if @pool_mutex
# No error raised? Good, check the session into the pool.
@pool_mutex.synchronize do
@pool[endpoint] = [] unless @pool.key?(endpoint)
@pool[endpoint] << session
end
else
session.finish
end
end
nil
Expand All @@ -120,6 +130,8 @@ def session_for(endpoint, &block)
# @return [Integer] Returns the count of sessions currently in the
# pool, not counting those currently in use.
def size
return 0 unless @pool_mutex

@pool_mutex.synchronize do
@pool.values.flatten.size
end
Expand All @@ -129,7 +141,7 @@ def size
# the idle timeout).
# @return [nil]
def clean!
@pool_mutex.synchronize { _clean }
@pool_mutex.synchronize { _clean } if @pool_mutex
nil
end

Expand All @@ -139,9 +151,11 @@ def clean!
# state.
# @return [nil]
def empty!
@pool_mutex.synchronize do
@pool.values.flatten.map(&:finish)
@pool.clear
if @pool_mutex
@pool_mutex.synchronize do
@pool.values.flatten.map(&:finish)
@pool.clear
end
end
nil
end
Expand Down Expand Up @@ -214,14 +228,20 @@ class << self
# @return [ConnectionPool]
def for options = {}
options = pool_options(options)
@pools_mutex.synchronize do
@pools[options] ||= new(options)
if @pools_mutex
@pools_mutex.synchronize do
@pools[options] ||= new(options)
end
else
new(options)
end
end

# @return [Array<ConnectionPool>] Returns a list of the
# constructed connection pools.
def pools
return [] unless @pools_mutex

@pools_mutex.synchronize do
@pools.values
end
Expand Down