1
2
3
4
5
6 import socket
7 import time
8
9 from restkit.pool.base import BasePool
10 from restkit.util import sock
11
13
20
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
42 while self.nb_connections:
43 self.nb_connections -= 1
44 conn = self.do_get()
45 sock.close(conn)
46
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
56 raise NotImplementedError
57
59 raise NotImplementedError
60
62 raise NotImplementedError
63
65 raise NotImplementedError
66
67
69
70 HOST_CLASS = None
71
72 - def __init__(self, keepalive=10, timeout=300):
77
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
96 if netloc not in self._hosts:
97 return
98 host = self._hosts[netloc]
99 host.clear()
100 del self._hosts[netloc]
101
103 for netloc, host in self._hosts.items():
104 host.clear()
105 del self._hosts[netloc]
106