Commit 883307ba authored by juga  's avatar juga
Browse files

destination: remove unused code

parent 84e5d82c
import logging
import random
import time
import os
from threading import RLock
import requests
from urllib.parse import urlparse
from stem.control import EventType
import sbws.util.stem as stem_utils
import sbws.util.requests as requests_utils
......@@ -153,17 +149,6 @@ class Destination:
self.consecutive_failures += 1
self.failed = True
def is_usable(self, circ_id, session, cont):
''' Use **connect_to_destination_over_circuit** to determine if this
destination is usable and return what it returns. Just a small wrapper.
if not isinstance(self.verify, bool):
if not os.path.isfile(self.verify):
return False, '{} is believed to be a CA bundle file on disk '\
'but it does not exist'.format(self.verify)
return connect_to_destination_over_circuit(
self, circ_id, session, cont, self._max_dl)
def url(self):
return self._url.geturl()
......@@ -208,71 +193,11 @@ class DestinationList:
self._cb = circuit_builder
self._rl = relay_list
self._all_dests = dests
# Inially all destionations are usable until proven otherwise.
self._usable_dests = self._all_dests
self._last_usability_test = 0
self._usability_test_interval = \
conf.getint('destinations', 'usability_test_interval')
self._usability_test_timeout = \
conf.getfloat('general', 'http_timeout')
self._usability_lock = RLock()
def functional_destinations(self):
return [d for d in self._all_dests if d.is_functional]
def _should_perform_usability_test(self):
# Until bigger refactor, do not perform usability test.
# Every time a measurement is done, it already performs what usability
# test does.
return False
def _perform_usability_test(self):
log.debug('Perform usability tests')
cont = self._cont
timeout = self._usability_test_timeout
session = requests_utils.make_session(cont, timeout)
usable_dests = []
for dest in self._all_dests:
possible_exits = self._rl.exits_not_bad_allowing_port(dest.port)
# Keep the fastest 10% of exits, or 3, whichever is larger
num_keep = int(max(3, len(possible_exits) * 0.1))
possible_exits = sorted(
possible_exits, key=lambda e: e.consensus_bandwidth,
exits = possible_exits[0:num_keep]
if len(exits) < 1:
log.warning("There are no exits to perform usability tests.")
# Try three times to build a circuit to test this destination
circ_id = None
for _ in range(0, 3):
# Pick a random exit
exit = self._rng.choice(exits)
circ_id, reason = \
self._cb.build_circuit([None, exit.fingerprint])
if circ_id:
if not circ_id:
log.warning('Unable to build a circuit to test the usability '
'of %s, %s', dest.url, reason)
log.debug('Built circ %s %s to test usability of %s', circ_id,
stem_utils.circuit_str(cont, circ_id), dest.url)
is_usable, data = dest.is_usable(circ_id, session, cont)
if not is_usable:
assert is_usable
log.debug('%s seems usable so we will keep it', dest.url)
self._usable_dests = usable_dests
self._last_usability_test = time.time()
def from_config(conf, circuit_builder, relay_list, controller):
assert 'destinations' in conf
......@@ -13,10 +13,6 @@ def test_destination_list_no_usability_test_success(
destination_list, error_msg = DestinationList.from_config(
conf, cb, rl, persistent_launch_tor
# Initially all destinations should be "usable".
assert destination_list._all_dests == destination_list._usable_dests
# Because this is disabled.
assert destination_list._should_perform_usability_test() is False
# Because there's only 1 destination in conftest, random should return
# the same one.
assert == \
......@@ -14,7 +14,6 @@ def static_time(value):
def _build_result_for_relay(conf, rl, result_type, relay_nick,
relay = [r for r in rl.relays if r.nickname == relay_nick]
assert len(relay) == 1
relay = relay[0]
other = [r for r in rl.relays if r.nickname != relay_nick][0]
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment