Package restkit :: Package pool :: Module simple
[hide private]
[frames] | no frames]

Source Code for Module restkit.pool.simple

 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  """ Thread-safe pool """ 
 7  import collections 
 8  import time 
 9   
10  from restkit.pool.base import BasePool 
11  from restkit.util import sock 
12  from restkit.util.rwlock import RWLock 
13   
14 -class Host(object):
15
16 - def __init__(self, keepalive, timeout):
17 self.keepalive = keepalive 18 self.timeout = timeout 19 self.connections = collections.deque()
20
21 - def get(self):
22 if len(self.connections) < self.keepalive: 23 return None 24 25 while len(self.connections): 26 conn, expires = self.connections.popleft() 27 if expires >= time.time(): 28 return conn 29 return None
30
31 - def put(self, conn):
32 if len(self.connections) >= self.keepalive: 33 sock.close(conn) 34 return 35 expires = time.time() + self.timeout 36 self.connections.append((conn, expires))
37
38 - def clear(self):
39 while self.connections: 40 conn, expires = self.connections.popleft() 41 sock.close(conn)
42
43 -class SimplePool(BasePool):
44
45 - def __init__(self, keepalive=10, timeout=300):
46 super(SimplePool, self).__init__(keepalive=keepalive, 47 timeout=timeout) 48 self._hosts = {} 49 self._lock = RWLock()
50
51 - def get(self, netloc):
52 self._lock.reader_enters() 53 try: 54 if netloc not in self._hosts: 55 return 56 host = self._hosts[netloc] 57 conn = host.get() 58 return conn 59 finally: 60 self._lock.reader_leaves()
61
62 - def put(self, netloc, conn):
63 self._lock.writer_enters() 64 try: 65 if netloc not in self._hosts: 66 host = Host(self.keepalive, self.timeout) 67 self._hosts[netloc] = host 68 else: 69 host = self._hosts[netloc] 70 host.put(conn) 71 finally: 72 self._lock.writer_leaves()
73
74 - def clear_host(self, netloc):
75 self._lock.writer_enters() 76 try: 77 if netloc not in self._hosts: 78 return 79 host = self._hosts[netloc] 80 host.clear() 81 finally: 82 self._lock.writer_leaves()
83
84 - def clear(self):
85 self._lock.writer_enters() 86 try: 87 for netloc, host in self._hosts.items(): 88 host.clear() 89 del self._hosts[netloc] 90 finally: 91 self._lock.writer_leaves()
92