Browse Source

Fix KeystoneMiddleware memcachepool abstraction

Keystonemiddleware's abstraction for the memcache pool was broken
when converting to use a queue.Queue. The logic that placed the
connection back into the pool was moved to .acquire and the reserve
method was not using acquire.

Change-Id: I0eda5981cbb661f63790258cf8e70c7340615159
Closes-Bug: #1782404
Morgan Fainberg 9 months ago
parent
commit
c46f29278d

+ 5
- 1
keystonemiddleware/auth_token/_cache.py View File

@@ -98,7 +98,11 @@ class _MemcacheClientPool(object):
98 98
 
99 99
     @contextlib.contextmanager
100 100
     def reserve(self):
101
-        yield self._pool.get()
101
+        # NOTE(morgan): We must use "acquire" if we want all the added context
102
+        # manager logic that places the connection back into the pool at the
103
+        # end of it's use.
104
+        with self._pool.acquire() as client:
105
+            yield client
102 106
 
103 107
 
104 108
 class TokenCache(object):

+ 23
- 0
keystonemiddleware/tests/unit/auth_token/test_cache.py View File

@@ -12,8 +12,10 @@
12 12
 
13 13
 import uuid
14 14
 
15
+import fixtures
15 16
 import six
16 17
 
18
+from keystonemiddleware.auth_token import _cache
17 19
 from keystonemiddleware.auth_token import _exceptions as exc
18 20
 from keystonemiddleware.tests.unit.auth_token import base
19 21
 from keystonemiddleware.tests.unit import utils
@@ -165,3 +167,24 @@ class TestLiveMemcache(base.BaseAuthTokenTestCase):
165 167
 
166 168
         token_cache.set(token, data)
167 169
         self.assertEqual(token_cache.get(token), data)
170
+
171
+
172
+class TestMemcachePoolAbstraction(utils.TestCase):
173
+    def setUp(self):
174
+        super(TestMemcachePoolAbstraction, self).setUp()
175
+        self.useFixture(fixtures.MockPatch(
176
+            'oslo_cache._memcache_pool._MemcacheClient'))
177
+
178
+    def test_abstraction_layer_reserve_places_connection_back_in_pool(self):
179
+        cache_pool = _cache._MemcacheClientPool(
180
+            memcache_servers=[], arguments={}, maxsize=1, unused_timeout=10)
181
+        conn = None
182
+        with cache_pool.reserve() as client:
183
+            self.assertEqual(cache_pool._pool._acquired, 1)
184
+            conn = client
185
+
186
+        self.assertEqual(cache_pool._pool._acquired, 0)
187
+        with cache_pool.reserve() as client:
188
+            # Make sure the connection we got before is in-fact the one we
189
+            # get again.
190
+            self.assertEqual(conn, client)

+ 8
- 0
releasenotes/notes/bug-1782404-c4e37bbc83756a89.yaml View File

@@ -0,0 +1,8 @@
1
+---
2
+fixes:
3
+  - >
4
+    [`bug 1782404 <https://bugs.launchpad.net/keystonemiddleware/+bug/1782404>`_]
5
+    Keystonemiddleware incorrectly implemented an abstraction for the memcache
6
+    client pool that utilized a `queue.Queue` `get` method instead of the
7
+    supplied `acquire()` context manager. The `acquire()` context manager
8
+    properly places the client connection back into the pool after `__exit__`.

Loading…
Cancel
Save