[tor-commits] [nyx/master] Update ConsensusTracker with unparsed consensus content

atagar at torproject.org atagar at torproject.org
Sun Oct 29 19:22:59 UTC 2017


commit a8cf209dce02dc7722ff3601633b9ab847e5851e
Author: Damian Johnson <atagar at torproject.org>
Date:   Sun Oct 29 12:21:58 2017 -0700

    Update ConsensusTracker with unparsed consensus content
    
    Lighten the work needed by the ConsensusTracker by having it process unparsed
    consensus content. We already do this when starting up, so this actually
    simplifies our code quite a bit.
---
 nyx/__init__.py |  4 ++++
 nyx/tracker.py  | 43 ++++++++++++++++---------------------------
 2 files changed, 20 insertions(+), 27 deletions(-)

diff --git a/nyx/__init__.py b/nyx/__init__.py
index 7c61651..7394872 100644
--- a/nyx/__init__.py
+++ b/nyx/__init__.py
@@ -112,6 +112,10 @@ stem.control.CACHE_ADDRESS_FOR = 30
 
 stem.control.LOG_CACHE_FETCHES = False
 
+# don't parse NEWCONSENSUS events since we just care about the content
+
+stem.response.events.PARSE_NEWCONSENSUS_EVENTS = False
+
 # Duration for threads to pause when there's no work left to do. This is a
 # compromise - lower means fater shutdown when quit but higher means lower cpu
 # usage when running.
diff --git a/nyx/tracker.py b/nyx/tracker.py
index fa2381e..c2c4714 100644
--- a/nyx/tracker.py
+++ b/nyx/tracker.py
@@ -818,43 +818,32 @@ class ConsensusTracker(object):
       stem.util.log.info('Cache is only %s old, no need to refresh it.' % str_tools.time_label(cache_age, is_long = True))
     else:
       stem.util.log.info('Cache is %s old, refreshing relay information.' % str_tools.time_label(cache_age, is_long = True))
-      start_time = time.time()
       ns_response = controller.get_info('ns/all', None)
 
       if ns_response:
-        with nyx.cache().write() as writer:
-          for line in ns_response.splitlines():
-            if line.startswith('r '):
-              r_comp = line.split(' ')
-
-              address = r_comp[6]
-              or_port = int(r_comp[7])
-              fingerprint = stem.descriptor.router_status_entry._base64_to_hex(r_comp[2])
-              nickname = r_comp[1]
-
-              writer.record_relay(fingerprint, address, or_port, nickname)
-
-        stem.util.log.info('Cached consensus data, took %0.2fs.' % (time.time() - start_time))
-
-    controller.add_event_listener(lambda event: self.update(event.desc), stem.control.EventType.NEWCONSENSUS)
+        self._update(ns_response)
 
-  def update(self, router_status_entries):
-    """
-    Updates our cache with the given router status entries.
-
-    :param list router_status_entries: router status entries to populate our cache with
-    """
+    controller.add_event_listener(lambda event: self._update(event.consensus_content), stem.control.EventType.NEWCONSENSUS)
 
+  def _update(self, consensus_content):
     start_time = time.time()
     our_fingerprint = tor_controller().get_info('fingerprint', None)
 
     with nyx.cache().write() as writer:
-      for desc in router_status_entries:
-        writer.record_relay(desc.fingerprint, desc.address, desc.or_port, desc.nickname)
+      for line in consensus_content.splitlines():
+        if line.startswith('r '):
+          r_comp = line.split(' ')
+
+          address = r_comp[6]
+          or_port = int(r_comp[7])
+          fingerprint = stem.descriptor.router_status_entry._base64_to_hex(r_comp[2])
+          nickname = r_comp[1]
+
+          if fingerprint == our_fingerprint:
+            self._my_router_status_entry = None
+            self._my_router_status_entry_time = 0
 
-        if desc.fingerprint == our_fingerprint:
-          self._my_router_status_entry = desc
-          self._my_router_status_entry_time = time.time()
+          writer.record_relay(fingerprint, address, or_port, nickname)
 
     stem.util.log.info('Updated consensus cache, took %0.2fs.' % (time.time() - start_time))
 



More information about the tor-commits mailing list