-
Notifications
You must be signed in to change notification settings - Fork 17.7k
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
net/http: expose matched pattern in Request #66405
Comments
see also #61551 |
Similar to #61551 but instead of setting pattern info in Context, I suggest we export it directly in http.Request where it already has that info and the pattern is actually corresponding to request itself instead of any Context. |
Maybe the string pattern value could be made accessible via special name |
We definitely need a way to get pattern info in lower cost instead of using For now there are three suggestions for getting that
|
I wouldn't be opposed to adding:
|
@sillydong, if you're OK with my suggestion above, please edit the top post to refer to my comment. You may also want to edit the issue title. |
Pattern() string
to *http.Request to expose pattern info of current request
What about custom mux implementations? Should they be able to store whatever pattern they used in the |
Perhaps there should be some way for custom muxes to set |
it can be set with Request.SetPathValue |
Today I learned! In that case, I agree that there should be a corresponding |
I'm OK with a |
To expose the |
Change https://go.dev/cl/574997 mentions this issue: |
…tern info SetPattern will parse string with parsePattern function. This allows third party frameworks set pattern info in requests. Pattern will return matched pattern string if it is set. Fixes golang#66405
Or we could just have:
That seems pretty simple. If we do have a |
@neild No. Pattern is actually a structured data. It contains segments and pattern string itself. Segments are used while getting path value. If we turn pattern into string, we lost everything. I have created PR for |
@sillydong I think you're missing @neild's point. Your code for |
I think both you guys explained makes sense. But the problem is if we provide a function to set a string pattern, we have to create a new field to save that string info. That makes us not able to take advantage of Requst.pat. And also the Pattern function would have to return that string value instead of the real pattern string in Request.pat.str. This is kind of weird. I'm not quite sure about if this is a good way to handle the pattern info. |
We would be duplicating information, that's true. |
@sillydong My code was typed without testing, thanks for fixing it. Can I ask you once again to change the top post to reflect the current proposal, which is to have a Are there any unresolved objections to that? |
FWIW, it is usually clearer to post the updated proposal in a new message, and then update the first comment to just say "The latest proposal is <link to message>." than to rewrite history. Rewriting history is confusing to anyone trying to get up to speed on the conversation. |
This comment describes the current proposal. Add a |
Instead of checking @rsc Proposal is updated and code change has been done. Please help push forward the progress. Thanks a lot. PR: golang.org/cl/574997 |
Have all remaining concerns about this proposal been addressed? The proposal is to add a Pattern string field to net/http.Request. |
Does accepting this mean declining #61551 ? |
I think yes. I don't believe that proposal adds anything once we have
No. I don't even know what that would mean in some cases. For example, say the pattern is |
So there's #64909 to teach StripPrefix about path patterns. I was wondering about a more limited form where the pattern is processed the same as |
@rsc @jba Hi, what do you think of this update? I think it's fair enough to also update the pattern for mux121 since it does have the pattern info from |
We were away last week. This issue will move to likely accept this week. We should leave the old mux behavior unchanged. It is still accessible from a GODEBUG but we want people to move off it. We are not updating it, and eventually it may be removed. Adding new API to it gives the opposite impression. |
Based on the discussion above, this proposal seems like a likely accept. The proposal is to add a Pattern string field to net/http.Request. |
No change in consensus, so accepted. 🎉 The proposal is to add a Pattern string field to net/http.Request. |
Proposal Details
Go 1.22 has provided
http.ServeMux
with supporting of pattern match for requests. While I'm using it, I tried to get the matched pattern of that request. With help of*ServeMux.Handler
, I can successfully get matched handler and pattern, but later when I run debug of the code, I found in*http.Request
itself, there is a privatepat
which is exactly matched pattern info. I suggest we export thepat
for public access or provide some function to allow user to get the matched pattern, so that we can avoid much logic to use*ServeMux.Handler
to parse the request again. This reduces huge resource usage in high load services.The matched pattern info is usually very useful when we do analysis for requests. We can group requests by their pattern and make the analysis more generic. This is a very high frequency usage scenario which makes reducing the resource usage very worthy.
sample code:
When we do request
GET http://127.0.0.1:8999/aloha
, we can see such info in debug console.Update 2024/03/28:
As we talked in comments, @jba 's suggestion is a good solution #66405 (comment)
Update 2024/04/11:
After many discussions, the latest proposal can be found here #66405 (comment)
In short, add a
Pattern
field inhttp.Request
to expose matched pattern info and can be set by third party routers.Update 2024/05/13:
Final design: #66405 (comment)
The proposal is to add a Pattern string field to net/http.Request.
ServeMux.ServeHTTP will set the field to the pattern of the matching handler before dispatching to that handler when using the new Go 1.22 HTTP mux.
In Go 1.21 compatibility mode the field is unset.
The field is otherwise left untouched. Third-party routers may set it if they wish.
The text was updated successfully, but these errors were encountered: