fix: optimizate with context (#1460)

* fix: optimizate with context
pull/1462/head
seasrain 3 years ago committed by GitHub
parent 4bdafa6393
commit 8b1086ad56
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 13
      transport/http/server.go

@ -181,12 +181,17 @@ func (s *Server) ServeHTTP(res http.ResponseWriter, req *http.Request) {
func (s *Server) filter() mux.MiddlewareFunc { func (s *Server) filter() mux.MiddlewareFunc {
return func(next http.Handler) http.Handler { return func(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) { return http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
ctx, cancel := context.WithCancel(req.Context()) var (
defer cancel() ctx context.Context
cancel context.CancelFunc
)
if s.timeout > 0 { if s.timeout > 0 {
ctx, cancel = context.WithTimeout(ctx, s.timeout) ctx, cancel = context.WithTimeout(req.Context(), s.timeout)
defer cancel() } else {
ctx, cancel = context.WithCancel(req.Context())
} }
defer cancel()
pathTemplate := req.URL.Path pathTemplate := req.URL.Path
if route := mux.CurrentRoute(req); route != nil { if route := mux.CurrentRoute(req); route != nil {
// /path/123 -> /path/{id} // /path/123 -> /path/{id}

Loading…
Cancel
Save