Skip to content

Commit

Permalink
server: reduce clone request by using base context
Browse files Browse the repository at this point in the history
  • Loading branch information
acoshift committed May 27, 2023
1 parent 2545817 commit bbe5ebb
Showing 1 changed file with 8 additions and 5 deletions.
13 changes: 8 additions & 5 deletions server.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ type Server struct {
ReusePort bool
ConnState func(conn net.Conn, state http.ConnState)
TLSConfig *tls.Config
BaseContext func(net.Listener) context.Context
}

type serverContextKey struct{}
Expand Down Expand Up @@ -86,12 +87,14 @@ func (s *Server) configHandler() {
Trust: s.TrustProxy,
Handler: h,
}
h = func(h http.Handler) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
ctx := context.WithValue(r.Context(), ServerContextKey, s)
h.ServeHTTP(w, r.WithContext(ctx))
s.s.BaseContext = func(l net.Listener) context.Context {
ctx := context.Background()
if s.BaseContext != nil {
ctx = s.BaseContext(l)
}
}(h)
ctx = context.WithValue(ctx, ServerContextKey, s)
return ctx
}
if s.H2C {
h = h2c.NewHandler(h, &http2.Server{})
}
Expand Down

0 comments on commit bbe5ebb

Please sign in to comment.