Package restkit :: Package conn :: Module base
[hide private]
[frames] | no frames]

Source Code for Module restkit.conn.base

 1  # -*- coding: utf-8 - 
 2  # 
 3  # This file is part of restkit released under the MIT license.  
 4  # See the NOTICE for more information. 
 5   
 6  import threading 
 7   
8 -class Pool(object):
9 """ Connection pull for a given route """ 10
11 - def __init__(self, conn_manageer, route, timeout=300, 12 nb_connections=10):
13 """ constructor for the pool 14 15 :attr conn_manager: instance of ConnectionManager 16 :attr route: tupple (address, is_ssl, filters, ssl_args) 17 where address is a tuple of (host, port) and ssl_args a dict 18 containing ssl arguments 19 :attr timeout: integer, default timeout 20 :attr nb_connections: integeger, number of connections in the 21 pool 22 """
23
24 - def requet(self):
25 """ return a free connection """ 26 raise NotImplementedError
27
28 - def release(self, conn, duration=300):
29 """ release a connection in the pool, and make it available for 30 duration. """ 31 raise NotImplementedError
32
33 - def clean_iddle_duration(self):
34 """ close any connections that haven been used in fixed duration 35 """ 36 raise NotImplementedError
37
38 - def shutdown(self):
39 """ close all connections in the pool """ 40 raise NotImplementedError
41
42 -class ConnectionManager(object):
43 """ maintain all connections pools. By default a pool have 10 44 connections """ 45 46 POOL_CLASS = None 47
48 - def __init__(self, timeout=300, nb_connections=10):
49 """ constructor for the manager 50 51 :attr timeout: integer, default timeout 52 :attr nb_connections: integeger, number of connections in the 53 pool 54 """ 55 self.timeout = timeout 56 self.nb_connections = nb_connections 57 self._connections = {} 58 self._lock = self.init_lock()
59
60 - def init_lock(self):
61 return threading.Lock()
62
63 - def get_key(self, route):
64 key = (route[0], route[1]) 65 return key
66
67 - def get_pool(self, route):
68 """ get a pool for given route 69 70 where address is a tuple of (host, port) and ssl_args a dict 71 containing ssl arguments 72 """ 73 self._lock.acquire() 74 try: 75 key = self.get_key(route) 76 if key not in self._connections: 77 pool = self.POOL_CLASS(self, route, timeout=self.timeout, 78 nb_connections=self.nb_connections) 79 self._connections[key] = pool 80 else: 81 pool = self._connections[key] 82 return pool 83 finally: 84 self._lock.release()
85