1 from cStringIO
import StringIO
5 """Manager of multiple concurrent or overlapping HTTP requests.
7 This is a utility class acting as a pump of several overlapping
8 HTTP requests against any number of HTTP or HTTPS servers. It
9 uses a configurable number of simultaneous connections, ten by
10 default. The actual connection layer is handled using curl, and
11 the client classes need to aware of this to a limited degree.
13 The client supplies optional callback methods for initialising,
14 responding and handling errors on connections. At the very least
15 the request response callback should be defined.
17 This class is not designed for multi-threaded use. It employs
18 overlapping requests, but in a single thread. Only one thread
19 at a time should be calling `process()`; several threads may
20 call `.put()` provided the caller uses a mutex so that only one
21 thread calls into the method at a time."""
23 def __init__(self, num_connections = 10, ssl_opts = None,
24 user_agent =
None, request_headers =
None,
25 request_init =
None, request_respond =
None,
26 request_error =
None, handle_init =
None):
27 """Initialise the request manager. The arguments are:
29 :arg num_connections: maximum number of simultaneous connections.
30 :arg ssl_opts: optional SSLOptions (Monitoring.Core.X509) for SSL
31 X509 parametre values, e.g. for X509 client authentication.
32 :arg user_agent: sets user agent identification string if defined.
33 :arg request_headers: if defined, specifies list of additional HTTP
34 request headers to be added to each request.
35 :arg request_init: optional callback to initialise requests; the
36 default assumes each task is a URL to access and sets the `URL`
37 property on the curl object to the task value.
38 :arg request_respond: callback for handling responses; at the very
39 minimum this should be defined as the default one does nothing.
40 :arg request_error: callback for handling connection errors; the
41 default one raises a RuntimeException.
42 :arg handle_init: callback for customising connection handles at
43 creation time; the callback will be invoked for each connection
44 object as it's created and queued to the idle connection list."""
49 self.
handles = [Curl()
for i
in xrange(0, num_connections)]
56 c.setopt(TIMEOUT, 300)
57 c.setopt(CONNECTTIMEOUT, 30)
58 c.setopt(FOLLOWLOCATION, 1)
59 c.setopt(MAXREDIRS, 5)
61 c.setopt(USERAGENT, user_agent)
63 c.setopt(CAPATH, ssl_opts.ca_path)
64 c.setopt(SSLCERT, ssl_opts.cert_file)
65 c.setopt(SSLKEY, ssl_opts.key_file)
67 c.setopt(SSLKEYPASSWD, ssl_opts.key_pass)
69 c.setopt(HTTPHEADER, request_headers)
74 """Default request initialisation callback."""
78 """Default request error callback."""
79 raise RuntimeError((task, errmsg, errno))
82 """Default request response callback."""
86 """Add a new task. The task object should be a tuple and is
87 passed to ``request_init`` callback passed to the constructor."""
88 self.queue.append(task)
91 """Process pending requests until none are left.
93 This method processes all requests queued with `.put()` until they
94 have been fully processed. It calls the ``request_respond`` callback
95 for all successfully completed requests, and ``request_error`` for
98 Any new requests added by callbacks by invoking ``put()`` are also
99 processed before returning."""
101 while self.
queue or npending:
104 c.task = self.queue.pop(0)
105 c.buffer = b = StringIO()
106 c.setopt(WRITEFUNCTION, b.write)
108 self.cm.add_handle(c)
112 ret, nhandles = self.cm.perform()
113 if ret != E_CALL_MULTI_PERFORM:
117 numq, ok, err = self.cm.info_read()
121 self.cm.remove_handle(c)
127 for c, errno, errmsg
in err:
129 self.cm.remove_handle(c)