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

Add request counter #129

Merged
merged 6 commits into from
Sep 22, 2022
Merged

Add request counter #129

merged 6 commits into from
Sep 22, 2022

Conversation

ahopkins
Copy link
Member

Coming off the discussion re: the worker manager, I implemented the idea of adding a request counter. I thought of a few different strategies. Most important to me was that it not be forced upon users who do not opt in.

The obvious first choice was of course config. But, I do not want to even include a check for this on every request. I thought about using a touchup scheme, but we do not have one that is currently setup for general conditional checks. Maybe we should, but that is outside the scope here.

Doing it on Request.__init__ is the easiest. So, I thought perhaps as a subclass we can get the result without any of the consequences. It does however bring the question about whether a self-contained opinionated piece of logic belongs in the main project at all, or if it should be pushed over to Sanic Extensions.

Thoughts?


Implementation:

from sanic.request import CountedRequest

app = Sanic(..., request_class=CountedRequest)

@app.get("/")
async def handler(request: CountedRequest):
    return json({"count": request.count})

FYI - the result will show in the worker state

image

Copy link
Member

@ChihweiLHBird ChihweiLHBird left a comment

Choose a reason for hiding this comment

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

Looks good to me.

But test automation failed?

@ahopkins
Copy link
Member Author

Looks good to me.

But test automation failed?

Yeah, have not checked. But I am 99% sure it is because we need to use the unreleased sanic-testing because of the changes to the workers in core.

@ahopkins ahopkins merged commit 293d884 into main Sep 22, 2022
@ahopkins ahopkins deleted the request-counter branch September 22, 2022 07:33
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