Skip to content

Commit

Permalink
Merge pull request matrix-org#1114 from matrix-org/markjh/limit_key_r…
Browse files Browse the repository at this point in the history
…etries

Limit how often we ask for keys from dead servers
  • Loading branch information
NegativeMjark authored Sep 13, 2016
2 parents 0c05773 + ad816b0 commit 497f053
Showing 1 changed file with 30 additions and 11 deletions.
41 changes: 30 additions & 11 deletions synapse/handlers/e2e_keys.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
from synapse.api.errors import SynapseError, CodeMessageException
from synapse.types import get_domain_from_id
from synapse.util.logcontext import preserve_fn, preserve_context_over_deferred
from synapse.util.retryutils import get_retry_limiter, NotRetryingDestination

logger = logging.getLogger(__name__)

Expand Down Expand Up @@ -88,18 +89,28 @@ def query_devices(self, query_body, timeout):
def do_remote_query(destination):
destination_query = remote_queries[destination]
try:
remote_result = yield self.federation.query_client_keys(
destination,
{"device_keys": destination_query},
timeout=timeout
limiter = yield get_retry_limiter(
destination, self.clock, self.store
)
with limiter:
remote_result = yield self.federation.query_client_keys(
destination,
{"device_keys": destination_query},
timeout=timeout
)

for user_id, keys in remote_result["device_keys"].items():
if user_id in destination_query:
results[user_id] = keys

except CodeMessageException as e:
failures[destination] = {
"status": e.code, "message": e.message
}
except NotRetryingDestination as e:
failures[destination] = {
"status": 503, "message": "Not ready for retry",
}

yield preserve_context_over_deferred(defer.gatherResults([
preserve_fn(do_remote_query)(destination)
Expand Down Expand Up @@ -191,18 +202,26 @@ def claim_one_time_keys(self, query, timeout):
def claim_client_keys(destination):
device_keys = remote_queries[destination]
try:
remote_result = yield self.federation.claim_client_keys(
destination,
{"one_time_keys": device_keys},
timeout=timeout
limiter = yield get_retry_limiter(
destination, self.clock, self.store
)
for user_id, keys in remote_result["one_time_keys"].items():
if user_id in device_keys:
json_result[user_id] = keys
with limiter:
remote_result = yield self.federation.claim_client_keys(
destination,
{"one_time_keys": device_keys},
timeout=timeout
)
for user_id, keys in remote_result["one_time_keys"].items():
if user_id in device_keys:
json_result[user_id] = keys
except CodeMessageException as e:
failures[destination] = {
"status": e.code, "message": e.message
}
except NotRetryingDestination as e:
failures[destination] = {
"status": 503, "message": "Not ready for retry",
}

yield preserve_context_over_deferred(defer.gatherResults([
preserve_fn(claim_client_keys)(destination)
Expand Down

0 comments on commit 497f053

Please sign in to comment.