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

Add Retry-After to M_LIMIT_EXCEEDED error responses #16136

Merged
merged 15 commits into from
Aug 24, 2023
Merged
1 change: 1 addition & 0 deletions changelog.d/16136.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Return a `Retry-After` with `M_LIMIT_EXCEEDED` error responses.
8 changes: 7 additions & 1 deletion synapse/api/errors.py
clokep marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
"""Contains exceptions and error codes."""

import logging
import math
import typing
from enum import Enum
from http import HTTPStatus
Expand Down Expand Up @@ -510,7 +511,12 @@ def __init__(
retry_after_ms: Optional[int] = None,
errcode: str = Codes.LIMIT_EXCEEDED,
):
super().__init__(code, msg, errcode)
headers = (
None
if retry_after_ms is None
else {"Retry-After": str(math.ceil(retry_after_ms / 1000))}
clokep marked this conversation as resolved.
Show resolved Hide resolved
)
super().__init__(code, msg, errcode, headers=headers)
self.retry_after_ms = retry_after_ms

def error_dict(self, config: Optional["HomeServerConfig"]) -> "JsonDict":
Expand Down
32 changes: 32 additions & 0 deletions tests/api/test_errors.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# Copyright 2023 The Matrix.org Foundation C.I.C.
#
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

from synapse.api.errors import LimitExceededError

from tests import unittest


class ErrorsTestCase(unittest.TestCase):
def test_limit_exceeded_header(self) -> None:
err = LimitExceededError(retry_after_ms=100)
self.assertEqual(err.error_dict(None).get("retry_after_ms"), 100)
assert err.headers
Half-Shot marked this conversation as resolved.
Show resolved Hide resolved
self.assertEqual(err.headers.get("Retry-After"), "1")

def test_limit_exceeded_rounding(self) -> None:
err = LimitExceededError(retry_after_ms=3001)
self.assertEqual(err.error_dict(None).get("retry_after_ms"), 3001)
assert err.headers
self.assertEqual(err.headers.get("Retry-After"), "4")
15 changes: 12 additions & 3 deletions tests/rest/client/test_login.py
clokep marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
Expand Up @@ -189,12 +189,15 @@ def test_POST_ratelimiting_per_address(self) -> None:
if i == 5:
self.assertEqual(channel.code, 429, msg=channel.result)
retry_after_ms = int(channel.json_body["retry_after_ms"])
retry_header = channel.headers.getRawHeaders("Retry-After")
else:
self.assertEqual(channel.code, 200, msg=channel.result)

# Since we're ratelimiting at 1 request/min, retry_after_ms should be lower
# than 1min.
self.assertTrue(retry_after_ms < 6000)
self.assertLess(retry_after_ms, 6000)
assert retry_header
self.assertLessEqual(int(retry_header[0]), 6)

self.reactor.advance(retry_after_ms / 1000.0 + 1.0)

Expand Down Expand Up @@ -234,12 +237,15 @@ def test_POST_ratelimiting_per_account(self) -> None:
if i == 5:
self.assertEqual(channel.code, 429, msg=channel.result)
retry_after_ms = int(channel.json_body["retry_after_ms"])
retry_header = channel.headers.getRawHeaders("Retry-After")
else:
self.assertEqual(channel.code, 200, msg=channel.result)

# Since we're ratelimiting at 1 request/min, retry_after_ms should be lower
# than 1min.
self.assertTrue(retry_after_ms < 6000)
self.assertLess(retry_after_ms, 6000)
assert retry_header
self.assertLessEqual(int(retry_header[0]), 6)

self.reactor.advance(retry_after_ms / 1000.0)

Expand Down Expand Up @@ -279,12 +285,15 @@ def test_POST_ratelimiting_per_account_failed_attempts(self) -> None:
if i == 5:
self.assertEqual(channel.code, 429, msg=channel.result)
retry_after_ms = int(channel.json_body["retry_after_ms"])
retry_header = channel.headers.getRawHeaders("Retry-After")
else:
self.assertEqual(channel.code, 403, msg=channel.result)

# Since we're ratelimiting at 1 request/min, retry_after_ms should be lower
# than 1min.
self.assertTrue(retry_after_ms < 6000)
self.assertLess(retry_after_ms, 6000)
assert retry_header
self.assertLessEqual(int(retry_header[0]), 6)

self.reactor.advance(retry_after_ms / 1000.0 + 1.0)

Expand Down