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

Source Code for Module restkit.pool.monitored

  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 socket 
  7  import time 
  8   
  9  from restkit.pool.base import BasePool 
 10  from restkit.util import sock 
 11   
12 -class MonitoredHost(object):
13
14 - def __init__(self, keepalive, timeout):
15 self.keepalive = keepalive 16 self.timeout = timeout 17 self.nb_connections = 0 18 self.alive = dict() 19 self.init_pool()
20
21 - def get(self):
22 while self.nb_connections: 23 self.nb_connections -= 1 24 conn = self.do_get() 25 try: 26 _ = conn.fileno() 27 return conn 28 except socket.error: 29 """ connection probably closed """ 30 continue 31 return None
32
33 - def put(self, conn):
34 if self.nb_connections > self.keepalive and self.waiting(): 35 sock.close(conn) 36 return 37 self.nb_connections += 1 38 self.do_put(conn) 39 self.alive[conn.fileno()] = (conn, self.timeout + time.time())
40
41 - def clear(self):
42 while self.nb_connections: 43 self.nb_connections -= 1 44 conn = self.do_get() 45 sock.close(conn)
46
47 - def murder_connections(self):
48 for fno, connection in self.alive.items(): 49 conn, expires = connection 50 if expires < time.time(): 51 sock.close(conn) 52 self.nb_connections -= 1 53 del self.alive[fno]
54
55 - def init_pool(self):
56 raise NotImplementedError
57
58 - def do_get(self):
59 raise NotImplementedError
60
61 - def do_put(self, conn):
62 raise NotImplementedError
63
64 - def waiting(self):
65 raise NotImplementedError
66 67
68 -class MonitoredPool(BasePool):
69 70 HOST_CLASS = None 71
72 - def __init__(self, keepalive=10, timeout=300):
73 super(MonitoredPool, self).__init__(keepalive=keepalive, 74 timeout=timeout) 75 self._hosts = {} 76 self.start()
77
78 - def start(self):
79 raise NotImplementedError
80
81 - def get(self, netloc):
82 if netloc not in self._hosts: 83 return 84 host = self._hosts[netloc] 85 return host.get()
86
87 - def put(self, netloc, conn):
88 if netloc in self._hosts: 89 host = self._hosts[netloc] 90 else: 91 host = self.HOST_CLASS(self.keepalive, self.timeout) 92 self._hosts[netloc] = host 93 host.put(conn)
94
95 - def clear_host(self, netloc):
96 if netloc not in self._hosts: 97 return 98 host = self._hosts[netloc] 99 host.clear() 100 del self._hosts[netloc]
101
102 - def clear(self):
103 for netloc, host in self._hosts.items(): 104 host.clear() 105 del self._hosts[netloc]
106