Tidy up get_events

This commit is contained in:
Erik Johnston 2016-08-04 14:01:18 +01:00
parent edb33eb163
commit e3ee63578f

View file

@ -411,28 +411,26 @@ class FederationClient(FederationBase):
return srvs return srvs
batch_size = 20 batch_size = 20
while missing_events: missing_events = len(missing_events)
batch = [] for i in xrange(0, batch_size, batch_size):
try: batch = set(missing_events[i:i + batch_size])
for _ in range(0, batch_size):
batch.append(missing_events.pop())
except KeyError:
pass
deferreds = [ deferreds = [
self.get_pdu( self.get_pdu(
destinations=random_server_list(), destinations=random_server_list(),
event_id=e_id, event_id=e_id,
).addBoth(lambda r, e: (r, e), e_id) )
for e_id in batch for e_id in batch
] ]
res = yield defer.DeferredList(deferreds, consumeErrors=True) res = yield defer.DeferredList(deferreds, consumeErrors=True)
for success, (result, e_id) in res: for success, result in res:
if success and result: if success:
signed_events.append(result) signed_events.append(result)
else: batch.discard(result.event_id)
failed_to_fetch.add(e_id)
# We removed all events we successfully fetched from `batch`
failed_to_fetch.update(batch)
defer.returnValue((signed_events, failed_to_fetch)) defer.returnValue((signed_events, failed_to_fetch))