@@ -154,17 +154,21 @@ def __init__(
154154
155155 if key_fetchers is None :
156156 key_fetchers = (
157+ # Fetch keys from the database.
157158 StoreKeyFetcher (hs ),
159+ # Fetch keys from a configured Perspectives server.
158160 PerspectivesKeyFetcher (hs ),
161+ # Fetch keys from the origin server directly.
159162 ServerKeyFetcher (hs ),
160163 )
161164 self ._key_fetchers = key_fetchers
162165
163- self ._server_queue : BatchingQueue [
166+ self ._fetch_keys_queue : BatchingQueue [
164167 _FetchKeyRequest , Dict [str , Dict [str , FetchKeyResult ]]
165168 ] = BatchingQueue (
166169 "keyring_server" ,
167170 clock = hs .get_clock (),
171+ # The method called to fetch each key
168172 process_batch_callback = self ._inner_fetch_key_requests ,
169173 )
170174
@@ -287,7 +291,7 @@ async def process_request(self, verify_request: VerifyJsonRequest) -> None:
287291 minimum_valid_until_ts = verify_request .minimum_valid_until_ts ,
288292 key_ids = list (key_ids_to_find ),
289293 )
290- found_keys_by_server = await self ._server_queue .add_to_queue (
294+ found_keys_by_server = await self ._fetch_keys_queue .add_to_queue (
291295 key_request , key = verify_request .server_name
292296 )
293297
@@ -352,7 +356,17 @@ async def _process_json(
352356 async def _inner_fetch_key_requests (
353357 self , requests : List [_FetchKeyRequest ]
354358 ) -> Dict [str , Dict [str , FetchKeyResult ]]:
355- """Processing function for the queue of `_FetchKeyRequest`."""
359+ """Processing function for the queue of `_FetchKeyRequest`.
360+
361+ Takes a list of key fetch requests, de-duplicates them and then carries out
362+ each request by invoking self._inner_fetch_key_request.
363+
364+ Args:
365+ requests: A list of requests for homeserver verify keys.
366+
367+ Returns:
368+ {server name: {key id: fetch key result}}
369+ """
356370
357371 logger .debug ("Starting fetch for %s" , requests )
358372
@@ -397,8 +411,23 @@ async def _inner_fetch_key_requests(
397411 async def _inner_fetch_key_request (
398412 self , verify_request : _FetchKeyRequest
399413 ) -> Dict [str , FetchKeyResult ]:
400- """Attempt to fetch the given key by calling each key fetcher one by
401- one.
414+ """Attempt to fetch the given key by calling each key fetcher one by one.
415+
416+ If a key is found, check whether its `valid_until_ts` attribute satisfies the
417+ `minimum_valid_until_ts` attribute of the `verify_request`. If it does, we
418+ refrain from asking subsequent fetchers for that key.
419+
420+ Even if the above check fails, we still return the found key - the caller may
421+ still find the invalid key result useful. In this case, we continue to ask
422+ subsequent fetchers for the invalid key, in case they return a valid result
423+ for it. This can happen when fetching a stale key result from the database,
424+ before querying the origin server for an up-to-date result.
425+
426+ Args:
427+ verify_request: The request for a verify key. Can include multiple key IDs.
428+
429+ Returns:
430+ A map of {key_id: the key fetch result}.
402431 """
403432 logger .debug ("Starting fetch for %s" , verify_request )
404433
@@ -420,26 +449,22 @@ async def _inner_fetch_key_request(
420449 if not key :
421450 continue
422451
423- # If we already have a result for the given key ID we keep the
452+ # If we already have a result for the given key ID, we keep the
424453 # one with the highest `valid_until_ts`.
425454 existing_key = found_keys .get (key_id )
426- if existing_key :
427- if key .valid_until_ts <= existing_key .valid_until_ts :
428- continue
455+ if existing_key and existing_key .valid_until_ts > key .valid_until_ts :
456+ continue
457+
458+ # Check if this key's expiry timestamp is valid for the verify request.
459+ if key .valid_until_ts >= verify_request .minimum_valid_until_ts :
460+ # Stop looking for this key from subsequent fetchers.
461+ missing_key_ids .discard (key_id )
429462
430- # We always store the returned key even if it doesn't the
463+ # We always store the returned key even if it doesn't meet the
431464 # `minimum_valid_until_ts` requirement, as some verification
432465 # requests may still be able to be satisfied by it.
433- #
434- # We still keep looking for the key from other fetchers in that
435- # case though.
436466 found_keys [key_id ] = key
437467
438- if key .valid_until_ts < verify_request .minimum_valid_until_ts :
439- continue
440-
441- missing_key_ids .discard (key_id )
442-
443468 return found_keys
444469
445470
0 commit comments