Skip to content
This repository has been archived by the owner on Apr 26, 2024. It is now read-only.

Commit

Permalink
Merge pull request #384 from matrix-org/erikj/shorter_retries
Browse files Browse the repository at this point in the history
Only retry federation requests for a long time for background requests
  • Loading branch information
erikjohnston committed Nov 17, 2015
2 parents a9770e5 + bd3de8f commit b697a84
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 7 deletions.
1 change: 1 addition & 0 deletions synapse/federation/transport/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,7 @@ def send_transaction(self, transaction, json_data_callback=None):
path=PREFIX + "/send/%s/" % transaction.transaction_id,
data=json_data,
json_data_callback=json_data_callback,
long_retries=True,
)

logger.debug(
Expand Down
28 changes: 21 additions & 7 deletions synapse/http/matrixfederationclient.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,8 @@
)


MAX_RETRIES = 10
MAX_LONG_RETRIES = 10
MAX_SHORT_RETRIES = 3


class MatrixFederationEndpointFactory(object):
Expand Down Expand Up @@ -103,7 +104,7 @@ def _create_url(self, destination, path_bytes, param_bytes, query_bytes):
def _create_request(self, destination, method, path_bytes,
body_callback, headers_dict={}, param_bytes=b"",
query_bytes=b"", retry_on_dns_fail=True,
timeout=None):
timeout=None, long_retries=False):
""" Creates and sends a request to the given url
"""
headers_dict[b"User-Agent"] = [self.version_string]
Expand All @@ -123,7 +124,10 @@ def _create_request(self, destination, method, path_bytes,

# XXX: Would be much nicer to retry only at the transaction-layer
# (once we have reliable transactions in place)
retries_left = MAX_RETRIES
if long_retries:
retries_left = MAX_LONG_RETRIES
else:
retries_left = MAX_SHORT_RETRIES

http_url_bytes = urlparse.urlunparse(
("", "", path_bytes, param_bytes, query_bytes, "")
Expand Down Expand Up @@ -184,9 +188,15 @@ def send_request():
)

if retries_left and not timeout:
delay = 4 ** (MAX_RETRIES + 1 - retries_left)
delay = max(delay, 60)
delay *= random.uniform(0.8, 1.4)
if long_retries:
delay = 4 ** (MAX_LONG_RETRIES + 1 - retries_left)
delay = max(delay, 60)
delay *= random.uniform(0.8, 1.4)
else:
delay = 0.5 * 2 ** (MAX_SHORT_RETRIES - retries_left)
delay = max(delay, 2)
delay *= random.uniform(0.8, 1.4)

yield sleep(delay)
retries_left -= 1
else:
Expand Down Expand Up @@ -237,7 +247,8 @@ def sign_request(self, destination, method, url_bytes, headers_dict,
headers_dict[b"Authorization"] = auth_headers

@defer.inlineCallbacks
def put_json(self, destination, path, data={}, json_data_callback=None):
def put_json(self, destination, path, data={}, json_data_callback=None,
long_retries=False):
""" Sends the specifed json data using PUT
Args:
Expand All @@ -248,6 +259,8 @@ def put_json(self, destination, path, data={}, json_data_callback=None):
the request body. This will be encoded as JSON.
json_data_callback (callable): A callable returning the dict to
use as the request body.
long_retries (bool): A boolean that indicates whether we should
retry for a short or long time.
Returns:
Deferred: Succeeds when we get a 2xx HTTP response. The result
Expand All @@ -273,6 +286,7 @@ def body_callback(method, url_bytes, headers_dict):
path.encode("ascii"),
body_callback=body_callback,
headers_dict={"Content-Type": ["application/json"]},
long_retries=long_retries,
)

if 200 <= response.code < 300:
Expand Down
2 changes: 2 additions & 0 deletions tests/federation/test_federation.py
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,7 @@ def test_send_pdu(self):
'pdu_failures': [],
},
json_data_callback=ANY,
long_retries=True,
)

@defer.inlineCallbacks
Expand Down Expand Up @@ -228,6 +229,7 @@ def test_send_edu(self):
'pdu_failures': [],
},
json_data_callback=ANY,
long_retries=True,
)

@defer.inlineCallbacks
Expand Down
13 changes: 13 additions & 0 deletions tests/handlers/test_presence.py
Original file line number Diff line number Diff line change
Expand Up @@ -409,6 +409,7 @@ def test_invite_remote(self):
}
),
json_data_callback=ANY,
long_retries=True,
),
defer.succeed((200, "OK"))
)
Expand Down Expand Up @@ -443,6 +444,7 @@ def test_accept_remote(self):
}
),
json_data_callback=ANY,
long_retries=True,
),
defer.succeed((200, "OK"))
)
Expand Down Expand Up @@ -483,6 +485,7 @@ def test_invited_remote_nonexistant(self):
}
),
json_data_callback=ANY,
long_retries=True,
),
defer.succeed((200, "OK"))
)
Expand Down Expand Up @@ -827,6 +830,7 @@ def test_push_remote(self):
}
),
json_data_callback=ANY,
long_retries=True,
),
defer.succeed((200, "OK"))
)
Expand All @@ -843,6 +847,7 @@ def test_push_remote(self):
}
),
json_data_callback=ANY,
long_retries=True,
),
defer.succeed((200, "OK"))
)
Expand Down Expand Up @@ -1033,6 +1038,7 @@ def test_join_room_remote(self):
}
),
json_data_callback=ANY,
long_retries=True,
),
defer.succeed((200, "OK"))
)
Expand All @@ -1048,6 +1054,7 @@ def test_join_room_remote(self):
}
),
json_data_callback=ANY,
long_retries=True,
),
defer.succeed((200, "OK"))
)
Expand Down Expand Up @@ -1078,6 +1085,7 @@ def test_join_room_remote(self):
}
),
json_data_callback=ANY,
long_retries=True,
),
defer.succeed((200, "OK"))
)
Expand Down Expand Up @@ -1184,6 +1192,7 @@ def test_remote_poll_send(self):
},
),
json_data_callback=ANY,
long_retries=True,
),
defer.succeed((200, "OK"))
)
Expand All @@ -1200,6 +1209,7 @@ def test_remote_poll_send(self):
},
),
json_data_callback=ANY,
long_retries=True,
),
defer.succeed((200, "OK"))
)
Expand Down Expand Up @@ -1232,6 +1242,7 @@ def test_remote_poll_send(self):
},
),
json_data_callback=ANY,
long_retries=True,
),
defer.succeed((200, "OK"))
)
Expand Down Expand Up @@ -1265,6 +1276,7 @@ def test_remote_poll_send(self):
},
),
json_data_callback=ANY,
long_retries=True,
),
defer.succeed((200, "OK"))
)
Expand Down Expand Up @@ -1297,6 +1309,7 @@ def test_remote_poll_receive(self):
},
),
json_data_callback=ANY,
long_retries=True,
),
defer.succeed((200, "OK"))
)
Expand Down
2 changes: 2 additions & 0 deletions tests/handlers/test_typing.py
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,7 @@ def test_started_typing_remote_send(self):
}
),
json_data_callback=ANY,
long_retries=True,
),
defer.succeed((200, "OK"))
)
Expand Down Expand Up @@ -284,6 +285,7 @@ def test_stopped_typing(self):
}
),
json_data_callback=ANY,
long_retries=True,
),
defer.succeed((200, "OK"))
)
Expand Down

0 comments on commit b697a84

Please sign in to comment.