Open
Conversation
7 tasks
RensDimmendaal
commented
Apr 17, 2026
| def __init__(self, app): | ||
| self._req_var = ContextVar('limiter_req') | ||
| async def _store(req): self._req_var.set(req) | ||
| app.before.insert(0, _store) |
Author
There was a problem hiding this comment.
Ive been wondering if we could do this in a cleaner way. The issue is that req needs to be available in the decorator even when the route doesnt have req as a param.
If we can do that then Limiter can just be a function.
Author
There was a problem hiding this comment.
One way could do it is incore.py create this global current_req ContextVar...but I'm not sure if thats agood or a really bad idea...Curious what you think!
+current_req = ContextVar('fh_current_req', default=None)
+
+@contextmanager
+def req_ctx(req):
+ token = current_req.set(req)
+ try: yield
+ finally: current_req.reset(token)
@patch
def _endp(self:FastHTML, f, body_wrap):
"Create endpoint wrapper with before/after middleware processing"
sig = signature_ex(f, True)
for n,p in sig.parameters.items(): (msg:=_check_anno(n,p.annotation)) and warn(msg)
async def _f(req):
+ with req_ctx(req):
... # rest as before
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
This PR adds a token-bucket rate limiter for FastHTML routes. I figured we needed something like this for the MagicKey PR to avoid abuse of the routes that send emails and I saw it was a common pattern for other web app frameworks. What's different about this implementation is that I've made it easy to re-use the request parameters as a
keyin thelimiter.I didn't add a separate explainer doc page, because the source doc page is quite extensive and builds up nicely. But i can add one if you like.
I've uploaded a demo app here
Example usage:
Types of changes
What types of changes does your code introduce? Put an
xin all the boxes that apply:Checklist
Go over all the following points, and put an
xin all the boxes that apply:Additional Information
The changes to nbs 00 and 01 are due to the latest nbdev-version and not related to this PR.