Skip to content

Commit

Permalink
Revert "Use sync.Map for http.mux.wildcards (#3417)"
Browse files Browse the repository at this point in the history
This reverts commit ab8c54d.
  • Loading branch information
tchssk committed Nov 27, 2023
1 parent 8bbad70 commit 454877d
Showing 1 changed file with 7 additions and 8 deletions.
15 changes: 7 additions & 8 deletions http/mux.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,15 +77,15 @@ type (
middlewares []func(http.Handler) http.Handler
// wildcards maps a method and a pattern to the name of the wildcard
// this is needed because chi does not expose the name of the wildcard
wildcards sync.Map
wildcards map[string]string
}
)

// NewMuxer returns a Muxer implementation based on a Chi router.
func NewMuxer() ResolverMuxer {
return &mux{
Router: chi.NewRouter(),
wildcards: sync.Map{},
wildcards: make(map[string]string),
middlewares: []func(http.Handler) http.Handler{},
}
}
Expand Down Expand Up @@ -114,7 +114,7 @@ func (m *mux) Handle(method, pattern string, handler http.HandlerFunc) {
panic("too many wildcards")
}
pattern = wildPath.ReplaceAllString(pattern, "/*")
m.wildcards.Store(method+"::"+pattern, wildcards[1])
m.wildcards[method+"::"+pattern] = wildcards[1]
}
m.Method(method, pattern, handler)
}
Expand All @@ -132,9 +132,8 @@ func (m *mux) Vars(r *http.Request) map[string]string {
vars := make(map[string]string, len(params.Keys))
for i, k := range params.Keys {
if k == "*" {
if wildcard, ok := m.wildcards.Load(r.Method + "::" + ctx.RoutePattern()); ok {
vars[wildcard.(string)] = unescape(params.Values[i])
}
wildcard := m.wildcards[r.Method+"::"+ctx.RoutePattern()]
vars[wildcard] = unescape(params.Values[i])
continue
}
vars[k] = unescape(params.Values[i])
Expand Down Expand Up @@ -175,8 +174,8 @@ func (m *mux) ResolvePattern(r *http.Request) string {
// resolveWildcard returns the route pattern with the wildcard replaced by the
// name of the wildcard.
func (m *mux) resolveWildcard(method, pattern string) string {
if wildcard, ok := m.wildcards.Load(method + "::" + pattern); ok {
return pattern[:len(pattern)-2] + "/{*" + wildcard.(string) + "}"
if wildcard, ok := m.wildcards[method+"::"+pattern]; ok {
return pattern[:len(pattern)-2] + "/{*" + wildcard + "}"
}
return pattern
}
Expand Down

0 comments on commit 454877d

Please sign in to comment.