Revision edb7875c

b/snf-pithos-app/pithos/api/util.py
789 789
        return json.dumps(l)
790 790

  
791 791

  
792
def _get_backend():
793
    backend = connect_backend(db_module=BACKEND_DB_MODULE,
794
                              db_connection=BACKEND_DB_CONNECTION,
795
                              block_module=BACKEND_BLOCK_MODULE,
796
                              block_path=BACKEND_BLOCK_PATH,
797
                              block_umask=BACKEND_BLOCK_UMASK,
798
                              queue_module=BACKEND_QUEUE_MODULE,
799
                              queue_connection=BACKEND_QUEUE_CONNECTION)
800
    backend.default_policy['quota'] = BACKEND_QUOTA
801
    backend.default_policy['versioning'] = BACKEND_VERSIONING
802
    return backend
803

  
804

  
805
def _pooled_backend_close(backend):
806
    backend._pool.pool_put(backend)
807

  
808

  
809
from synnefo.lib.pool import ObjectPool
810
from new import instancemethod
811
from select import select
812
from traceback import print_exc
813

  
814
USAGE_LIMIT = 500
792
from pithos.backends.util import PithosBackendPool
815 793
POOL_SIZE = 5
816 794

  
817
class PithosBackendPool(ObjectPool):
818
    def _pool_create(self):
819
        backend = _get_backend()
820
        backend._real_close = backend.close
821
        backend.close = instancemethod(_pooled_backend_close, backend,
822
                                       type(backend))
823
        backend._pool = self
824
        backend._use_count = USAGE_LIMIT
825
        return backend
826

  
827
    def _pool_verify(self, backend):
828
        wrapper = backend.wrapper
829
        conn = wrapper.conn
830
        if conn.closed:
831
            return False
832

  
833
        if conn.in_transaction():
834
            conn.close()
835
            return False
836 795

  
837
        try:
838
            fd = conn.connection.connection.fileno()
839
            r, w, x = select([fd], (), (), 0)
840
            if r:
841
                conn.close()
842
                return False
843
        except:
844
            print_exc()
845
            return False
846

  
847
        return True
848

  
849
    def _pool_cleanup(self, backend):
850
        c = backend._use_count - 1
851
        if c < 0:
852
            backend._real_close()
853
            return True
854

  
855
        backend._use_count = c
856
        wrapper = backend.wrapper
857
        if wrapper.trans is not None:
858
            conn = wrapper.conn
859
            if conn.closed:
860
                wrapper.trans = None
861
            else:
862
                wrapper.rollback()
863
        if backend.messages:
864
            backend.messages = []
865
        return False
866

  
867
_pithos_backend_pool = PithosBackendPool(size=POOL_SIZE)
796
_pithos_backend_pool = PithosBackendPool(size=POOL_SIZE,
797
                                         db_module=BACKEND_DB_MODULE,
798
                                         db_connection=BACKEND_DB_CONNECTION,
799
                                         block_module=BACKEND_BLOCK_MODULE,
800
                                         block_path=BACKEND_BLOCK_PATH,
801
                                         block_umask=BACKEND_BLOCK_UMASK,
802
                                         queue_module=BACKEND_QUEUE_MODULE,
803
                                         queue_connection=BACKEND_QUEUE_CONNECTION)
868 804

  
869 805

  
870 806
def get_backend():
871
    return _pithos_backend_pool.pool_get()
807
    backend = _pithos_backend_pool.pool_get()
808
    backend.default_policy['quota'] = BACKEND_QUOTA
809
    backend.default_policy['versioning'] = BACKEND_VERSIONING
810
    return backend
872 811

  
873 812

  
874 813
def update_request_headers(request):
b/snf-pithos-backend/pithos/backends/util.py
1
# Copyright 2011-2012 GRNET S.A. All rights reserved.
2
#
3
# Redistribution and use in source and binary forms, with or
4
# without modification, are permitted provided that the following
5
# conditions are met:
6
#
7
#   1. Redistributions of source code must retain the above
8
#      copyright notice, this list of conditions and the following
9
#      disclaimer.
10
#
11
#   2. Redistributions in binary form must reproduce the above
12
#      copyright notice, this list of conditions and the following
13
#      disclaimer in the documentation and/or other materials
14
#      provided with the distribution.
15
#
16
# THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS
17
# OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19
# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR
20
# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
23
# USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
24
# AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
26
# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27
# POSSIBILITY OF SUCH DAMAGE.
28
#
29
# The views and conclusions contained in the software and
30
# documentation are those of the authors and should not be
31
# interpreted as representing official policies, either expressed
32
# or implied, of GRNET S.A.
33

  
34
from synnefo.lib.pool import ObjectPool
35
from new import instancemethod
36
from select import select
37
from traceback import print_exc
38
from pithos.backends import connect_backend
39

  
40
USAGE_LIMIT = 500
41

  
42

  
43
class PithosBackendPool(ObjectPool):
44
    def __init__(self, size=None, db_module=None, db_connection=None,
45
                 block_module=None, block_path=None, block_umask=None,
46
                 queue_module=None, queue_connection=None):
47
        super(PithosBackendPool, self).__init__(size=size)
48
        self.db_module = db_module
49
        self.db_connection = db_connection
50
        self.block_module = block_module
51
        self.block_path = block_path
52
        self.block_umask = block_umask
53
        self.queue_module = queue_module
54
        self.queue_connection = queue_connection
55

  
56
    def _pool_create(self):
57
        backend = connect_backend(db_module=self.db_module,
58
                                  db_connection=self.db_connection,
59
                                  block_module=self.block_module,
60
                                  block_path=self.block_path,
61
                                  block_umask=self.block_umask,
62
                                  queue_module=self.queue_module,
63
                                  queue_connection=self.queue_connection)
64

  
65
        backend._real_close = backend.close
66
        backend.close = instancemethod(_pooled_backend_close, backend,
67
                                       type(backend))
68
        backend._pool = self
69
        backend._use_count = USAGE_LIMIT
70
        return backend
71

  
72
    def _pool_verify(self, backend):
73
        wrapper = backend.wrapper
74
        conn = wrapper.conn
75
        if conn.closed:
76
            return False
77

  
78
        if conn.in_transaction():
79
            conn.close()
80
            return False
81

  
82
        try:
83
            fd = conn.connection.connection.fileno()
84
            r, w, x = select([fd], (), (), 0)
85
            if r:
86
                conn.close()
87
                return False
88
        except:
89
            print_exc()
90
            return False
91

  
92
        return True
93

  
94
    def _pool_cleanup(self, backend):
95
        c = backend._use_count - 1
96
        if c < 0:
97
            backend._real_close()
98
            return True
99

  
100
        backend._use_count = c
101
        wrapper = backend.wrapper
102
        if wrapper.trans is not None:
103
            conn = wrapper.conn
104
            if conn.closed:
105
                wrapper.trans = None
106
            else:
107
                wrapper.rollback()
108
        if backend.messages:
109
            backend.messages = []
110
        return False
111

  
112

  
113
def _pooled_backend_close(backend):
114
    backend._pool.pool_put(backend)

Also available in: Unified diff