Simplification to Keyring.wait_for_previous_lookups. (#5250)

The list of server names was redundant, since it was equivalent to the keys on
the server_to_deferred map. This reduces the number of large lists being passed
around, and has the benefit of deduplicating the entries in `wait_on`.
This commit is contained in:
Richard van der Hoff 2019-05-24 22:17:18 +01:00 committed by GitHub
parent dd64b9dbdd
commit fa1b293da2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 7 additions and 9 deletions

1
changelog.d/5250.misc Normal file
View File

@ -0,0 +1 @@
Simplification to Keyring.wait_for_previous_lookups.

View File

@ -180,9 +180,7 @@ class Keyring(object):
# We want to wait for any previous lookups to complete before # We want to wait for any previous lookups to complete before
# proceeding. # proceeding.
yield self.wait_for_previous_lookups( yield self.wait_for_previous_lookups(server_to_deferred)
[rq.server_name for rq in verify_requests], server_to_deferred
)
# Actually start fetching keys. # Actually start fetching keys.
self._get_server_verify_keys(verify_requests) self._get_server_verify_keys(verify_requests)
@ -215,12 +213,11 @@ class Keyring(object):
logger.exception("Error starting key lookups") logger.exception("Error starting key lookups")
@defer.inlineCallbacks @defer.inlineCallbacks
def wait_for_previous_lookups(self, server_names, server_to_deferred): def wait_for_previous_lookups(self, server_to_deferred):
"""Waits for any previous key lookups for the given servers to finish. """Waits for any previous key lookups for the given servers to finish.
Args: Args:
server_names (list): list of server_names we want to lookup server_to_deferred (dict[str, Deferred]): server_name to deferred which gets
server_to_deferred (dict): server_name to deferred which gets
resolved once we've finished looking up keys for that server. resolved once we've finished looking up keys for that server.
The Deferreds should be regular twisted ones which call their The Deferreds should be regular twisted ones which call their
callbacks with no logcontext. callbacks with no logcontext.
@ -233,7 +230,7 @@ class Keyring(object):
while True: while True:
wait_on = [ wait_on = [
(server_name, self.key_downloads[server_name]) (server_name, self.key_downloads[server_name])
for server_name in server_names for server_name in server_to_deferred.keys()
if server_name in self.key_downloads if server_name in self.key_downloads
] ]
if not wait_on: if not wait_on:

View File

@ -85,7 +85,7 @@ class KeyringTestCase(unittest.HomeserverTestCase):
# we run the lookup in a logcontext so that the patched inlineCallbacks can check # we run the lookup in a logcontext so that the patched inlineCallbacks can check
# it is doing the right thing with logcontexts. # it is doing the right thing with logcontexts.
wait_1_deferred = run_in_context( wait_1_deferred = run_in_context(
kr.wait_for_previous_lookups, ["server1"], {"server1": lookup_1_deferred} kr.wait_for_previous_lookups, {"server1": lookup_1_deferred}
) )
# there were no previous lookups, so the deferred should be ready # there were no previous lookups, so the deferred should be ready
@ -94,7 +94,7 @@ class KeyringTestCase(unittest.HomeserverTestCase):
# set off another wait. It should block because the first lookup # set off another wait. It should block because the first lookup
# hasn't yet completed. # hasn't yet completed.
wait_2_deferred = run_in_context( wait_2_deferred = run_in_context(
kr.wait_for_previous_lookups, ["server1"], {"server1": lookup_2_deferred} kr.wait_for_previous_lookups, {"server1": lookup_2_deferred}
) )
self.assertFalse(wait_2_deferred.called) self.assertFalse(wait_2_deferred.called)