Skip to content
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

Merged
merged 12 commits into from
Oct 10, 2024

Conversation

vvanglro
Copy link
Contributor

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

  • I understand that this PR may be closed in case there was no previous discussion. (This doesn't apply to typos!)
  • I've added a test for each change that was introduced, and I tried as much as possible to make a single atomic change.
  • I've updated the documentation accordingly.

Comment on lines 1101 to 1111
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
Copy link
Member

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?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, moved to config.

Comment on lines 561 to 571
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
Copy link
Member

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?

Copy link
Contributor Author

@vvanglro vvanglro Aug 20, 2024

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.

Copy link
Contributor Author

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

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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
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

@Kludex Kludex enabled auto-merge (squash) October 10, 2024 05:57
@Kludex Kludex merged commit 079f07a into encode:master Oct 10, 2024
15 checks passed
@vvanglro vvanglro deleted the feat/max_request_limit_warning branch October 10, 2024 08:33
raf-moreau pushed a commit to raf-moreau/uvicorn that referenced this pull request Nov 5, 2024
…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]>
raf-moreau pushed a commit to raf-moreau/uvicorn that referenced this pull request Nov 5, 2024
…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]>
raf-moreau pushed a commit to raf-moreau/uvicorn that referenced this pull request Nov 5, 2024
…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]>
raf-moreau pushed a commit to raf-moreau/uvicorn that referenced this pull request Nov 5, 2024
…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]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants