Change in python/osmo-python-tests[master]: ctrl2cgi: properly limit number of requests

This is merely a historical archive of years 2008-2021, before the migration to mailman3.

A maintained and still updated list archive can be found at https://lists.osmocom.org/hyperkitty/list/gerrit-log@lists.osmocom.org/.

Max gerrit-no-reply at lists.osmocom.org
Wed Dec 5 14:48:47 UTC 2018


Max has uploaded this change for review. ( https://gerrit.osmocom.org/12139


Change subject: ctrl2cgi: properly limit number of requests
......................................................................

ctrl2cgi: properly limit number of requests

Manual acquire()/release() of semaphore does not limit number of
concurrent requests when combined with explicit yield. Fix this by using
semaphore.run() and removing inilineCallbacks decorator.

Change-Id: I47b8b9f5b726ca0905bb7c023d63b325c7f7d85f
Related: SYS#4399
---
M scripts/ctrl2cgi.py
1 file changed, 9 insertions(+), 8 deletions(-)



  git pull ssh://gerrit.osmocom.org:29418/python/osmo-python-tests refs/changes/39/12139/1

diff --git a/scripts/ctrl2cgi.py b/scripts/ctrl2cgi.py
index 1d90ee0..676fc11 100755
--- a/scripts/ctrl2cgi.py
+++ b/scripts/ctrl2cgi.py
@@ -60,6 +60,12 @@
     #print('HASH: \nparams="%r"\ninput="%s" \nres="%s"' %(params, input, res))
     return res
 
+def make_async_req(dst, par, f_write, f_log):
+    d = post(dst, par)
+    d.addCallback(collect, partial(handle_reply, f_write, f_log)) # treq's collect helper is handy to get all reply content at once
+    d.addErrback(lambda e: f_log.critical("HTTP POST error %s while trying to register BSC %s on %s" % (e, par['bsc_id'], dst))) # handle HTTP errors
+    return d
+
 class Trap(CTRL):
     """
     TRAP handler (agnostic to factory's client object)
@@ -93,7 +99,6 @@
         self.factory.log.info("Connected to CTRL@%s:%d" % (self.factory.addr_ctrl, self.factory.port_ctrl))
         super(CTRL, self).connectionMade()
 
-    @defer.inlineCallbacks
     def handle_locationstate(self, net, bsc, bts, trx, data):
         """
         Handle location-state TRAP: parse trap content, build CGI Request and use treq's routines to post it while setting up async handlers
@@ -101,13 +106,8 @@
         params = make_params(bsc, data)
         self.factory.log.debug('location-state@%s.%s.%s.%s (%s) => %s' % (net, bsc, bts, trx, params['time_stamp'], data))
         params['h'] = gen_hash(params, self.factory.secret_key)
-        d = post(self.factory.location, params)
-        d.addCallback(collect, partial(handle_reply, self.transport.write, self.factory.log)) # treq's collect helper is handy to get all reply content at once
-        d.addErrback(lambda e, bsc: self.factory.log.critical("HTTP POST error %s while trying to register BSC %s on %s" % (e, bsc, self.factory.location)), bsc) # handle HTTP errors
         # Ensure that we run only limited number of requests in parallel:
-        yield self.factory.semaphore.acquire()
-        yield d # we end up here only if semaphore is available which means it's ok to fire the request without exceeding the limit
-        self.factory.semaphore.release()
+        self.factory.semaphore.run(make_async_req, self.factory.location, params, self.transport.write, self.factory.log)
 
     def handle_notificationrejectionv1(self, net, bsc, bts, trx, data):
         """
@@ -121,7 +121,6 @@
     Store CGI information so TRAP handler can use it for requests
     """
     def __init__(self, proto, log):
-        self.semaphore = defer.DeferredSemaphore(self.num_max_conn)
         self.log = log
         level = self.log.getEffectiveLevel()
         self.log.setLevel(logging.WARNING) # we do not need excessive debug from lower levels
@@ -155,6 +154,8 @@
         T.num_max_conn = config['main'].getint('num_max_conn', T.num_max_conn)
         T.secret_key = config['main'].get('secret_key', T.secret_key)
 
+    T.semaphore = defer.DeferredSemaphore(T.num_max_conn)
+
     log.info("CGI proxy v%s starting with PID %d:" % (__version__, os.getpid()))
     log.info("destination %s (concurrency %d)" % (T.location, T.num_max_conn))
     log.info("connecting to %s:%d..." % (T.addr_ctrl, T.port_ctrl))

-- 
To view, visit https://gerrit.osmocom.org/12139
To unsubscribe, or for help writing mail filters, visit https://gerrit.osmocom.org/settings

Gerrit-Project: python/osmo-python-tests
Gerrit-Branch: master
Gerrit-MessageType: newchange
Gerrit-Change-Id: I47b8b9f5b726ca0905bb7c023d63b325c7f7d85f
Gerrit-Change-Number: 12139
Gerrit-PatchSet: 1
Gerrit-Owner: Max <msuraev at sysmocom.de>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.osmocom.org/pipermail/gerrit-log/attachments/20181205/8b69309b/attachment.htm>


More information about the gerrit-log mailing list