chiark / gitweb /
peerdb/tripe-newpeers.in: Split out a class for a host's resolved names.
[tripe] / peerdb / tripe-newpeers.in
index 0ba9cb61401ff298a37681d0fe5262c2ae07adaa..502492d9673b904c2c990a5536a59a4050d0cba6 100644 (file)
@@ -48,16 +48,44 @@ class CDBFake (object):
   def finish(me):
     pass
 
+class ExpectedError (Exception): pass
+
 ###--------------------------------------------------------------------------
 ### A bulk DNS resolver.
 
-class ResolverFailure (Exception):
+class ResolverFailure (ExpectedError):
   def __init__(me, host, msg):
     me.host = host
     me.msg = msg
   def __str__(me):
     return "failed to resolve `%s': %s" % (me.host, me.msg)
 
+class ResolvingHost (object):
+  """
+  A host name which is being looked up by a bulk-resolver instance.
+  """
+
+  def __init__(me, name):
+    """Make a new resolving-host object for the host NAME."""
+    me.name = name
+    me.addr = None
+    me.failure = None
+
+  def setaddr(me, addr):
+    """Add the address ADDR."""
+    me.addr = addr
+
+  def failed(me, msg):
+    """
+    Report that resolution of this host failed, with a human-readable MSG.
+    """
+    me.failure = msg
+
+  def get(me):
+    """Return the resolved address."""
+    if me.failure is not None: raise ResolverFailure(me.name, me.failure)
+    return me.addr
+
 class BulkResolver (object):
   """
   Resolve a number of DNS names in parallel.
@@ -76,36 +104,35 @@ class BulkResolver (object):
 
   def __init__(me):
     """Initialize the resolver."""
-    me._resolvers = {}
     me._namemap = {}
-
-  def prepare(me, host):
-    """Prime the resolver to resolve the name HOST."""
-    if host not in me._resolvers:
-      me._resolvers[host] = M.SelResolveByName \
-                            (host,
-                             lambda name, alias, addr:
-                               me._resolved(host, addr[0]),
-                             lambda: me._resolved(host, None))
+    me._noutstand = 0
+
+  def prepare(me, name):
+    """Prime the resolver to resolve the given host NAME."""
+    if name not in me._namemap:
+      me._namemap[name] = host = ResolvingHost(name)
+      host._resolv = M.SelResolveByName(
+        name,
+        lambda cname, alias, addr: me._resolved(host, addr[0]),
+        lambda: me._resolved(host, None))
+      me._noutstand += 1
 
   def run(me):
     """Run the background DNS resolver until it's finished."""
-    while me._resolvers:
-      M.select()
+    while me._noutstand: M.select()
 
-  def lookup(me, host):
-    """
-    Fetch the address corresponding to HOST.
-    """
-    addr = me._namemap[host]
-    if addr is None:
-      raise ResolverFailure(host, '(unknown failure)')
-    return addr
+  def lookup(me, name):
+    """Fetch the address corresponding to the host NAME."""
+    return me._namemap[name].get()
 
   def _resolved(me, host, addr):
     """Callback function: remember that ADDR is the address for HOST."""
-    me._namemap[host] = addr
-    del me._resolvers[host]
+    if addr is None:
+      host.failed('(unknown failure)')
+    else:
+      host.setaddr(addr)
+    host._resolv = None
+    me._noutstand -= 1
 
 ###--------------------------------------------------------------------------
 ### The configuration parser.
@@ -134,7 +161,7 @@ RX_REF = RX.compile(r'(?x) \$ \( ([^)]+) \)')
 ## Match a $[HOST] name resolution reference; group 1 is the HOST.
 RX_RESOLVE = RX.compile(r'(?x) \$ \[ ([^]]+) \]')
 
-class ConfigSyntaxError (Exception):
+class ConfigSyntaxError (ExpectedError):
   def __init__(me, fname, lno, msg):
     me.fname = fname
     me.lno = lno
@@ -145,7 +172,7 @@ class ConfigSyntaxError (Exception):
 def _fmt_path(path):
   return ' -> '.join(["`%s'" % hop for hop in path])
 
-class AmbiguousOptionError (Exception):
+class AmbiguousOptionError (ExpectedError):
   def __init__(me, key, patha, vala, pathb, valb):
     me.key = key
     me.patha, me.vala = patha, vala
@@ -155,7 +182,7 @@ class AmbiguousOptionError (Exception):
         "path %s yields `%s' but %s yields `%s'" % \
         (me.key, _fmt_path(me.patha), me.vala, _fmt_path(me.pathb), me.valb)
 
-class InheritanceCycleError (Exception):
+class InheritanceCycleError (ExpectedError):
   def __init__(me, key, path):
     me.key = key
     me.path = path
@@ -163,13 +190,13 @@ class InheritanceCycleError (Exception):
     return "Found a cycle %s looking up key `%s'" % \
         (_fmt_path(me.path), me.key)
 
-class MissingSectionException (Exception):
+class MissingSectionException (ExpectedError):
   def __init__(me, sec):
-    me.key = key
+    me.sec = sec
   def __str__(me):
     return "Section `%s' not found" % (me.sec)
 
-class MissingKeyException (Exception):
+class MissingKeyException (ExpectedError):
   def __init__(me, sec, key):
     me.sec = sec
     me.key = key
@@ -562,8 +589,12 @@ def main():
     cdb = CDB.cdbmake(opts.cdbfile, opts.cdbfile + '.new')
   else:
     cdb = CDBFake()
-  conf = getconf(args[1:])
-  output(conf, cdb)
+  try:
+    conf = getconf(args[1:])
+    output(conf, cdb)
+  except ExpectedError, e:
+    M.moan(str(e))
+    exit(2)
 
 if __name__ == '__main__':
   main()