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 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
45 while self.nb_connections:
46 self.nb_connections -= 1
47 conn = self.do_get()
48 sock.close(conn)
49
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
59 raise NotImplementedError
60
62 raise NotImplementedError
63
65 raise NotImplementedError
66
68 raise NotImplementedError
69
70
72
73 HOST_CLASS = None
74
75 - def __init__(self, keepalive=10, timeout=300):
80
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
99 if netloc not in self._hosts:
100 return
101 host = self._hosts[netloc]
102 host.clear()
103 del self._hosts[netloc]
104
106 for netloc, host in self._hosts.items():
107 host.clear()
108 del self._hosts[netloc]
109