From bd21a922bdc528d13b6c50a49f3c6772e4a75e13 Mon Sep 17 00:00:00 2001 From: Merkushin Date: Wed, 2 Oct 2013 17:37:03 +0600 Subject: [PATCH] fix(connection) synchronize agents pool --- .../integration/mysql/connection_pool.rb | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) diff --git a/lib/sphinx/integration/mysql/connection_pool.rb b/lib/sphinx/integration/mysql/connection_pool.rb index 5888f2a..1d7658f 100644 --- a/lib/sphinx/integration/mysql/connection_pool.rb +++ b/lib/sphinx/integration/mysql/connection_pool.rb @@ -48,11 +48,16 @@ def self.master_pool end def self.agents_pool(agent_name) - @agents_pool ||= {} - @agents_pool[agent_name] ||= Innertube::Pool.new( - Proc.new { Sphinx::Integration::Mysql::ConnectionPool.slave_connection(agent_name) }, - Proc.new { |connection| connection.close } - ) + return @agents_pool[agent_name] if @agents_pool.key?(agent_name) + + @take_slaves_mutex.synchronize do + return @agents_pool[agent_name] if @agents_pool.key?(agent_name) + + @agents_pool[agent_name] ||= Innertube::Pool.new( + Proc.new { Sphinx::Integration::Mysql::ConnectionPool.slave_connection(agent_name) }, + Proc.new { |connection| connection.close } + ) + end end def self.take @@ -79,6 +84,9 @@ def self.take end def self.take_slaves(silent = true) + @take_slaves_mutex ||= Mutex.new + @agents_pool ||= {} + threads = [] ThinkingSphinx::Configuration.instance.agents.each do |agent_name, _|