Revision 35d42381 snf-pithos-app/pithos/api/util.py

b/snf-pithos-app/pithos/api/util.py
788 788
    if request.serialization == 'json':
789 789
        return json.dumps(l)
790 790

  
791
def get_backend():
791

  
792
def _get_backend():
792 793
    backend = connect_backend(db_module=BACKEND_DB_MODULE,
793 794
                              db_connection=BACKEND_DB_CONNECTION,
794 795
                              block_module=BACKEND_BLOCK_MODULE,
......
800 801
    backend.default_policy['versioning'] = BACKEND_VERSIONING
801 802
    return backend
802 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

  
812
USAGE_LIMIT = 500
813
POOL_SIZE = 5
814

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

  
825
    def _pool_cleanup(self, backend):
826
        c = backend._use_count - 1
827
        if c < 0:
828
            backend._real_close()
829
            return True
830

  
831
        backend._use_count = c
832
        return False
833

  
834
_pithos_backend_pool = PithosBackendPool(size=POOL_SIZE)
835

  
836

  
837
def get_backend():
838
    return _pithos_backend_pool.pool_get()
839

  
840

  
803 841
def update_request_headers(request):
804 842
    # Handle URL-encoded keys and values.
805 843
    meta = dict([(k, v) for k, v in request.META.iteritems() if k.startswith('HTTP_')])
......
870 908
    
871 909
    return 'text'
872 910

  
911

  
873 912
def api_method(http_method=None, format_allowed=False, user_required=True):
874 913
    """Decorator function for views that implement an API method."""
875 914
    

Also available in: Unified diff