net/http: rename server receiver for consistency

"receiver name `srv` should be consistent with the previous receiver name
`s` for Server" according to go-lint.

Change-Id: I3071ae30b7c1375999e5a599a0eee7b88494f17e
GitHub-Last-Rev: 093c7679b1
GitHub-Pull-Request: golang/go#65787
Reviewed-on: https://go-review.googlesource.com/c/go/+/565175
Auto-Submit: Damien Neil <dneil@google.com>
Reviewed-by: Carlos Amedee <carlos@golang.org>
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
Reviewed-by: Damien Neil <dneil@google.com>
This commit is contained in:
wineandchord 2024-08-12 20:52:05 +00:00 committed by Gopher Robot
parent 85d2eadcf2
commit 206df8e7ad
1 changed files with 91 additions and 91 deletions

View File

@ -628,9 +628,9 @@ func (w *response) ReadFrom(src io.Reader) (n int64, err error) {
const debugServerConnections = false const debugServerConnections = false
// Create new connection from rwc. // Create new connection from rwc.
func (srv *Server) newConn(rwc net.Conn) *conn { func (s *Server) newConn(rwc net.Conn) *conn {
c := &conn{ c := &conn{
server: srv, server: s,
rwc: rwc, rwc: rwc,
} }
if debugServerConnections { if debugServerConnections {
@ -915,15 +915,15 @@ func putBufioWriter(bw *bufio.Writer) {
// This can be overridden by setting [Server.MaxHeaderBytes]. // This can be overridden by setting [Server.MaxHeaderBytes].
const DefaultMaxHeaderBytes = 1 << 20 // 1 MB const DefaultMaxHeaderBytes = 1 << 20 // 1 MB
func (srv *Server) maxHeaderBytes() int { func (s *Server) maxHeaderBytes() int {
if srv.MaxHeaderBytes > 0 { if s.MaxHeaderBytes > 0 {
return srv.MaxHeaderBytes return s.MaxHeaderBytes
} }
return DefaultMaxHeaderBytes return DefaultMaxHeaderBytes
} }
func (srv *Server) initialReadLimitSize() int64 { func (s *Server) initialReadLimitSize() int64 {
return int64(srv.maxHeaderBytes()) + 4096 // bufio slop return int64(s.maxHeaderBytes()) + 4096 // bufio slop
} }
// tlsHandshakeTimeout returns the time limit permitted for the TLS // tlsHandshakeTimeout returns the time limit permitted for the TLS
@ -931,12 +931,12 @@ func (srv *Server) initialReadLimitSize() int64 {
// //
// It returns the minimum of any positive ReadHeaderTimeout, // It returns the minimum of any positive ReadHeaderTimeout,
// ReadTimeout, or WriteTimeout. // ReadTimeout, or WriteTimeout.
func (srv *Server) tlsHandshakeTimeout() time.Duration { func (s *Server) tlsHandshakeTimeout() time.Duration {
var ret time.Duration var ret time.Duration
for _, v := range [...]time.Duration{ for _, v := range [...]time.Duration{
srv.ReadHeaderTimeout, s.ReadHeaderTimeout,
srv.ReadTimeout, s.ReadTimeout,
srv.WriteTimeout, s.WriteTimeout,
} { } {
if v <= 0 { if v <= 0 {
continue continue
@ -2996,23 +2996,23 @@ type Server struct {
// //
// Close returns any error returned from closing the [Server]'s // Close returns any error returned from closing the [Server]'s
// underlying Listener(s). // underlying Listener(s).
func (srv *Server) Close() error { func (s *Server) Close() error {
srv.inShutdown.Store(true) s.inShutdown.Store(true)
srv.mu.Lock() s.mu.Lock()
defer srv.mu.Unlock() defer s.mu.Unlock()
err := srv.closeListenersLocked() err := s.closeListenersLocked()
// Unlock srv.mu while waiting for listenerGroup. // Unlock s.mu while waiting for listenerGroup.
// The group Add and Done calls are made with srv.mu held, // The group Add and Done calls are made with s.mu held,
// to avoid adding a new listener in the window between // to avoid adding a new listener in the window between
// us setting inShutdown above and waiting here. // us setting inShutdown above and waiting here.
srv.mu.Unlock() s.mu.Unlock()
srv.listenerGroup.Wait() s.listenerGroup.Wait()
srv.mu.Lock() s.mu.Lock()
for c := range srv.activeConn { for c := range s.activeConn {
c.rwc.Close() c.rwc.Close()
delete(srv.activeConn, c) delete(s.activeConn, c)
} }
return err return err
} }
@ -3046,16 +3046,16 @@ const shutdownPollIntervalMax = 500 * time.Millisecond
// //
// Once Shutdown has been called on a server, it may not be reused; // Once Shutdown has been called on a server, it may not be reused;
// future calls to methods such as Serve will return ErrServerClosed. // future calls to methods such as Serve will return ErrServerClosed.
func (srv *Server) Shutdown(ctx context.Context) error { func (s *Server) Shutdown(ctx context.Context) error {
srv.inShutdown.Store(true) s.inShutdown.Store(true)
srv.mu.Lock() s.mu.Lock()
lnerr := srv.closeListenersLocked() lnerr := s.closeListenersLocked()
for _, f := range srv.onShutdown { for _, f := range s.onShutdown {
go f() go f()
} }
srv.mu.Unlock() s.mu.Unlock()
srv.listenerGroup.Wait() s.listenerGroup.Wait()
pollIntervalBase := time.Millisecond pollIntervalBase := time.Millisecond
nextPollInterval := func() time.Duration { nextPollInterval := func() time.Duration {
@ -3072,7 +3072,7 @@ func (srv *Server) Shutdown(ctx context.Context) error {
timer := time.NewTimer(nextPollInterval()) timer := time.NewTimer(nextPollInterval())
defer timer.Stop() defer timer.Stop()
for { for {
if srv.closeIdleConns() { if s.closeIdleConns() {
return lnerr return lnerr
} }
select { select {
@ -3089,10 +3089,10 @@ func (srv *Server) Shutdown(ctx context.Context) error {
// undergone ALPN protocol upgrade or that have been hijacked. // undergone ALPN protocol upgrade or that have been hijacked.
// This function should start protocol-specific graceful shutdown, // This function should start protocol-specific graceful shutdown,
// but should not wait for shutdown to complete. // but should not wait for shutdown to complete.
func (srv *Server) RegisterOnShutdown(f func()) { func (s *Server) RegisterOnShutdown(f func()) {
srv.mu.Lock() s.mu.Lock()
srv.onShutdown = append(srv.onShutdown, f) s.onShutdown = append(s.onShutdown, f)
srv.mu.Unlock() s.mu.Unlock()
} }
// closeIdleConns closes all idle connections and reports whether the // closeIdleConns closes all idle connections and reports whether the
@ -3236,19 +3236,19 @@ func AllowQuerySemicolons(h Handler) Handler {
}) })
} }
// ListenAndServe listens on the TCP network address srv.Addr and then // ListenAndServe listens on the TCP network address s.Addr and then
// calls [Serve] to handle requests on incoming connections. // calls [Serve] to handle requests on incoming connections.
// Accepted connections are configured to enable TCP keep-alives. // Accepted connections are configured to enable TCP keep-alives.
// //
// If srv.Addr is blank, ":http" is used. // If s.Addr is blank, ":http" is used.
// //
// ListenAndServe always returns a non-nil error. After [Server.Shutdown] or [Server.Close], // ListenAndServe always returns a non-nil error. After [Server.Shutdown] or [Server.Close],
// the returned error is [ErrServerClosed]. // the returned error is [ErrServerClosed].
func (srv *Server) ListenAndServe() error { func (s *Server) ListenAndServe() error {
if srv.shuttingDown() { if s.shuttingDown() {
return ErrServerClosed return ErrServerClosed
} }
addr := srv.Addr addr := s.Addr
if addr == "" { if addr == "" {
addr = ":http" addr = ":http"
} }
@ -3256,20 +3256,20 @@ func (srv *Server) ListenAndServe() error {
if err != nil { if err != nil {
return err return err
} }
return srv.Serve(ln) return s.Serve(ln)
} }
var testHookServerServe func(*Server, net.Listener) // used if non-nil var testHookServerServe func(*Server, net.Listener) // used if non-nil
// shouldConfigureHTTP2ForServe reports whether Server.Serve should configure // shouldConfigureHTTP2ForServe reports whether Server.Serve should configure
// automatic HTTP/2. (which sets up the srv.TLSNextProto map) // automatic HTTP/2. (which sets up the s.TLSNextProto map)
func (srv *Server) shouldConfigureHTTP2ForServe() bool { func (s *Server) shouldConfigureHTTP2ForServe() bool {
if srv.TLSConfig == nil { if s.TLSConfig == nil {
// Compatibility with Go 1.6: // Compatibility with Go 1.6:
// If there's no TLSConfig, it's possible that the user just // If there's no TLSConfig, it's possible that the user just
// didn't set it on the http.Server, but did pass it to // didn't set it on the http.Server, but did pass it to
// tls.NewListener and passed that listener to Serve. // tls.NewListener and passed that listener to Serve.
// So we should configure HTTP/2 (to set up srv.TLSNextProto) // So we should configure HTTP/2 (to set up s.TLSNextProto)
// in case the listener returns an "h2" *tls.Conn. // in case the listener returns an "h2" *tls.Conn.
return true return true
} }
@ -3280,7 +3280,7 @@ func (srv *Server) shouldConfigureHTTP2ForServe() bool {
// passed this tls.Config to tls.NewListener. And if they did, // passed this tls.Config to tls.NewListener. And if they did,
// it's too late anyway to fix it. It would only be potentially racy. // it's too late anyway to fix it. It would only be potentially racy.
// See Issue 15908. // See Issue 15908.
return slices.Contains(srv.TLSConfig.NextProtos, http2NextProtoTLS) return slices.Contains(s.TLSConfig.NextProtos, http2NextProtoTLS)
} }
// ErrServerClosed is returned by the [Server.Serve], [ServeTLS], [ListenAndServe], // ErrServerClosed is returned by the [Server.Serve], [ServeTLS], [ListenAndServe],
@ -3289,7 +3289,7 @@ var ErrServerClosed = errors.New("http: Server closed")
// Serve accepts incoming connections on the Listener l, creating a // Serve accepts incoming connections on the Listener l, creating a
// new service goroutine for each. The service goroutines read requests and // new service goroutine for each. The service goroutines read requests and
// then call srv.Handler to reply to them. // then call s.Handler to reply to them.
// //
// HTTP/2 support is only enabled if the Listener returns [*tls.Conn] // HTTP/2 support is only enabled if the Listener returns [*tls.Conn]
// connections and they were configured with "h2" in the TLS // connections and they were configured with "h2" in the TLS
@ -3297,27 +3297,27 @@ var ErrServerClosed = errors.New("http: Server closed")
// //
// Serve always returns a non-nil error and closes l. // Serve always returns a non-nil error and closes l.
// After [Server.Shutdown] or [Server.Close], the returned error is [ErrServerClosed]. // After [Server.Shutdown] or [Server.Close], the returned error is [ErrServerClosed].
func (srv *Server) Serve(l net.Listener) error { func (s *Server) Serve(l net.Listener) error {
if fn := testHookServerServe; fn != nil { if fn := testHookServerServe; fn != nil {
fn(srv, l) // call hook with unwrapped listener fn(s, l) // call hook with unwrapped listener
} }
origListener := l origListener := l
l = &onceCloseListener{Listener: l} l = &onceCloseListener{Listener: l}
defer l.Close() defer l.Close()
if err := srv.setupHTTP2_Serve(); err != nil { if err := s.setupHTTP2_Serve(); err != nil {
return err return err
} }
if !srv.trackListener(&l, true) { if !s.trackListener(&l, true) {
return ErrServerClosed return ErrServerClosed
} }
defer srv.trackListener(&l, false) defer s.trackListener(&l, false)
baseCtx := context.Background() baseCtx := context.Background()
if srv.BaseContext != nil { if s.BaseContext != nil {
baseCtx = srv.BaseContext(origListener) baseCtx = s.BaseContext(origListener)
if baseCtx == nil { if baseCtx == nil {
panic("BaseContext returned a nil context") panic("BaseContext returned a nil context")
} }
@ -3325,11 +3325,11 @@ func (srv *Server) Serve(l net.Listener) error {
var tempDelay time.Duration // how long to sleep on accept failure var tempDelay time.Duration // how long to sleep on accept failure
ctx := context.WithValue(baseCtx, ServerContextKey, srv) ctx := context.WithValue(baseCtx, ServerContextKey, s)
for { for {
rw, err := l.Accept() rw, err := l.Accept()
if err != nil { if err != nil {
if srv.shuttingDown() { if s.shuttingDown() {
return ErrServerClosed return ErrServerClosed
} }
if ne, ok := err.(net.Error); ok && ne.Temporary() { if ne, ok := err.(net.Error); ok && ne.Temporary() {
@ -3341,21 +3341,21 @@ func (srv *Server) Serve(l net.Listener) error {
if max := 1 * time.Second; tempDelay > max { if max := 1 * time.Second; tempDelay > max {
tempDelay = max tempDelay = max
} }
srv.logf("http: Accept error: %v; retrying in %v", err, tempDelay) s.logf("http: Accept error: %v; retrying in %v", err, tempDelay)
time.Sleep(tempDelay) time.Sleep(tempDelay)
continue continue
} }
return err return err
} }
connCtx := ctx connCtx := ctx
if cc := srv.ConnContext; cc != nil { if cc := s.ConnContext; cc != nil {
connCtx = cc(connCtx, rw) connCtx = cc(connCtx, rw)
if connCtx == nil { if connCtx == nil {
panic("ConnContext returned nil") panic("ConnContext returned nil")
} }
} }
tempDelay = 0 tempDelay = 0
c := srv.newConn(rw) c := s.newConn(rw)
c.setState(c.rwc, StateNew, runHooks) // before Serve can return c.setState(c.rwc, StateNew, runHooks) // before Serve can return
go c.serve(connCtx) go c.serve(connCtx)
} }
@ -3363,7 +3363,7 @@ func (srv *Server) Serve(l net.Listener) error {
// ServeTLS accepts incoming connections on the Listener l, creating a // ServeTLS accepts incoming connections on the Listener l, creating a
// new service goroutine for each. The service goroutines perform TLS // new service goroutine for each. The service goroutines perform TLS
// setup and then read requests, calling srv.Handler to reply to them. // setup and then read requests, calling s.Handler to reply to them.
// //
// Files containing a certificate and matching private key for the // Files containing a certificate and matching private key for the
// server must be provided if neither the [Server]'s // server must be provided if neither the [Server]'s
@ -3375,14 +3375,14 @@ func (srv *Server) Serve(l net.Listener) error {
// //
// ServeTLS always returns a non-nil error. After [Server.Shutdown] or [Server.Close], the // ServeTLS always returns a non-nil error. After [Server.Shutdown] or [Server.Close], the
// returned error is [ErrServerClosed]. // returned error is [ErrServerClosed].
func (srv *Server) ServeTLS(l net.Listener, certFile, keyFile string) error { func (s *Server) ServeTLS(l net.Listener, certFile, keyFile string) error {
// Setup HTTP/2 before srv.Serve, to initialize srv.TLSConfig // Setup HTTP/2 before s.Serve, to initialize s.TLSConfig
// before we clone it and create the TLS Listener. // before we clone it and create the TLS Listener.
if err := srv.setupHTTP2_ServeTLS(); err != nil { if err := s.setupHTTP2_ServeTLS(); err != nil {
return err return err
} }
config := cloneTLSConfig(srv.TLSConfig) config := cloneTLSConfig(s.TLSConfig)
if !slices.Contains(config.NextProtos, "http/1.1") { if !slices.Contains(config.NextProtos, "http/1.1") {
config.NextProtos = append(config.NextProtos, "http/1.1") config.NextProtos = append(config.NextProtos, "http/1.1")
} }
@ -3398,7 +3398,7 @@ func (srv *Server) ServeTLS(l net.Listener, certFile, keyFile string) error {
} }
tlsListener := tls.NewListener(l, config) tlsListener := tls.NewListener(l, config)
return srv.Serve(tlsListener) return s.Serve(tlsListener)
} }
// trackListener adds or removes a net.Listener to the set of tracked // trackListener adds or removes a net.Listener to the set of tracked
@ -3469,15 +3469,15 @@ func (s *Server) shuttingDown() bool {
// By default, keep-alives are always enabled. Only very // By default, keep-alives are always enabled. Only very
// resource-constrained environments or servers in the process of // resource-constrained environments or servers in the process of
// shutting down should disable them. // shutting down should disable them.
func (srv *Server) SetKeepAlivesEnabled(v bool) { func (s *Server) SetKeepAlivesEnabled(v bool) {
if v { if v {
srv.disableKeepAlives.Store(false) s.disableKeepAlives.Store(false)
return return
} }
srv.disableKeepAlives.Store(true) s.disableKeepAlives.Store(true)
// Close idle HTTP/1 conns: // Close idle HTTP/1 conns:
srv.closeIdleConns() s.closeIdleConns()
// TODO: Issue 26303: close HTTP/2 conns as soon as they become idle. // TODO: Issue 26303: close HTTP/2 conns as soon as they become idle.
} }
@ -3524,7 +3524,7 @@ func ListenAndServeTLS(addr, certFile, keyFile string, handler Handler) error {
return server.ListenAndServeTLS(certFile, keyFile) return server.ListenAndServeTLS(certFile, keyFile)
} }
// ListenAndServeTLS listens on the TCP network address srv.Addr and // ListenAndServeTLS listens on the TCP network address s.Addr and
// then calls [ServeTLS] to handle requests on incoming TLS connections. // then calls [ServeTLS] to handle requests on incoming TLS connections.
// Accepted connections are configured to enable TCP keep-alives. // Accepted connections are configured to enable TCP keep-alives.
// //
@ -3535,15 +3535,15 @@ func ListenAndServeTLS(addr, certFile, keyFile string, handler Handler) error {
// concatenation of the server's certificate, any intermediates, and // concatenation of the server's certificate, any intermediates, and
// the CA's certificate. // the CA's certificate.
// //
// If srv.Addr is blank, ":https" is used. // If s.Addr is blank, ":https" is used.
// //
// ListenAndServeTLS always returns a non-nil error. After [Server.Shutdown] or // ListenAndServeTLS always returns a non-nil error. After [Server.Shutdown] or
// [Server.Close], the returned error is [ErrServerClosed]. // [Server.Close], the returned error is [ErrServerClosed].
func (srv *Server) ListenAndServeTLS(certFile, keyFile string) error { func (s *Server) ListenAndServeTLS(certFile, keyFile string) error {
if srv.shuttingDown() { if s.shuttingDown() {
return ErrServerClosed return ErrServerClosed
} }
addr := srv.Addr addr := s.Addr
if addr == "" { if addr == "" {
addr = ":https" addr = ":https"
} }
@ -3555,42 +3555,42 @@ func (srv *Server) ListenAndServeTLS(certFile, keyFile string) error {
defer ln.Close() defer ln.Close()
return srv.ServeTLS(ln, certFile, keyFile) return s.ServeTLS(ln, certFile, keyFile)
} }
// setupHTTP2_ServeTLS conditionally configures HTTP/2 on // setupHTTP2_ServeTLS conditionally configures HTTP/2 on
// srv and reports whether there was an error setting it up. If it is // s and reports whether there was an error setting it up. If it is
// not configured for policy reasons, nil is returned. // not configured for policy reasons, nil is returned.
func (srv *Server) setupHTTP2_ServeTLS() error { func (s *Server) setupHTTP2_ServeTLS() error {
srv.nextProtoOnce.Do(srv.onceSetNextProtoDefaults) s.nextProtoOnce.Do(s.onceSetNextProtoDefaults)
return srv.nextProtoErr return s.nextProtoErr
} }
// setupHTTP2_Serve is called from (*Server).Serve and conditionally // setupHTTP2_Serve is called from (*Server).Serve and conditionally
// configures HTTP/2 on srv using a more conservative policy than // configures HTTP/2 on s using a more conservative policy than
// setupHTTP2_ServeTLS because Serve is called after tls.Listen, // setupHTTP2_ServeTLS because Serve is called after tls.Listen,
// and may be called concurrently. See shouldConfigureHTTP2ForServe. // and may be called concurrently. See shouldConfigureHTTP2ForServe.
// //
// The tests named TestTransportAutomaticHTTP2* and // The tests named TestTransportAutomaticHTTP2* and
// TestConcurrentServerServe in server_test.go demonstrate some // TestConcurrentServerServe in server_test.go demonstrate some
// of the supported use cases and motivations. // of the supported use cases and motivations.
func (srv *Server) setupHTTP2_Serve() error { func (s *Server) setupHTTP2_Serve() error {
srv.nextProtoOnce.Do(srv.onceSetNextProtoDefaults_Serve) s.nextProtoOnce.Do(s.onceSetNextProtoDefaults_Serve)
return srv.nextProtoErr return s.nextProtoErr
} }
func (srv *Server) onceSetNextProtoDefaults_Serve() { func (s *Server) onceSetNextProtoDefaults_Serve() {
if srv.shouldConfigureHTTP2ForServe() { if s.shouldConfigureHTTP2ForServe() {
srv.onceSetNextProtoDefaults() s.onceSetNextProtoDefaults()
} }
} }
var http2server = godebug.New("http2server") var http2server = godebug.New("http2server")
// onceSetNextProtoDefaults configures HTTP/2, if the user hasn't // onceSetNextProtoDefaults configures HTTP/2, if the user hasn't
// configured otherwise. (by setting srv.TLSNextProto non-nil) // configured otherwise. (by setting s.TLSNextProto non-nil)
// It must only be called via srv.nextProtoOnce (use srv.setupHTTP2_*). // It must only be called via s.nextProtoOnce (use s.setupHTTP2_*).
func (srv *Server) onceSetNextProtoDefaults() { func (s *Server) onceSetNextProtoDefaults() {
if omitBundledHTTP2 { if omitBundledHTTP2 {
return return
} }
@ -3600,9 +3600,9 @@ func (srv *Server) onceSetNextProtoDefaults() {
} }
// Enable HTTP/2 by default if the user hasn't otherwise // Enable HTTP/2 by default if the user hasn't otherwise
// configured their TLSNextProto map. // configured their TLSNextProto map.
if srv.TLSNextProto == nil { if s.TLSNextProto == nil {
conf := &http2Server{} conf := &http2Server{}
srv.nextProtoErr = http2ConfigureServer(srv, conf) s.nextProtoErr = http2ConfigureServer(s, conf)
} }
} }