Statistics
| Branch: | Tag: | Revision:

root / snf-pithos-backend / pithos / backends / util.py @ d3655326

History | View | Annotate | Download (4.5 kB)

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_hosts=None,
47
                 queue_exchange=None, quotaholder_url=None,
48
                 free_versioning=True):
49
        super(PithosBackendPool, self).__init__(size=size)
50
        self.db_module = db_module
51
        self.db_connection = db_connection
52
        self.block_module = block_module
53
        self.block_path = block_path
54
        self.block_umask = block_umask
55
        self.queue_module = queue_module
56
        self.queue_hosts = queue_hosts
57
        self.queue_exchange = queue_exchange
58
        self.quotaholder_url = quotaholder_url
59
        self.free_versioning = free_versioning
60
    
61
    def _pool_create(self):
62
        backend = connect_backend(db_module=self.db_module,
63
                                  db_connection=self.db_connection,
64
                                  block_module=self.block_module,
65
                                  block_path=self.block_path,
66
                                  block_umask=self.block_umask,
67
                                  queue_module=self.queue_module,
68
                                  queue_hosts=self.queue_hosts,
69
                                  queue_exchange=self.queue_exchange,
70
                                  quotaholder_url=self.quotaholder_url,
71
                                  free_versioning=self.free_versioning)
72

    
73
        backend._real_close = backend.close
74
        backend.close = instancemethod(_pooled_backend_close, backend,
75
                                       type(backend))
76
        backend._pool = self
77
        backend._use_count = USAGE_LIMIT
78
        return backend
79

    
80
    def _pool_verify(self, backend):
81
        wrapper = backend.wrapper
82
        conn = wrapper.conn
83
        if conn.closed:
84
            return False
85

    
86
        if conn.in_transaction():
87
            conn.close()
88
            return False
89

    
90
        try:
91
            fd = conn.connection.connection.fileno()
92
            r, w, x = select([fd], (), (), 0)
93
            if r:
94
                conn.close()
95
                return False
96
        except:
97
            print_exc()
98
            return False
99

    
100
        return True
101

    
102
    def _pool_cleanup(self, backend):
103
        c = backend._use_count - 1
104
        if c < 0:
105
            backend._real_close()
106
            return True
107

    
108
        backend._use_count = c
109
        wrapper = backend.wrapper
110
        if wrapper.trans is not None:
111
            conn = wrapper.conn
112
            if conn.closed:
113
                wrapper.trans = None
114
            else:
115
                wrapper.rollback()
116
        if backend.messages:
117
            backend.messages = []
118
        return False
119

    
120

    
121
def _pooled_backend_close(backend):
122
    backend._pool.pool_put(backend)