[tor-commits] [sbws/master] Make sure we only refresh our list of relays once

pastly at torproject.org pastly at torproject.org
Tue Jun 26 15:36:50 UTC 2018


commit 5df7396760a0703e948e97c74e2179b86aef6a90
Author: Matt Traudt <sirmatt at ksu.edu>
Date:   Fri Jun 22 09:53:17 2018 -0400

    Make sure we only refresh our list of relays once
    
    GH: ref #205
---
 sbws/lib/relaylist.py | 19 +++++++++++++++++--
 1 file changed, 17 insertions(+), 2 deletions(-)

diff --git a/sbws/lib/relaylist.py b/sbws/lib/relaylist.py
index f87d3d0..8b19a7c 100644
--- a/sbws/lib/relaylist.py
+++ b/sbws/lib/relaylist.py
@@ -8,6 +8,7 @@ import random
 import time
 import logging
 from sbws.globals import resolve
+from threading import Lock
 
 log = logging.getLogger(__name__)
 
@@ -124,12 +125,26 @@ class RelayList:
     def __init__(self, args, conf, controller):
         self._controller = controller
         self.rng = random.SystemRandom()
+        self._refresh_lock = Lock()
         self._refresh()
 
+    def _need_refresh(self):
+        return time.time() >= self._last_refresh + self.REFRESH_INTERVAL
+
     @property
     def relays(self):
-        if time.time() >= self._last_refresh + self.REFRESH_INTERVAL:
-            self._refresh()
+        # See if we can get the list of relays without having to do a refresh,
+        # which is expensive and blocks other threads
+        if self._need_refresh():
+            # Whelp we couldn't just get the list of relays because the list is
+            # stale. Wait for the lock so we can refresh it.
+            with self._refresh_lock:
+                # Now we have the lock ... but wait! Maybe someone else already
+                # did the refreshing. So check if it still needs refreshing. If
+                # not, we can do nothing.
+                if self._need_refresh():
+                    self._refresh()
+        assert not self._need_refresh()
         return self._relays
 
     @property





More information about the tor-commits mailing list