CMS 3D CMS Logo

List of all members | Public Member Functions | Public Attributes | Private Member Functions
HTTP.RequestManager Class Reference

Public Member Functions

def __init__ (self, num_connections=10, ssl_opts=None, user_agent=None, request_headers=None, request_init=None, request_respond=None, request_error=None, handle_init=None)
 
def process (self)
 
def put (self, task)
 

Public Attributes

 cm
 
 free
 
 handles
 
 queue
 
 request_error
 
 request_init
 
 request_respond
 

Private Member Functions

def _request_error (self, c, task, errmsg, errno)
 
def _request_init (self, c, url)
 
def _request_respond (self, args)
 

Detailed Description

Manager of multiple concurrent or overlapping HTTP requests.

This is a utility class acting as a pump of several overlapping
HTTP requests against any number of HTTP or HTTPS servers. It
uses a configurable number of simultaneous connections, ten by
default. The actual connection layer is handled using curl, and
the client classes need to aware of this to a limited degree.

The client supplies optional callback methods for initialising,
responding and handling errors on connections. At the very least
the request response callback should be defined.

This class is not designed for multi-threaded use. It employs
overlapping requests, but in a single thread. Only one thread
at a time should be calling `process()`; several threads may
call `.put()` provided the caller uses a mutex so that only one
thread calls into the method at a time.

Definition at line 6 of file HTTP.py.

Constructor & Destructor Documentation

◆ __init__()

def HTTP.RequestManager.__init__ (   self,
  num_connections = 10,
  ssl_opts = None,
  user_agent = None,
  request_headers = None,
  request_init = None,
  request_respond = None,
  request_error = None,
  handle_init = None 
)
Initialise the request manager. The arguments are:

:arg num_connections: maximum number of simultaneous connections.
:arg ssl_opts: optional SSLOptions (Monitoring.Core.X509) for SSL
X509 parametre values, e.g. for X509 client authentication.
:arg user_agent: sets user agent identification string if defined.
:arg request_headers: if defined, specifies list of additional HTTP
request headers to be added to each request.
:arg request_init: optional callback to initialise requests; the
default assumes each task is a URL to access and sets the `URL`
property on the curl object to the task value.
:arg request_respond: callback for handling responses; at the very
minimum this should be defined as the default one does nothing.
:arg request_error: callback for handling connection errors; the
default one raises a RuntimeException.
:arg handle_init: callback for customising connection handles at
creation time; the callback will be invoked for each connection
object as it's created and queued to the idle connection list.

Definition at line 28 of file HTTP.py.

28  request_error = None, handle_init = None):
29  """Initialise the request manager. The arguments are:
30 
31 :arg num_connections: maximum number of simultaneous connections.
32 :arg ssl_opts: optional SSLOptions (Monitoring.Core.X509) for SSL
33 X509 parametre values, e.g. for X509 client authentication.
34 :arg user_agent: sets user agent identification string if defined.
35 :arg request_headers: if defined, specifies list of additional HTTP
36 request headers to be added to each request.
37 :arg request_init: optional callback to initialise requests; the
38 default assumes each task is a URL to access and sets the `URL`
39 property on the curl object to the task value.
40 :arg request_respond: callback for handling responses; at the very
41 minimum this should be defined as the default one does nothing.
42 :arg request_error: callback for handling connection errors; the
43 default one raises a RuntimeException.
44 :arg handle_init: callback for customising connection handles at
45 creation time; the callback will be invoked for each connection
46 object as it's created and queued to the idle connection list."""
47  self.request_respond = request_respond or self._request_respond
48  self.request_error = request_error or self._request_error
49  self.request_init = request_init or self._request_init
50  self.cm = CurlMulti()
51  self.handles = [Curl() for i in range(0, num_connections)]
52  self.free = [c for c in self.handles]
53  self.queue = []
54 
55  for c in self.handles:
56  c.buffer = None
57  c.setopt(NOSIGNAL, 1)
58  c.setopt(TIMEOUT, 300)
59  c.setopt(CONNECTTIMEOUT, 30)
60  c.setopt(FOLLOWLOCATION, 1)
61  c.setopt(MAXREDIRS, 5)
62  if user_agent:
63  c.setopt(USERAGENT, user_agent)
64  if ssl_opts:
65  c.setopt(CAPATH, ssl_opts.ca_path)
66  c.setopt(SSLCERT, ssl_opts.cert_file)
67  c.setopt(SSLKEY, ssl_opts.key_file)
68  if ssl_opts.key_pass:
69  c.setopt(SSLKEYPASSWD, ssl_opts.key_pass)
70  if request_headers:
71  c.setopt(HTTPHEADER, request_headers)
72  if handle_init:
73  handle_init(c)
74 

Member Function Documentation

◆ _request_error()

def HTTP.RequestManager._request_error (   self,
  c,
  task,
  errmsg,
  errno 
)
private
Default request error callback.

Definition at line 79 of file HTTP.py.

79  def _request_error(self, c, task, errmsg, errno):
80  """Default request error callback."""
81  raise RuntimeError((task, errmsg, errno))
82 

◆ _request_init()

def HTTP.RequestManager._request_init (   self,
  c,
  url 
)
private
Default request initialisation callback.

Definition at line 75 of file HTTP.py.

75  def _request_init(self, c, url):
76  """Default request initialisation callback."""
77  c.setopt(URL, url)
78 

◆ _request_respond()

def HTTP.RequestManager._request_respond (   self,
  args 
)
private
Default request response callback.

Definition at line 83 of file HTTP.py.

83  def _request_respond(self, *args):
84  """Default request response callback."""
85  pass
86 

◆ process()

def HTTP.RequestManager.process (   self)
Process pending requests until none are left.

This method processes all requests queued with `.put()` until they
have been fully processed. It calls the ``request_respond`` callback
for all successfully completed requests, and ``request_error`` for
all failed ones.

Any new requests added by callbacks by invoking ``put()`` are also
processed before returning.

Definition at line 92 of file HTTP.py.

References mps_setup.append, HTTP.RequestManager.cm, HTTP.RequestManager.free, HTTP.RequestManager.queue, HTTP.RequestManager.request_error, HTTP.RequestManager.request_init, HTTP.RequestManager.request_respond, and singleTopDQM_cfi.select.

92  def process(self):
93  """Process pending requests until none are left.
94 
95 This method processes all requests queued with `.put()` until they
96 have been fully processed. It calls the ``request_respond`` callback
97 for all successfully completed requests, and ``request_error`` for
98 all failed ones.
99 
100 Any new requests added by callbacks by invoking ``put()`` are also
101 processed before returning."""
102  npending = 0
103  while self.queue or npending:
104  while self.queue and self.free:
105  c = self.free.pop()
106  c.task = self.queue.pop(0)
107  c.buffer = b = BytesIO()
108  c.setopt(WRITEFUNCTION, b.write)
109  self.request_init(c, *c.task)
110  self.cm.add_handle(c)
111  npending += 1
112 
113  while True:
114  ret, nhandles = self.cm.perform()
115  if ret != E_CALL_MULTI_PERFORM:
116  break
117 
118  while True:
119  numq, ok, err = self.cm.info_read()
120 
121  for c in ok:
122  assert npending > 0
123  self.cm.remove_handle(c)
124  self.request_respond(c)
125  c.buffer = None
126  self.free.append(c)
127  npending -= 1
128 
129  for c, errno, errmsg in err:
130  assert npending > 0
131  self.cm.remove_handle(c)
132  self.free.append(c)
133  npending -= 1
134  self.request_error(c, c.task, errmsg, errno)
135 
136  if numq == 0:
137  break
138 
139  self.cm.select(1.)
140 

◆ put()

def HTTP.RequestManager.put (   self,
  task 
)
Add a new task. The task object should be a tuple and is
passed to ``request_init`` callback passed to the constructor.

Definition at line 87 of file HTTP.py.

87  def put(self, task):
88  """Add a new task. The task object should be a tuple and is
89 passed to ``request_init`` callback passed to the constructor."""
90  self.queue.append(task)
91 
void put(edm::Event &evt, double value, const char *instanceName)

Member Data Documentation

◆ cm

HTTP.RequestManager.cm

Definition at line 50 of file HTTP.py.

Referenced by HTTP.RequestManager.process().

◆ free

HTTP.RequestManager.free

Definition at line 52 of file HTTP.py.

Referenced by HTTP.RequestManager.process().

◆ handles

HTTP.RequestManager.handles

Definition at line 51 of file HTTP.py.

Referenced by SimpleMuonAnalyzer.SimpleMuonAnalyzer.declareHandles(), ZEleEleAnalyzer.ZEleEleAnalyzer.declareHandles(), ZMuMuAnalyzer.ZMuMuAnalyzer.declareHandles(), TriggerAnalyzer.TriggerAnalyzer.declareHandles(), MetAnalyzer.MetAnalyzer.declareHandles(), SimpleJetAnalyzer.SimpleJetAnalyzer.declareHandles(), objects.TauAnalyzer.TauAnalyzer.declareHandles(), core.TriggerBitFilter.TriggerBitFilter.declareHandles(), core.TriggerMatchAnalyzer.TriggerMatchAnalyzer.declareHandles(), core.TriggerBitAnalyzer.TriggerBitAnalyzer.declareHandles(), RazorAnalyzer.RazorAnalyzer.declareHandles(), objects.METAnalyzer.METAnalyzer.declareHandles(), AlphaTAnalyzer.AlphaTAnalyzer.declareHandles(), core.AutoFillTreeProducer.AutoFillTreeProducer.declareHandles(), MT2Analyzer.MT2Analyzer.declareHandles(), objects.PhotonAnalyzer.PhotonAnalyzer.declareHandles(), JetAnalyzer.JetAnalyzer.declareHandles(), objects.VertexAnalyzer.VertexAnalyzer.declareHandles(), objects.IsoTrackAnalyzer.IsoTrackAnalyzer.declareHandles(), core.PileUpAnalyzer.PileUpAnalyzer.declareHandles(), objects.LeptonAnalyzer.LeptonAnalyzer.declareHandles(), objects.JetAnalyzer.JetAnalyzer.declareHandles(), core.AutoFillTreeProducer.AutoFillTreeProducer.fillTree(), objects.LeptonAnalyzer.LeptonAnalyzer.makeAllElectrons(), objects.LeptonAnalyzer.LeptonAnalyzer.makeAllMuons(), objects.IsoTrackAnalyzer.IsoTrackAnalyzer.makeIsoTrack(), objects.LeptonAnalyzer.LeptonAnalyzer.makeLeptons(), objects.PhotonAnalyzer.PhotonAnalyzer.makePhotons(), objects.TauAnalyzer.TauAnalyzer.makeTaus(), objects.METAnalyzer.METAnalyzer.makeTkMETs(), SimpleMuonAnalyzer.SimpleMuonAnalyzer.process(), SimpleJetAnalyzer.SimpleJetAnalyzer.process(), MetAnalyzer.MetAnalyzer.process(), core.TriggerBitFilter.TriggerBitFilter.process(), core.TriggerMatchAnalyzer.TriggerMatchAnalyzer.process(), TriggerAnalyzer.TriggerAnalyzer.process(), JetAnalyzer.JetAnalyzer.process(), core.TriggerBitAnalyzer.TriggerBitAnalyzer.process(), DiLeptonAnalyzer.DiLeptonAnalyzer.process(), core.PileUpAnalyzer.PileUpAnalyzer.process(), objects.VertexAnalyzer.VertexAnalyzer.process(), objects.JetAnalyzer.JetAnalyzer.process(), objects.PhotonAnalyzer.PhotonAnalyzer.randomCone(), and core.Analyzer.Analyzer.readCollections().

◆ queue

HTTP.RequestManager.queue

Definition at line 53 of file HTTP.py.

Referenced by HTTP.RequestManager.process().

◆ request_error

HTTP.RequestManager.request_error

Definition at line 48 of file HTTP.py.

Referenced by HTTP.RequestManager.process().

◆ request_init

HTTP.RequestManager.request_init

Definition at line 49 of file HTTP.py.

Referenced by HTTP.RequestManager.process().

◆ request_respond

HTTP.RequestManager.request_respond

Definition at line 47 of file HTTP.py.

Referenced by HTTP.RequestManager.process().