@@ -619,9 +619,9 @@ func (w *response) ReadFrom(src io.Reader) (n int64, err error) {
619
619
const debugServerConnections = false
620
620
621
621
// Create new connection from rwc.
622
- func (srv * Server ) newConn (rwc net.Conn ) * conn {
622
+ func (s * Server ) newConn (rwc net.Conn ) * conn {
623
623
c := & conn {
624
- server : srv ,
624
+ server : s ,
625
625
rwc : rwc ,
626
626
}
627
627
if debugServerConnections {
@@ -870,28 +870,28 @@ func putBufioWriter(bw *bufio.Writer) {
870
870
// This can be overridden by setting [Server.MaxHeaderBytes].
871
871
const DefaultMaxHeaderBytes = 1 << 20 // 1 MB
872
872
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
876
876
}
877
877
return DefaultMaxHeaderBytes
878
878
}
879
879
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
882
882
}
883
883
884
884
// tlsHandshakeTimeout returns the time limit permitted for the TLS
885
885
// handshake, or zero for unlimited.
886
886
//
887
887
// It returns the minimum of any positive ReadHeaderTimeout,
888
888
// ReadTimeout, or WriteTimeout.
889
- func (srv * Server ) tlsHandshakeTimeout () time.Duration {
889
+ func (s * Server ) tlsHandshakeTimeout () time.Duration {
890
890
var ret time.Duration
891
891
for _ , v := range [... ]time.Duration {
892
- srv .ReadHeaderTimeout ,
893
- srv .ReadTimeout ,
894
- srv .WriteTimeout ,
892
+ s .ReadHeaderTimeout ,
893
+ s .ReadTimeout ,
894
+ s .WriteTimeout ,
895
895
} {
896
896
if v <= 0 {
897
897
continue
@@ -2932,23 +2932,23 @@ type Server struct {
2932
2932
//
2933
2933
// Close returns any error returned from closing the [Server]'s
2934
2934
// 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,
2943
2943
// to avoid adding a new listener in the window between
2944
2944
// 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 ()
2948
2948
2949
- for c := range srv .activeConn {
2949
+ for c := range s .activeConn {
2950
2950
c .rwc .Close ()
2951
- delete (srv .activeConn , c )
2951
+ delete (s .activeConn , c )
2952
2952
}
2953
2953
return err
2954
2954
}
@@ -2982,16 +2982,16 @@ const shutdownPollIntervalMax = 500 * time.Millisecond
2982
2982
//
2983
2983
// Once Shutdown has been called on a server, it may not be reused;
2984
2984
// 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 )
2987
2987
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 {
2991
2991
go f ()
2992
2992
}
2993
- srv .mu .Unlock ()
2994
- srv .listenerGroup .Wait ()
2993
+ s .mu .Unlock ()
2994
+ s .listenerGroup .Wait ()
2995
2995
2996
2996
pollIntervalBase := time .Millisecond
2997
2997
nextPollInterval := func () time.Duration {
@@ -3008,7 +3008,7 @@ func (srv *Server) Shutdown(ctx context.Context) error {
3008
3008
timer := time .NewTimer (nextPollInterval ())
3009
3009
defer timer .Stop ()
3010
3010
for {
3011
- if srv .closeIdleConns () {
3011
+ if s .closeIdleConns () {
3012
3012
return lnerr
3013
3013
}
3014
3014
select {
@@ -3025,10 +3025,10 @@ func (srv *Server) Shutdown(ctx context.Context) error {
3025
3025
// undergone ALPN protocol upgrade or that have been hijacked.
3026
3026
// This function should start protocol-specific graceful shutdown,
3027
3027
// 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 ()
3032
3032
}
3033
3033
3034
3034
// closeIdleConns closes all idle connections and reports whether the
@@ -3169,32 +3169,32 @@ func AllowQuerySemicolons(h Handler) Handler {
3169
3169
//
3170
3170
// ListenAndServe always returns a non-nil error. After [Server.Shutdown] or [Server.Close],
3171
3171
// 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 () {
3174
3174
return ErrServerClosed
3175
3175
}
3176
- addr := srv .Addr
3176
+ addr := s .Addr
3177
3177
if addr == "" {
3178
3178
addr = ":http"
3179
3179
}
3180
3180
ln , err := net .Listen ("tcp" , addr )
3181
3181
if err != nil {
3182
3182
return err
3183
3183
}
3184
- return srv .Serve (ln )
3184
+ return s .Serve (ln )
3185
3185
}
3186
3186
3187
3187
var testHookServerServe func (* Server , net.Listener ) // used if non-nil
3188
3188
3189
3189
// shouldConfigureHTTP2ForServe reports whether Server.Serve should configure
3190
3190
// 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 {
3193
3193
// Compatibility with Go 1.6:
3194
3194
// If there's no TLSConfig, it's possible that the user just
3195
3195
// didn't set it on the http.Server, but did pass it to
3196
3196
// 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)
3198
3198
// in case the listener returns an "h2" *tls.Conn.
3199
3199
return true
3200
3200
}
@@ -3205,7 +3205,7 @@ func (srv *Server) shouldConfigureHTTP2ForServe() bool {
3205
3205
// passed this tls.Config to tls.NewListener. And if they did,
3206
3206
// it's too late anyway to fix it. It would only be potentially racy.
3207
3207
// See Issue 15908.
3208
- return strSliceContains (srv .TLSConfig .NextProtos , http2NextProtoTLS )
3208
+ return strSliceContains (s .TLSConfig .NextProtos , http2NextProtoTLS )
3209
3209
}
3210
3210
3211
3211
// ErrServerClosed is returned by the [Server.Serve], [ServeTLS], [ListenAndServe],
@@ -3222,39 +3222,39 @@ var ErrServerClosed = errors.New("http: Server closed")
3222
3222
//
3223
3223
// Serve always returns a non-nil error and closes l.
3224
3224
// 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 {
3226
3226
if fn := testHookServerServe ; fn != nil {
3227
- fn (srv , l ) // call hook with unwrapped listener
3227
+ fn (s , l ) // call hook with unwrapped listener
3228
3228
}
3229
3229
3230
3230
origListener := l
3231
3231
l = & onceCloseListener {Listener : l }
3232
3232
defer l .Close ()
3233
3233
3234
- if err := srv .setupHTTP2_Serve (); err != nil {
3234
+ if err := s .setupHTTP2_Serve (); err != nil {
3235
3235
return err
3236
3236
}
3237
3237
3238
- if ! srv .trackListener (& l , true ) {
3238
+ if ! s .trackListener (& l , true ) {
3239
3239
return ErrServerClosed
3240
3240
}
3241
- defer srv .trackListener (& l , false )
3241
+ defer s .trackListener (& l , false )
3242
3242
3243
3243
baseCtx := context .Background ()
3244
- if srv .BaseContext != nil {
3245
- baseCtx = srv .BaseContext (origListener )
3244
+ if s .BaseContext != nil {
3245
+ baseCtx = s .BaseContext (origListener )
3246
3246
if baseCtx == nil {
3247
3247
panic ("BaseContext returned a nil context" )
3248
3248
}
3249
3249
}
3250
3250
3251
3251
var tempDelay time.Duration // how long to sleep on accept failure
3252
3252
3253
- ctx := context .WithValue (baseCtx , ServerContextKey , srv )
3253
+ ctx := context .WithValue (baseCtx , ServerContextKey , s )
3254
3254
for {
3255
3255
rw , err := l .Accept ()
3256
3256
if err != nil {
3257
- if srv .shuttingDown () {
3257
+ if s .shuttingDown () {
3258
3258
return ErrServerClosed
3259
3259
}
3260
3260
if ne , ok := err .(net.Error ); ok && ne .Temporary () {
@@ -3266,21 +3266,21 @@ func (srv *Server) Serve(l net.Listener) error {
3266
3266
if max := 1 * time .Second ; tempDelay > max {
3267
3267
tempDelay = max
3268
3268
}
3269
- srv .logf ("http: Accept error: %v; retrying in %v" , err , tempDelay )
3269
+ s .logf ("http: Accept error: %v; retrying in %v" , err , tempDelay )
3270
3270
time .Sleep (tempDelay )
3271
3271
continue
3272
3272
}
3273
3273
return err
3274
3274
}
3275
3275
connCtx := ctx
3276
- if cc := srv .ConnContext ; cc != nil {
3276
+ if cc := s .ConnContext ; cc != nil {
3277
3277
connCtx = cc (connCtx , rw )
3278
3278
if connCtx == nil {
3279
3279
panic ("ConnContext returned nil" )
3280
3280
}
3281
3281
}
3282
3282
tempDelay = 0
3283
- c := srv .newConn (rw )
3283
+ c := s .newConn (rw )
3284
3284
c .setState (c .rwc , StateNew , runHooks ) // before Serve can return
3285
3285
go c .serve (connCtx )
3286
3286
}
@@ -3299,14 +3299,14 @@ func (srv *Server) Serve(l net.Listener) error {
3299
3299
//
3300
3300
// ServeTLS always returns a non-nil error. After [Server.Shutdown] or [Server.Close], the
3301
3301
// 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
3304
3304
// 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 {
3306
3306
return err
3307
3307
}
3308
3308
3309
- config := cloneTLSConfig (srv .TLSConfig )
3309
+ config := cloneTLSConfig (s .TLSConfig )
3310
3310
if ! strSliceContains (config .NextProtos , "http/1.1" ) {
3311
3311
config .NextProtos = append (config .NextProtos , "http/1.1" )
3312
3312
}
@@ -3322,7 +3322,7 @@ func (srv *Server) ServeTLS(l net.Listener, certFile, keyFile string) error {
3322
3322
}
3323
3323
3324
3324
tlsListener := tls .NewListener (l , config )
3325
- return srv .Serve (tlsListener )
3325
+ return s .Serve (tlsListener )
3326
3326
}
3327
3327
3328
3328
// trackListener adds or removes a net.Listener to the set of tracked
@@ -3393,15 +3393,15 @@ func (s *Server) shuttingDown() bool {
3393
3393
// By default, keep-alives are always enabled. Only very
3394
3394
// resource-constrained environments or servers in the process of
3395
3395
// shutting down should disable them.
3396
- func (srv * Server ) SetKeepAlivesEnabled (v bool ) {
3396
+ func (s * Server ) SetKeepAlivesEnabled (v bool ) {
3397
3397
if v {
3398
- srv .disableKeepAlives .Store (false )
3398
+ s .disableKeepAlives .Store (false )
3399
3399
return
3400
3400
}
3401
- srv .disableKeepAlives .Store (true )
3401
+ s .disableKeepAlives .Store (true )
3402
3402
3403
3403
// Close idle HTTP/1 conns:
3404
- srv .closeIdleConns ()
3404
+ s .closeIdleConns ()
3405
3405
3406
3406
// TODO: Issue 26303: close HTTP/2 conns as soon as they become idle.
3407
3407
}
@@ -3463,11 +3463,11 @@ func ListenAndServeTLS(addr, certFile, keyFile string, handler Handler) error {
3463
3463
//
3464
3464
// ListenAndServeTLS always returns a non-nil error. After [Server.Shutdown] or
3465
3465
// [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 () {
3468
3468
return ErrServerClosed
3469
3469
}
3470
- addr := srv .Addr
3470
+ addr := s .Addr
3471
3471
if addr == "" {
3472
3472
addr = ":https"
3473
3473
}
@@ -3479,15 +3479,15 @@ func (srv *Server) ListenAndServeTLS(certFile, keyFile string) error {
3479
3479
3480
3480
defer ln .Close ()
3481
3481
3482
- return srv .ServeTLS (ln , certFile , keyFile )
3482
+ return s .ServeTLS (ln , certFile , keyFile )
3483
3483
}
3484
3484
3485
3485
// setupHTTP2_ServeTLS conditionally configures HTTP/2 on
3486
3486
// srv and reports whether there was an error setting it up. If it is
3487
3487
// 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
3491
3491
}
3492
3492
3493
3493
// setupHTTP2_Serve is called from (*Server).Serve and conditionally
@@ -3498,14 +3498,14 @@ func (srv *Server) setupHTTP2_ServeTLS() error {
3498
3498
// The tests named TestTransportAutomaticHTTP2* and
3499
3499
// TestConcurrentServerServe in server_test.go demonstrate some
3500
3500
// 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
3504
3504
}
3505
3505
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 ()
3509
3509
}
3510
3510
}
3511
3511
@@ -3514,7 +3514,7 @@ var http2server = godebug.New("http2server")
3514
3514
// onceSetNextProtoDefaults configures HTTP/2, if the user hasn't
3515
3515
// configured otherwise. (by setting srv.TLSNextProto non-nil)
3516
3516
// It must only be called via srv.nextProtoOnce (use srv.setupHTTP2_*).
3517
- func (srv * Server ) onceSetNextProtoDefaults () {
3517
+ func (s * Server ) onceSetNextProtoDefaults () {
3518
3518
if omitBundledHTTP2 {
3519
3519
return
3520
3520
}
@@ -3524,11 +3524,11 @@ func (srv *Server) onceSetNextProtoDefaults() {
3524
3524
}
3525
3525
// Enable HTTP/2 by default if the user hasn't otherwise
3526
3526
// configured their TLSNextProto map.
3527
- if srv .TLSNextProto == nil {
3527
+ if s .TLSNextProto == nil {
3528
3528
conf := & http2Server {
3529
3529
NewWriteScheduler : func () http2WriteScheduler { return http2NewPriorityWriteScheduler (nil ) },
3530
3530
}
3531
- srv .nextProtoErr = http2ConfigureServer (srv , conf )
3531
+ s .nextProtoErr = http2ConfigureServer (s , conf )
3532
3532
}
3533
3533
}
3534
3534
0 commit comments