chiark / gitweb /
wip new config
[hippotat.git] / client
diff --git a/client b/client
index 48c4f3f..e5d4e00 100755 (executable)
--- a/client
+++ b/client
@@ -37,8 +37,9 @@ def process_cfg():
   c.routes = cfg.get('virtual','routes')
   c.max_queue_time = cfg.getint(client_cs, 'max_queue_time')
   c.max_batch_up   = cfg.getint(client_cs, 'max_batch_up')
-  c.http_timeout   = cfg.getint(client_cs, 'http_timeout')
   c.http_retry     = cfg.getint(client_cs, 'http_retry')
+  c.http_timeout = (cfg.getint(client_cs, 'http_timeout') +
+                    cfg.getint(client_cs, 'http_timeout_grace'))
 
   process_cfg_ipif(client_cs,
                    (('local', 'client'),
@@ -80,7 +81,7 @@ class ResponseConsumer(GeneralResponseConsumer):
     self._log(DBG.HTTP_CTRL, '__init__')
 
   def dataReceived(self, data):
-    self._log(DBG.HTTP_CTRL, 'dataReceived', d=data)
+    self._log(DBG.HTTP, 'dataReceived', d=data)
     try:
       self._ssd.inputdata(data)
     except Exception as e:
@@ -92,8 +93,9 @@ class ResponseConsumer(GeneralResponseConsumer):
       self.latefailure()
       return
     try:
+      self._log(DBG.HTTP, 'ResponseDone')
       self._ssd.flush()
-      req_fin(req)
+      req_fin(self._req)
     except Exception as e:
       self._handleexception()
 
@@ -101,7 +103,7 @@ class ResponseConsumer(GeneralResponseConsumer):
     self._latefailure(traceback.format_exc())
 
   def _latefailure(self, reason):
-    self._log(DBG.HTTP_CTRL, '_asyncFailure ' + str(reason))
+    self._log(DBG.HTTP_CTRL, '_latefailure ' + str(reason))
     req_err(self._req, reason)
 
 class ErrorResponseConsumer(twisted.internet.protocol.Protocol):
@@ -145,19 +147,30 @@ def req_ok(req, resp):
 def req_err(req, err):
   # called when the Deferred fails, or (if it completes),
   # later, by ResponsConsumer or ErrorResponsConsumer
-  log_debug(DBG.HTTP_CTRL, 'req_err ' + str(err), idof=req)
-  if isinstance(err, twisted.python.failure.Failure):
-    err = err.getTraceback()
-  print(err, file=sys.stderr)
-  outstanding[req] = 'ERR'
-  log_outstanding()
-  reactor.callLater(c.http_retry, (lambda: req_fin(req)))
+  try:
+    log_debug(DBG.HTTP_CTRL, 'req_err ' + str(err), idof=req)
+    if isinstance(err, twisted.python.failure.Failure):
+      err = err.getTraceback()
+    print('[%#x] %s' % (id(req), err), file=sys.stderr)
+    if not isinstance(outstanding[req], int):
+      raise RuntimeError('[%#x] previously %s' % (id(req), outstanding[req]))
+    outstanding[req] = err
+    log_outstanding()
+    reactor.callLater(c.http_retry, partial(req_fin, req))
+  except Exception as e:
+    crash(traceback.format_exc() + '\n----- handling -----\n' + err)
 
 def req_fin(req):
   del outstanding[req]
   log_debug(DBG.HTTP_CTRL, 'req_fin OS=%d' % len(outstanding), idof=req)
   check_outbound()
 
+class Errb:
+  def __init__(self, req):
+    self._req = req
+  def call(self, err):
+    req_err(self._req, err)
+
 def check_outbound():
   global outstanding
 
@@ -181,6 +194,7 @@ def check_outbound():
             str(c.client)             .encode('ascii')    + crlf +
             password                                      + crlf +
             str(c.target_outstanding) .encode('ascii')    + crlf +
+            str(c.http_timeout)       .encode('ascii')    + crlf +
           ((
             b'--b'                                        + crlf +
             b'Content-Type: application/octet-stream'     + crlf +
@@ -211,13 +225,12 @@ def check_outbound():
     outstanding[req] = len(d)
     log_debug(DBG.HTTP_CTRL, 'request OS=%d' % len(outstanding), idof=req, d=d)
     req.addTimeout(c.http_timeout, reactor)
-    req.addCallback((lambda resp: req_ok(req, resp)))
-    req.addErrback((lambda err: req_err(req, err)))
+    req.addCallback(partial(req_ok, req))
+    req.addErrback(partial(req_err, req))
 
   log_outstanding()
 
-common_startup()
-process_cfg()
+common_startup(process_cfg)
 start_client()
 start_ipif(c.ipif_command, outbound)
 check_outbound()