commit 2b8ae69028a4618b5bfaf1c46813b8daeecb1411 Author: Damian Johnson atagar@torproject.org Date: Sat Sep 2 12:51:32 2017 -0700
Move ConsensusTracker to the relay cache --- nyx/__init__.py | 23 +++++++++++++++++------ nyx/tracker.py | 44 ++++++++++++++++++++++---------------------- 2 files changed, 39 insertions(+), 28 deletions(-)
diff --git a/nyx/__init__.py b/nyx/__init__.py index 39c1e5d..26b490d 100644 --- a/nyx/__init__.py +++ b/nyx/__init__.py @@ -8,6 +8,7 @@ Tor curses monitoring application.
nyx_interface - nyx interface singleton tor_controller - tor connection singleton + cache - provides our application cache
show_message - shows a message to the user input_prompt - prompts the user for text input @@ -15,6 +16,15 @@ Tor curses monitoring application. expand_path - expands path with respect to our chroot join - joins a series of strings up to a set length
+ Cache - application cache + |- write - provides a content where we can write to the cache + | + |- relay_nickname - provides the nickname of a relay + +- relay_address - provides the address and orport of a relay + + CacheWriter - context in which we can write to the cache + +- record_relay - caches information about a relay + Interface - overall nyx interface |- get_page - page we're showing |- set_page - sets the page we're showing @@ -389,30 +399,31 @@ class Cache(object): with self._conn: yield CacheWriter(self)
- def relay_nickname(self, fingerprint): + def relay_nickname(self, fingerprint, default = None): """ Provides the nickname associated with the given relay.
:param str fingerprint: relay to look up + :param str default: response if no such relay exists
- :returns: **str** with the nickname ("Unnamed" if unset), and **None** if - no such relay exists + :returns: **str** with the nickname ("Unnamed" if unset) """
result = self._query('SELECT nickname FROM relays WHERE fingerprint=?', fingerprint).fetchone() - return result[0] if result else None + return result[0] if result else default
- def relay_address(self, fingerprint): + def relay_address(self, fingerprint, default = None): """ Provides the (address, port) tuple where a relay is running.
:param str fingerprint: fingerprint to be checked + :param str default: response if no such relay exists
:returns: **tuple** with a **str** address and **int** port """
result = self._query('SELECT address, or_port FROM relays WHERE fingerprint=?', fingerprint).fetchone() - return result if result else None # TODO: does this raise if fingerprint doesn't exist? + return result if result else default
def _query(self, query, *param): """ diff --git a/nyx/tracker.py b/nyx/tracker.py index affe680..cbfaedf 100644 --- a/nyx/tracker.py +++ b/nyx/tracker.py @@ -55,6 +55,7 @@ import os import time import threading
+import nyx import stem.control import stem.descriptor.router_status_entry import stem.util.log @@ -808,7 +809,6 @@ class ConsensusTracker(object):
def __init__(self): self._fingerprint_cache = {} # {address => [(port, fingerprint), ..]} for relays - self._relay_cache = {} # fingerprint => address, orport, nickname lookup cache
self._my_router_status_entry = None self._my_router_status_entry_time = 0 @@ -821,19 +821,20 @@ class ConsensusTracker(object): ns_response = controller.get_info('ns/all', None)
if ns_response: - for line in ns_response.splitlines(): - if line.startswith('r '): - r_comp = line.split(' ') + 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] + 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]
- self._fingerprint_cache.setdefault(address, []).append((or_port, fingerprint)) - self._relay_cache[fingerprint] = (address, or_port, nickname) + self._fingerprint_cache.setdefault(address, []).append((or_port, fingerprint)) + writer.record_relay(fingerprint, address, or_port, nickname)
- stem.util.log.info('Cached consensus data. Took %0.2fs. Cache size is %s for fingerprints, %s for relays' % (time.time() - start_time, stem.util.str_tools.size_label(system.size_of(self._fingerprint_cache), 2), stem.util.str_tools.size_label(system.size_of(self._relay_cache), 2))) + 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)
@@ -845,23 +846,22 @@ class ConsensusTracker(object): """
new_fingerprint_cache = {} - new_relay_cache = {}
start_time = time.time() our_fingerprint = tor_controller().get_info('fingerprint', None)
- for desc in router_status_entries: - new_fingerprint_cache.setdefault(desc.address, []).append((desc.or_port, desc.fingerprint)) - new_relay_cache[desc.fingerprint] = (desc.address, desc.or_port, desc.nickname) + with nyx.cache().write() as writer: + for desc in router_status_entries: + new_fingerprint_cache.setdefault(desc.address, []).append((desc.or_port, desc.fingerprint)) + writer.record_relay(desc.fingerprint, desc.address, desc.or_port, desc.nickname)
- if desc.fingerprint == our_fingerprint: - self._my_router_status_entry = desc - self._my_router_status_entry_time = time.time() + if desc.fingerprint == our_fingerprint: + self._my_router_status_entry = desc + self._my_router_status_entry_time = time.time()
self._fingerprint_cache = new_fingerprint_cache - self._relay_cache = new_relay_cache
- stem.util.log.info('Updated consensus cache. Took %0.2fs. Cache size is %s for fingerprints, %s for relays' % (time.time() - start_time, stem.util.str_tools.size_label(system.size_of(self._fingerprint_cache), 2), stem.util.str_tools.size_label(system.size_of(self._relay_cache), 2))) + stem.util.log.info('Updated consensus cache, took %0.2fs.' % (time.time() - start_time))
def my_router_status_entry(self): """ @@ -895,7 +895,7 @@ class ConsensusTracker(object): elif fingerprint == controller.get_info('fingerprint', None): return controller.get_conf('Nickname', 'Unnamed') else: - return self._relay_cache.get(fingerprint)[2] + return nyx.cache().relay_nickname(fingerprint)
def get_relay_fingerprints(self, address): """ @@ -935,4 +935,4 @@ class ConsensusTracker(object): if my_address and len(my_or_ports) == 1: return (my_address, my_or_ports[0])
- return self._relay_cache.get(fingerprint, default)[:2] + return nyx.cache().relay_address(fingerprint, default)