Skip to content

Commit ad17790

Browse files
committed
net/http: rename server receiver to keep consistency
1 parent 5c92f43 commit ad17790

File tree

1 file changed

+81
-81
lines changed

1 file changed

+81
-81
lines changed

src/net/http/server.go

Lines changed: 81 additions & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -619,9 +619,9 @@ func (w *response) ReadFrom(src io.Reader) (n int64, err error) {
619619
const debugServerConnections = false
620620

621621
// Create new connection from rwc.
622-
func (srv *Server) newConn(rwc net.Conn) *conn {
622+
func (s *Server) newConn(rwc net.Conn) *conn {
623623
c := &conn{
624-
server: srv,
624+
server: s,
625625
rwc: rwc,
626626
}
627627
if debugServerConnections {
@@ -870,28 +870,28 @@ func putBufioWriter(bw *bufio.Writer) {
870870
// This can be overridden by setting [Server.MaxHeaderBytes].
871871
const DefaultMaxHeaderBytes = 1 << 20 // 1 MB
872872

873-
func (srv *Server) maxHeaderBytes() int {
874-
if srv.MaxHeaderBytes > 0 {
875-
return srv.MaxHeaderBytes
873+
func (s *Server) maxHeaderBytes() int {
874+
if s.MaxHeaderBytes > 0 {
875+
return s.MaxHeaderBytes
876876
}
877877
return DefaultMaxHeaderBytes
878878
}
879879

880-
func (srv *Server) initialReadLimitSize() int64 {
881-
return int64(srv.maxHeaderBytes()) + 4096 // bufio slop
880+
func (s *Server) initialReadLimitSize() int64 {
881+
return int64(s.maxHeaderBytes()) + 4096 // bufio slop
882882
}
883883

884884
// tlsHandshakeTimeout returns the time limit permitted for the TLS
885885
// handshake, or zero for unlimited.
886886
//
887887
// It returns the minimum of any positive ReadHeaderTimeout,
888888
// ReadTimeout, or WriteTimeout.
889-
func (srv *Server) tlsHandshakeTimeout() time.Duration {
889+
func (s *Server) tlsHandshakeTimeout() time.Duration {
890890
var ret time.Duration
891891
for _, v := range [...]time.Duration{
892-
srv.ReadHeaderTimeout,
893-
srv.ReadTimeout,
894-
srv.WriteTimeout,
892+
s.ReadHeaderTimeout,
893+
s.ReadTimeout,
894+
s.WriteTimeout,
895895
} {
896896
if v <= 0 {
897897
continue
@@ -2932,23 +2932,23 @@ type Server struct {
29322932
//
29332933
// Close returns any error returned from closing the [Server]'s
29342934
// underlying Listener(s).
2935-
func (srv *Server) Close() error {
2936-
srv.inShutdown.Store(true)
2937-
srv.mu.Lock()
2938-
defer srv.mu.Unlock()
2939-
err := srv.closeListenersLocked()
2940-
2941-
// Unlock srv.mu while waiting for listenerGroup.
2942-
// The group Add and Done calls are made with srv.mu held,
2935+
func (s *Server) Close() error {
2936+
s.inShutdown.Store(true)
2937+
s.mu.Lock()
2938+
defer s.mu.Unlock()
2939+
err := s.closeListenersLocked()
2940+
2941+
// Unlock s.mu while waiting for listenerGroup.
2942+
// The group Add and Done calls are made with s.mu held,
29432943
// to avoid adding a new listener in the window between
29442944
// us setting inShutdown above and waiting here.
2945-
srv.mu.Unlock()
2946-
srv.listenerGroup.Wait()
2947-
srv.mu.Lock()
2945+
s.mu.Unlock()
2946+
s.listenerGroup.Wait()
2947+
s.mu.Lock()
29482948

2949-
for c := range srv.activeConn {
2949+
for c := range s.activeConn {
29502950
c.rwc.Close()
2951-
delete(srv.activeConn, c)
2951+
delete(s.activeConn, c)
29522952
}
29532953
return err
29542954
}
@@ -2982,16 +2982,16 @@ const shutdownPollIntervalMax = 500 * time.Millisecond
29822982
//
29832983
// Once Shutdown has been called on a server, it may not be reused;
29842984
// future calls to methods such as Serve will return ErrServerClosed.
2985-
func (srv *Server) Shutdown(ctx context.Context) error {
2986-
srv.inShutdown.Store(true)
2985+
func (s *Server) Shutdown(ctx context.Context) error {
2986+
s.inShutdown.Store(true)
29872987

2988-
srv.mu.Lock()
2989-
lnerr := srv.closeListenersLocked()
2990-
for _, f := range srv.onShutdown {
2988+
s.mu.Lock()
2989+
lnerr := s.closeListenersLocked()
2990+
for _, f := range s.onShutdown {
29912991
go f()
29922992
}
2993-
srv.mu.Unlock()
2994-
srv.listenerGroup.Wait()
2993+
s.mu.Unlock()
2994+
s.listenerGroup.Wait()
29952995

29962996
pollIntervalBase := time.Millisecond
29972997
nextPollInterval := func() time.Duration {
@@ -3008,7 +3008,7 @@ func (srv *Server) Shutdown(ctx context.Context) error {
30083008
timer := time.NewTimer(nextPollInterval())
30093009
defer timer.Stop()
30103010
for {
3011-
if srv.closeIdleConns() {
3011+
if s.closeIdleConns() {
30123012
return lnerr
30133013
}
30143014
select {
@@ -3025,10 +3025,10 @@ func (srv *Server) Shutdown(ctx context.Context) error {
30253025
// undergone ALPN protocol upgrade or that have been hijacked.
30263026
// This function should start protocol-specific graceful shutdown,
30273027
// but should not wait for shutdown to complete.
3028-
func (srv *Server) RegisterOnShutdown(f func()) {
3029-
srv.mu.Lock()
3030-
srv.onShutdown = append(srv.onShutdown, f)
3031-
srv.mu.Unlock()
3028+
func (s *Server) RegisterOnShutdown(f func()) {
3029+
s.mu.Lock()
3030+
s.onShutdown = append(s.onShutdown, f)
3031+
s.mu.Unlock()
30323032
}
30333033

30343034
// closeIdleConns closes all idle connections and reports whether the
@@ -3169,32 +3169,32 @@ func AllowQuerySemicolons(h Handler) Handler {
31693169
//
31703170
// ListenAndServe always returns a non-nil error. After [Server.Shutdown] or [Server.Close],
31713171
// the returned error is [ErrServerClosed].
3172-
func (srv *Server) ListenAndServe() error {
3173-
if srv.shuttingDown() {
3172+
func (s *Server) ListenAndServe() error {
3173+
if s.shuttingDown() {
31743174
return ErrServerClosed
31753175
}
3176-
addr := srv.Addr
3176+
addr := s.Addr
31773177
if addr == "" {
31783178
addr = ":http"
31793179
}
31803180
ln, err := net.Listen("tcp", addr)
31813181
if err != nil {
31823182
return err
31833183
}
3184-
return srv.Serve(ln)
3184+
return s.Serve(ln)
31853185
}
31863186

31873187
var testHookServerServe func(*Server, net.Listener) // used if non-nil
31883188

31893189
// shouldConfigureHTTP2ForServe reports whether Server.Serve should configure
31903190
// automatic HTTP/2. (which sets up the srv.TLSNextProto map)
3191-
func (srv *Server) shouldConfigureHTTP2ForServe() bool {
3192-
if srv.TLSConfig == nil {
3191+
func (s *Server) shouldConfigureHTTP2ForServe() bool {
3192+
if s.TLSConfig == nil {
31933193
// Compatibility with Go 1.6:
31943194
// If there's no TLSConfig, it's possible that the user just
31953195
// didn't set it on the http.Server, but did pass it to
31963196
// tls.NewListener and passed that listener to Serve.
3197-
// So we should configure HTTP/2 (to set up srv.TLSNextProto)
3197+
// So we should configure HTTP/2 (to set up s.TLSNextProto)
31983198
// in case the listener returns an "h2" *tls.Conn.
31993199
return true
32003200
}
@@ -3205,7 +3205,7 @@ func (srv *Server) shouldConfigureHTTP2ForServe() bool {
32053205
// passed this tls.Config to tls.NewListener. And if they did,
32063206
// it's too late anyway to fix it. It would only be potentially racy.
32073207
// See Issue 15908.
3208-
return strSliceContains(srv.TLSConfig.NextProtos, http2NextProtoTLS)
3208+
return strSliceContains(s.TLSConfig.NextProtos, http2NextProtoTLS)
32093209
}
32103210

32113211
// ErrServerClosed is returned by the [Server.Serve], [ServeTLS], [ListenAndServe],
@@ -3222,39 +3222,39 @@ var ErrServerClosed = errors.New("http: Server closed")
32223222
//
32233223
// Serve always returns a non-nil error and closes l.
32243224
// After [Server.Shutdown] or [Server.Close], the returned error is [ErrServerClosed].
3225-
func (srv *Server) Serve(l net.Listener) error {
3225+
func (s *Server) Serve(l net.Listener) error {
32263226
if fn := testHookServerServe; fn != nil {
3227-
fn(srv, l) // call hook with unwrapped listener
3227+
fn(s, l) // call hook with unwrapped listener
32283228
}
32293229

32303230
origListener := l
32313231
l = &onceCloseListener{Listener: l}
32323232
defer l.Close()
32333233

3234-
if err := srv.setupHTTP2_Serve(); err != nil {
3234+
if err := s.setupHTTP2_Serve(); err != nil {
32353235
return err
32363236
}
32373237

3238-
if !srv.trackListener(&l, true) {
3238+
if !s.trackListener(&l, true) {
32393239
return ErrServerClosed
32403240
}
3241-
defer srv.trackListener(&l, false)
3241+
defer s.trackListener(&l, false)
32423242

32433243
baseCtx := context.Background()
3244-
if srv.BaseContext != nil {
3245-
baseCtx = srv.BaseContext(origListener)
3244+
if s.BaseContext != nil {
3245+
baseCtx = s.BaseContext(origListener)
32463246
if baseCtx == nil {
32473247
panic("BaseContext returned a nil context")
32483248
}
32493249
}
32503250

32513251
var tempDelay time.Duration // how long to sleep on accept failure
32523252

3253-
ctx := context.WithValue(baseCtx, ServerContextKey, srv)
3253+
ctx := context.WithValue(baseCtx, ServerContextKey, s)
32543254
for {
32553255
rw, err := l.Accept()
32563256
if err != nil {
3257-
if srv.shuttingDown() {
3257+
if s.shuttingDown() {
32583258
return ErrServerClosed
32593259
}
32603260
if ne, ok := err.(net.Error); ok && ne.Temporary() {
@@ -3266,21 +3266,21 @@ func (srv *Server) Serve(l net.Listener) error {
32663266
if max := 1 * time.Second; tempDelay > max {
32673267
tempDelay = max
32683268
}
3269-
srv.logf("http: Accept error: %v; retrying in %v", err, tempDelay)
3269+
s.logf("http: Accept error: %v; retrying in %v", err, tempDelay)
32703270
time.Sleep(tempDelay)
32713271
continue
32723272
}
32733273
return err
32743274
}
32753275
connCtx := ctx
3276-
if cc := srv.ConnContext; cc != nil {
3276+
if cc := s.ConnContext; cc != nil {
32773277
connCtx = cc(connCtx, rw)
32783278
if connCtx == nil {
32793279
panic("ConnContext returned nil")
32803280
}
32813281
}
32823282
tempDelay = 0
3283-
c := srv.newConn(rw)
3283+
c := s.newConn(rw)
32843284
c.setState(c.rwc, StateNew, runHooks) // before Serve can return
32853285
go c.serve(connCtx)
32863286
}
@@ -3299,14 +3299,14 @@ func (srv *Server) Serve(l net.Listener) error {
32993299
//
33003300
// ServeTLS always returns a non-nil error. After [Server.Shutdown] or [Server.Close], the
33013301
// returned error is [ErrServerClosed].
3302-
func (srv *Server) ServeTLS(l net.Listener, certFile, keyFile string) error {
3303-
// Setup HTTP/2 before srv.Serve, to initialize srv.TLSConfig
3302+
func (s *Server) ServeTLS(l net.Listener, certFile, keyFile string) error {
3303+
// Setup HTTP/2 before s.Serve, to initialize s.TLSConfig
33043304
// before we clone it and create the TLS Listener.
3305-
if err := srv.setupHTTP2_ServeTLS(); err != nil {
3305+
if err := s.setupHTTP2_ServeTLS(); err != nil {
33063306
return err
33073307
}
33083308

3309-
config := cloneTLSConfig(srv.TLSConfig)
3309+
config := cloneTLSConfig(s.TLSConfig)
33103310
if !strSliceContains(config.NextProtos, "http/1.1") {
33113311
config.NextProtos = append(config.NextProtos, "http/1.1")
33123312
}
@@ -3322,7 +3322,7 @@ func (srv *Server) ServeTLS(l net.Listener, certFile, keyFile string) error {
33223322
}
33233323

33243324
tlsListener := tls.NewListener(l, config)
3325-
return srv.Serve(tlsListener)
3325+
return s.Serve(tlsListener)
33263326
}
33273327

33283328
// trackListener adds or removes a net.Listener to the set of tracked
@@ -3393,15 +3393,15 @@ func (s *Server) shuttingDown() bool {
33933393
// By default, keep-alives are always enabled. Only very
33943394
// resource-constrained environments or servers in the process of
33953395
// shutting down should disable them.
3396-
func (srv *Server) SetKeepAlivesEnabled(v bool) {
3396+
func (s *Server) SetKeepAlivesEnabled(v bool) {
33973397
if v {
3398-
srv.disableKeepAlives.Store(false)
3398+
s.disableKeepAlives.Store(false)
33993399
return
34003400
}
3401-
srv.disableKeepAlives.Store(true)
3401+
s.disableKeepAlives.Store(true)
34023402

34033403
// Close idle HTTP/1 conns:
3404-
srv.closeIdleConns()
3404+
s.closeIdleConns()
34053405

34063406
// TODO: Issue 26303: close HTTP/2 conns as soon as they become idle.
34073407
}
@@ -3463,11 +3463,11 @@ func ListenAndServeTLS(addr, certFile, keyFile string, handler Handler) error {
34633463
//
34643464
// ListenAndServeTLS always returns a non-nil error. After [Server.Shutdown] or
34653465
// [Server.Close], the returned error is [ErrServerClosed].
3466-
func (srv *Server) ListenAndServeTLS(certFile, keyFile string) error {
3467-
if srv.shuttingDown() {
3466+
func (s *Server) ListenAndServeTLS(certFile, keyFile string) error {
3467+
if s.shuttingDown() {
34683468
return ErrServerClosed
34693469
}
3470-
addr := srv.Addr
3470+
addr := s.Addr
34713471
if addr == "" {
34723472
addr = ":https"
34733473
}
@@ -3479,15 +3479,15 @@ func (srv *Server) ListenAndServeTLS(certFile, keyFile string) error {
34793479

34803480
defer ln.Close()
34813481

3482-
return srv.ServeTLS(ln, certFile, keyFile)
3482+
return s.ServeTLS(ln, certFile, keyFile)
34833483
}
34843484

34853485
// setupHTTP2_ServeTLS conditionally configures HTTP/2 on
34863486
// srv and reports whether there was an error setting it up. If it is
34873487
// not configured for policy reasons, nil is returned.
3488-
func (srv *Server) setupHTTP2_ServeTLS() error {
3489-
srv.nextProtoOnce.Do(srv.onceSetNextProtoDefaults)
3490-
return srv.nextProtoErr
3488+
func (s *Server) setupHTTP2_ServeTLS() error {
3489+
s.nextProtoOnce.Do(s.onceSetNextProtoDefaults)
3490+
return s.nextProtoErr
34913491
}
34923492

34933493
// setupHTTP2_Serve is called from (*Server).Serve and conditionally
@@ -3498,14 +3498,14 @@ func (srv *Server) setupHTTP2_ServeTLS() error {
34983498
// The tests named TestTransportAutomaticHTTP2* and
34993499
// TestConcurrentServerServe in server_test.go demonstrate some
35003500
// of the supported use cases and motivations.
3501-
func (srv *Server) setupHTTP2_Serve() error {
3502-
srv.nextProtoOnce.Do(srv.onceSetNextProtoDefaults_Serve)
3503-
return srv.nextProtoErr
3501+
func (s *Server) setupHTTP2_Serve() error {
3502+
s.nextProtoOnce.Do(s.onceSetNextProtoDefaults_Serve)
3503+
return s.nextProtoErr
35043504
}
35053505

3506-
func (srv *Server) onceSetNextProtoDefaults_Serve() {
3507-
if srv.shouldConfigureHTTP2ForServe() {
3508-
srv.onceSetNextProtoDefaults()
3506+
func (s *Server) onceSetNextProtoDefaults_Serve() {
3507+
if s.shouldConfigureHTTP2ForServe() {
3508+
s.onceSetNextProtoDefaults()
35093509
}
35103510
}
35113511

@@ -3514,7 +3514,7 @@ var http2server = godebug.New("http2server")
35143514
// onceSetNextProtoDefaults configures HTTP/2, if the user hasn't
35153515
// configured otherwise. (by setting srv.TLSNextProto non-nil)
35163516
// It must only be called via srv.nextProtoOnce (use srv.setupHTTP2_*).
3517-
func (srv *Server) onceSetNextProtoDefaults() {
3517+
func (s *Server) onceSetNextProtoDefaults() {
35183518
if omitBundledHTTP2 {
35193519
return
35203520
}
@@ -3524,11 +3524,11 @@ func (srv *Server) onceSetNextProtoDefaults() {
35243524
}
35253525
// Enable HTTP/2 by default if the user hasn't otherwise
35263526
// configured their TLSNextProto map.
3527-
if srv.TLSNextProto == nil {
3527+
if s.TLSNextProto == nil {
35283528
conf := &http2Server{
35293529
NewWriteScheduler: func() http2WriteScheduler { return http2NewPriorityWriteScheduler(nil) },
35303530
}
3531-
srv.nextProtoErr = http2ConfigureServer(srv, conf)
3531+
s.nextProtoErr = http2ConfigureServer(s, conf)
35323532
}
35333533
}
35343534

0 commit comments

Comments
 (0)