Package restkit :: Package conn :: Module gevent_manager
[hide private]
[frames] | no frames]

Source Code for Module restkit.conn.gevent_manager

 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 time 
 7   
 8  from restkit.conn.base import Pool, ConnectionManager 
 9  from restkit.conn import http_connection 
10   
11  from gevent import coros 
12  from gevent import queue 
13  from gevent import socket 
14  from gevent import ssl 
15   
16 -class GeventHttpConnection(http_connection.HttpConnection):
17
18 - def connect(self):
19 self.filters.apply("on_connect", self) 20 21 self.sock = socket.create_connection(self.addr, 22 timeout=self.timeout) 23 if self.is_ssl: 24 self.sock = ssl.SSLSocket(self.sock, **self.ssl_args)
25 26
27 -class GeventPool(Pool):
28
29 - def __init__(self, conn_manager, route, timeout=300, 30 nb_connections=10):
31 self.conn_manager = conn_manager 32 self.route = route 33 self.nb_connections = nb_connections 34 self.timeout = timeout 35 self.connections = queue.PriorityQueue() 36 self.iddle_connections = {} 37 self.active_connections = {}
38
39 - def request(self):
40 self.clean_iddle_connections() 41 try: 42 expires, conn = self.connections.get_nowait() 43 except queue.Empty: 44 conn = GeventHttpConnection( 45 self.conn_manager, 46 self.route[0], 47 self.route[1], 48 timeout=self.timeout, 49 filters=self.route[2], 50 **self.route[3]) 51 return conn
52
53 - def release(self, conn, duration=300):
54 if self.connections.qsize() >= self.nb_connecions: 55 conn.close() 56 return 57 expires = time.time() + duration 58 self.connections.put_nowait((expires, conn))
59
60 - def clean_iddle_connections(self):
61 while True: 62 try: 63 expires, conn = self.connections.get_nowait() 64 except queue.Empty: 65 break 66 if time.time() > expires: 67 conn.close() 68 else: 69 self.connections.put((expires, conn)) 70 break
71
72 - def shutdown(self):
73 while True: 74 try: 75 expires, conn = self.connections.get_nowait() 76 conn.close() 77 except queue.Empty: 78 break
79 80
81 -class GeventConnectionManager(ConnectionManager):
82 83 POOL_CLASS = GeventPool 84
85 - def init_lock(self):
86 return coros.Semaphore(1)
87