From f3370da11a867bae287d7f549a671811e8b399ef Mon Sep 17 00:00:00 2001
From: Mehdi Abaakouk <mehdi.abaakouk@enovance.com>
Date: Mon, 1 Dec 2014 11:11:05 +0100
Subject: [PATCH] Don't share connection pool between driver object

Each driver instance must use it's own connection pool.

This removes the last global state of qpid and rabbitmq driver
Make the relation between classes more simple.

The previous behavior was not very safe, as explained in the bug report.

And also, this is a first step to replace this custom connection pool
handling by the kombu one.

Closes bug: #1397925
Partial bug: #1397339

Change-Id: Iecd2b39c76417d9ac081d46810f72eb6e38edfda
---
 oslo/messaging/_drivers/amqp.py        | 23 -----------------------
 oslo/messaging/_drivers/impl_qpid.py   |  2 +-
 oslo/messaging/_drivers/impl_rabbit.py |  2 +-
 3 files changed, 2 insertions(+), 25 deletions(-)

diff --git a/oslo/messaging/_drivers/amqp.py b/oslo/messaging/_drivers/amqp.py
index f198f004c..a7c221090 100644
--- a/oslo/messaging/_drivers/amqp.py
+++ b/oslo/messaging/_drivers/amqp.py
@@ -25,7 +25,6 @@ uses AMQP, but is deprecated and predates this code.
 
 import collections
 import logging
-import threading
 import uuid
 
 import six
@@ -71,28 +70,6 @@ class ConnectionPool(pool.Pool):
     def empty(self):
         for item in self.iter_free():
             item.close()
-        # Force a new connection pool to be created.
-        # Note that this was added due to failing unit test cases. The issue
-        # is the above "while loop" gets all the cached connections from the
-        # pool and closes them, but never returns them to the pool, a pool
-        # leak. The unit tests hang waiting for an item to be returned to the
-        # pool. The unit tests get here via the tearDown() method. In the run
-        # time code, it gets here via cleanup() and only appears in service.py
-        # just before doing a sys.exit(), so cleanup() only happens once and
-        # the leakage is not a problem.
-        del self.connection_cls.pools[self.url]
-
-
-_pool_create_sem = threading.Lock()
-
-
-def get_connection_pool(conf, url, connection_cls):
-    with _pool_create_sem:
-        # Make sure only one thread tries to create the connection pool.
-        if url not in connection_cls.pools:
-            connection_cls.pools[url] = ConnectionPool(conf, url,
-                                                       connection_cls)
-    return connection_cls.pools[url]
 
 
 class ConnectionContext(rpc_common.Connection):
diff --git a/oslo/messaging/_drivers/impl_qpid.py b/oslo/messaging/_drivers/impl_qpid.py
index 28bde7ca9..6d4e7911f 100644
--- a/oslo/messaging/_drivers/impl_qpid.py
+++ b/oslo/messaging/_drivers/impl_qpid.py
@@ -723,7 +723,7 @@ class QpidDriver(amqpdriver.AMQPDriverBase):
         conf.register_opts(qpid_opts)
         conf.register_opts(rpc_amqp.amqp_opts)
 
-        connection_pool = rpc_amqp.get_connection_pool(conf, url, Connection)
+        connection_pool = rpc_amqp.ConnectionPool(conf, url, Connection)
 
         super(QpidDriver, self).__init__(conf, url,
                                          connection_pool,
diff --git a/oslo/messaging/_drivers/impl_rabbit.py b/oslo/messaging/_drivers/impl_rabbit.py
index 0c786ed7b..2f00d4f65 100644
--- a/oslo/messaging/_drivers/impl_rabbit.py
+++ b/oslo/messaging/_drivers/impl_rabbit.py
@@ -749,7 +749,7 @@ class RabbitDriver(amqpdriver.AMQPDriverBase):
         conf.register_opts(rabbit_opts)
         conf.register_opts(rpc_amqp.amqp_opts)
 
-        connection_pool = rpc_amqp.get_connection_pool(conf, url, Connection)
+        connection_pool = rpc_amqp.ConnectionPool(conf, url, Connection)
 
         super(RabbitDriver, self).__init__(conf, url,
                                            connection_pool,