Move PithosBackendPool to snf-pithos-backend
authorChristos Stavrakakis <cstavr@grnet.gr>
Fri, 5 Oct 2012 10:19:31 +0000 (13:19 +0300)
committerChristos Stavrakakis <cstavr@grnet.gr>
Fri, 5 Oct 2012 10:19:31 +0000 (13:19 +0300)
Move PithosBackendPool from snf-pithos-app to snf-pithos-backend

snf-pithos-app/pithos/api/util.py
snf-pithos-backend/pithos/backends/util.py [new file with mode: 0644]

index 710ee30..be785f9 100644 (file)
@@ -789,86 +789,25 @@ def simple_list_response(request, l):
         return json.dumps(l)
 
 
-def _get_backend():
-    backend = connect_backend(db_module=BACKEND_DB_MODULE,
-                              db_connection=BACKEND_DB_CONNECTION,
-                              block_module=BACKEND_BLOCK_MODULE,
-                              block_path=BACKEND_BLOCK_PATH,
-                              block_umask=BACKEND_BLOCK_UMASK,
-                              queue_module=BACKEND_QUEUE_MODULE,
-                              queue_connection=BACKEND_QUEUE_CONNECTION)
-    backend.default_policy['quota'] = BACKEND_QUOTA
-    backend.default_policy['versioning'] = BACKEND_VERSIONING
-    return backend
-
-
-def _pooled_backend_close(backend):
-    backend._pool.pool_put(backend)
-
-
-from synnefo.lib.pool import ObjectPool
-from new import instancemethod
-from select import select
-from traceback import print_exc
-
-USAGE_LIMIT = 500
+from pithos.backends.util import PithosBackendPool
 POOL_SIZE = 5
 
-class PithosBackendPool(ObjectPool):
-    def _pool_create(self):
-        backend = _get_backend()
-        backend._real_close = backend.close
-        backend.close = instancemethod(_pooled_backend_close, backend,
-                                       type(backend))
-        backend._pool = self
-        backend._use_count = USAGE_LIMIT
-        return backend
-
-    def _pool_verify(self, backend):
-        wrapper = backend.wrapper
-        conn = wrapper.conn
-        if conn.closed:
-            return False
-
-        if conn.in_transaction():
-            conn.close()
-            return False
 
-        try:
-            fd = conn.connection.connection.fileno()
-            r, w, x = select([fd], (), (), 0)
-            if r:
-                conn.close()
-                return False
-        except:
-            print_exc()
-            return False
-
-        return True
-
-    def _pool_cleanup(self, backend):
-        c = backend._use_count - 1
-        if c < 0:
-            backend._real_close()
-            return True
-
-        backend._use_count = c
-        wrapper = backend.wrapper
-        if wrapper.trans is not None:
-            conn = wrapper.conn
-            if conn.closed:
-                wrapper.trans = None
-            else:
-                wrapper.rollback()
-        if backend.messages:
-            backend.messages = []
-        return False
-
-_pithos_backend_pool = PithosBackendPool(size=POOL_SIZE)
+_pithos_backend_pool = PithosBackendPool(size=POOL_SIZE,
+                                         db_module=BACKEND_DB_MODULE,
+                                         db_connection=BACKEND_DB_CONNECTION,
+                                         block_module=BACKEND_BLOCK_MODULE,
+                                         block_path=BACKEND_BLOCK_PATH,
+                                         block_umask=BACKEND_BLOCK_UMASK,
+                                         queue_module=BACKEND_QUEUE_MODULE,
+                                         queue_connection=BACKEND_QUEUE_CONNECTION)
 
 
 def get_backend():
-    return _pithos_backend_pool.pool_get()
+    backend = _pithos_backend_pool.pool_get()
+    backend.default_policy['quota'] = BACKEND_QUOTA
+    backend.default_policy['versioning'] = BACKEND_VERSIONING
+    return backend
 
 
 def update_request_headers(request):
diff --git a/snf-pithos-backend/pithos/backends/util.py b/snf-pithos-backend/pithos/backends/util.py
new file mode 100644 (file)
index 0000000..e8d9837
--- /dev/null
@@ -0,0 +1,114 @@
+# Copyright 2011-2012 GRNET S.A. All rights reserved.
+#
+# Redistribution and use in source and binary forms, with or
+# without modification, are permitted provided that the following
+# conditions are met:
+#
+#   1. Redistributions of source code must retain the above
+#      copyright notice, this list of conditions and the following
+#      disclaimer.
+#
+#   2. Redistributions in binary form must reproduce the above
+#      copyright notice, this list of conditions and the following
+#      disclaimer in the documentation and/or other materials
+#      provided with the distribution.
+#
+# THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS
+# OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR
+# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
+# USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
+# AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
+# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+# POSSIBILITY OF SUCH DAMAGE.
+#
+# The views and conclusions contained in the software and
+# documentation are those of the authors and should not be
+# interpreted as representing official policies, either expressed
+# or implied, of GRNET S.A.
+
+from synnefo.lib.pool import ObjectPool
+from new import instancemethod
+from select import select
+from traceback import print_exc
+from pithos.backends import connect_backend
+
+USAGE_LIMIT = 500
+
+
+class PithosBackendPool(ObjectPool):
+    def __init__(self, size=None, db_module=None, db_connection=None,
+                 block_module=None, block_path=None, block_umask=None,
+                 queue_module=None, queue_connection=None):
+        super(PithosBackendPool, self).__init__(size=size)
+        self.db_module = db_module
+        self.db_connection = db_connection
+        self.block_module = block_module
+        self.block_path = block_path
+        self.block_umask = block_umask
+        self.queue_module = queue_module
+        self.queue_connection = queue_connection
+
+    def _pool_create(self):
+        backend = connect_backend(db_module=self.db_module,
+                                  db_connection=self.db_connection,
+                                  block_module=self.block_module,
+                                  block_path=self.block_path,
+                                  block_umask=self.block_umask,
+                                  queue_module=self.queue_module,
+                                  queue_connection=self.queue_connection)
+
+        backend._real_close = backend.close
+        backend.close = instancemethod(_pooled_backend_close, backend,
+                                       type(backend))
+        backend._pool = self
+        backend._use_count = USAGE_LIMIT
+        return backend
+
+    def _pool_verify(self, backend):
+        wrapper = backend.wrapper
+        conn = wrapper.conn
+        if conn.closed:
+            return False
+
+        if conn.in_transaction():
+            conn.close()
+            return False
+
+        try:
+            fd = conn.connection.connection.fileno()
+            r, w, x = select([fd], (), (), 0)
+            if r:
+                conn.close()
+                return False
+        except:
+            print_exc()
+            return False
+
+        return True
+
+    def _pool_cleanup(self, backend):
+        c = backend._use_count - 1
+        if c < 0:
+            backend._real_close()
+            return True
+
+        backend._use_count = c
+        wrapper = backend.wrapper
+        if wrapper.trans is not None:
+            conn = wrapper.conn
+            if conn.closed:
+                wrapper.trans = None
+            else:
+                wrapper.rollback()
+        if backend.messages:
+            backend.messages = []
+        return False
+
+
+def _pooled_backend_close(backend):
+    backend._pool.pool_put(backend)