-
-
Notifications
You must be signed in to change notification settings - Fork 748
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat(server): log warning when max request limit is exceeded #2430
feat(server): log warning when max request limit is exceeded #2430
Conversation
tests/protocols/test_http.py
Outdated
async def test_request_than_limit_max_requests_warn_log( | ||
unused_tcp_port: int, http_protocol_cls: HTTPProtocol, caplog: pytest.LogCaptureFixture | ||
): | ||
caplog.set_level(logging.WARNING, logger="uvicorn.error") | ||
config = Config(app=app, limit_max_requests=1, port=unused_tcp_port, http=http_protocol_cls) | ||
async with run_server(config): | ||
async with httpx.AsyncClient() as client: | ||
tasks = [client.get(f"http://127.0.0.1:{unused_tcp_port}") for _ in range(3)] | ||
responses = await asyncio.gather(*tasks) | ||
assert len(responses) == 3 | ||
assert "Exceeded max request limit ending process." in caplog.text |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This doesn't feel like the right place for this test. We are testing the config/server behavior. Can you find a better place for it, please?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, moved to config.
tests/test_config.py
Outdated
async def test_request_than_limit_max_requests_warn_log( | ||
unused_tcp_port: int, http_protocol_cls: type[H11Protocol | HttpToolsProtocol], caplog: pytest.LogCaptureFixture | ||
): | ||
caplog.set_level(logging.WARNING, logger="uvicorn.error") | ||
config = Config(app=app, limit_max_requests=1, port=unused_tcp_port, http=http_protocol_cls) | ||
async with run_server(config): | ||
async with httpx.AsyncClient() as client: | ||
tasks = [client.get(f"http://127.0.0.1:{unused_tcp_port}") for _ in range(2)] | ||
responses = await asyncio.gather(*tasks) | ||
assert len(responses) == 2 | ||
assert f"Maximum request limit of {config.limit_max_requests} exceeded. Terminating process." in caplog.text |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@vvanglro I don't want to be pedantic... But I want to follow our current structures. I don't think we want to add the run_server
function on this test file.
Is there another way to test this? Or maybe we should move this to elsewhere?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe it should be moved to the test_server.py
, because that's what's in the test server.py
.
If possible, I will commit.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If it's just for test coverage, then it can be.
async def test_request_than_limit_max_requests_warn_log(
unused_tcp_port: int, http_protocol_cls: type[H11Protocol | HttpToolsProtocol], caplog: pytest.LogCaptureFixture
):
caplog.set_level(logging.WARNING, logger="uvicorn.error")
config = Config(app=app, limit_max_requests=0, port=unused_tcp_port, http=http_protocol_cls)
server = Server(config=config)
await server.on_tick(1)
assert f"Maximum request limit of {config.limit_max_requests} exceeded. Terminating process." in caplog.text
tests/test_server.py
Outdated
tasks = [client.get(f"http://127.0.0.1:{unused_tcp_port}") for _ in range(2)] | ||
responses = await asyncio.gather(*tasks) | ||
assert len(responses) == 2 | ||
assert f"Maximum request limit of {config.limit_max_requests} exceeded. Terminating process." in caplog.text |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
assert f"Maximum request limit of {config.limit_max_requests} exceeded. Terminating process." in caplog.text | |
assert f"Maximum request limit of 1 exceeded. Terminating process." in caplog.text |
…2430) * feat(server): log warning when max request limit is exceeded * Make the message cute * better place for test case * fix ruff check * fix app * move to test_server.py * Update * Update cli_usage --------- Co-authored-by: Marcelo Trylesinski <[email protected]>
…2430) * feat(server): log warning when max request limit is exceeded * Make the message cute * better place for test case * fix ruff check * fix app * move to test_server.py * Update * Update cli_usage --------- Co-authored-by: Marcelo Trylesinski <[email protected]>
…2430) * feat(server): log warning when max request limit is exceeded * Make the message cute * better place for test case * fix ruff check * fix app * move to test_server.py * Update * Update cli_usage --------- Co-authored-by: Marcelo Trylesinski <[email protected]>
…2430) * feat(server): log warning when max request limit is exceeded * Make the message cute * better place for test case * fix ruff check * fix app * move to test_server.py * Update * Update cli_usage --------- Co-authored-by: Marcelo Trylesinski <[email protected]>
Summary
ref: #2412
We can't exactly limit the number of requests #2420, so at least we can have a friendly reminder log after meeting the limit.
Checklist