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