2023-07-12 10:18:30 -04:00
|
|
|
// Copyright (c) 2015-2023 MinIO, Inc.
|
2021-04-18 15:41:13 -04:00
|
|
|
//
|
|
|
|
// This file is part of MinIO Object Storage stack
|
|
|
|
//
|
|
|
|
// This program is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU Affero General Public License as published by
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
//
|
|
|
|
// This program is distributed in the hope that it will be useful
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU Affero General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU Affero General Public License
|
|
|
|
// along with this program. If not, see <http://www.gnu.org/licenses/>.
|
2017-07-12 19:33:21 -04:00
|
|
|
|
|
|
|
package http
|
|
|
|
|
|
|
|
import (
|
2021-10-05 13:13:04 -04:00
|
|
|
"context"
|
2017-07-12 19:33:21 -04:00
|
|
|
"crypto/tls"
|
|
|
|
"errors"
|
2021-11-29 12:06:56 -05:00
|
|
|
"log"
|
2023-07-12 10:18:30 -04:00
|
|
|
"math/rand"
|
2021-11-29 12:06:56 -05:00
|
|
|
"net"
|
2017-07-12 19:33:21 -04:00
|
|
|
"net/http"
|
2022-09-19 14:05:16 -04:00
|
|
|
"os"
|
2019-11-29 09:10:08 -05:00
|
|
|
"runtime/pprof"
|
2017-07-12 19:33:21 -04:00
|
|
|
"sync"
|
|
|
|
"sync/atomic"
|
|
|
|
"time"
|
|
|
|
|
2022-09-19 14:05:16 -04:00
|
|
|
"github.com/dustin/go-humanize"
|
2017-07-12 19:33:21 -04:00
|
|
|
)
|
|
|
|
|
2022-02-23 16:36:01 -05:00
|
|
|
var (
|
|
|
|
// GlobalMinIOVersion - is sent in the header to all http targets
|
|
|
|
GlobalMinIOVersion string
|
|
|
|
|
|
|
|
// GlobalDeploymentID - is sent in the header to all http targets
|
|
|
|
GlobalDeploymentID string
|
|
|
|
)
|
|
|
|
|
2017-07-12 19:33:21 -04:00
|
|
|
const (
|
2023-07-12 10:18:30 -04:00
|
|
|
shutdownPollIntervalMax = 500 * time.Millisecond
|
2017-07-12 19:33:21 -04:00
|
|
|
|
2021-11-29 12:06:56 -05:00
|
|
|
// DefaultShutdownTimeout - default shutdown timeout to gracefully shutdown server.
|
2017-07-12 19:33:21 -04:00
|
|
|
DefaultShutdownTimeout = 5 * time.Second
|
|
|
|
|
2022-05-30 09:24:51 -04:00
|
|
|
// DefaultIdleTimeout for idle inactive connections
|
|
|
|
DefaultIdleTimeout = 30 * time.Second
|
|
|
|
|
|
|
|
// DefaultReadHeaderTimeout for very slow inactive connections
|
|
|
|
DefaultReadHeaderTimeout = 30 * time.Second
|
|
|
|
|
2017-07-12 19:33:21 -04:00
|
|
|
// DefaultMaxHeaderBytes - default maximum HTTP header size in bytes.
|
|
|
|
DefaultMaxHeaderBytes = 1 * humanize.MiByte
|
|
|
|
)
|
|
|
|
|
|
|
|
// Server - extended http.Server supports multiple addresses to serve and enhanced connection handling.
|
|
|
|
type Server struct {
|
|
|
|
http.Server
|
2020-07-03 13:03:41 -04:00
|
|
|
Addrs []string // addresses on which the server listens for new connection.
|
2023-05-03 17:12:25 -04:00
|
|
|
TCPOptions TCPOptions // all the configurable TCP conn specific configurable options.
|
2020-07-03 13:03:41 -04:00
|
|
|
ShutdownTimeout time.Duration // timeout used for graceful server shutdown.
|
|
|
|
listenerMutex sync.Mutex // to guard 'listener' field.
|
|
|
|
listener *httpListener // HTTP listener for all 'Addrs' field.
|
|
|
|
inShutdown uint32 // indicates whether the server is in shutdown or not
|
|
|
|
requestCount int32 // counter holds no. of request in progress.
|
2018-06-27 21:59:38 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// GetRequestCount - returns number of request in progress.
|
2020-12-02 14:12:00 -05:00
|
|
|
func (srv *Server) GetRequestCount() int {
|
|
|
|
return int(atomic.LoadInt32(&srv.requestCount))
|
2017-07-12 19:33:21 -04:00
|
|
|
}
|
|
|
|
|
2023-06-12 12:09:28 -04:00
|
|
|
// Init - init HTTP server
|
|
|
|
func (srv *Server) Init(listenCtx context.Context, listenErrCallback func(listenAddr string, err error)) (serve func() error, err error) {
|
2017-07-12 19:33:21 -04:00
|
|
|
// Take a copy of server fields.
|
2018-01-12 22:42:11 -05:00
|
|
|
var tlsConfig *tls.Config
|
|
|
|
if srv.TLSConfig != nil {
|
|
|
|
tlsConfig = srv.TLSConfig.Clone()
|
|
|
|
}
|
|
|
|
handler := srv.Handler // if srv.Handler holds non-synced state -> possible data race
|
2017-07-12 19:33:21 -04:00
|
|
|
|
|
|
|
// Create new HTTP listener.
|
|
|
|
var listener *httpListener
|
2023-06-12 12:09:28 -04:00
|
|
|
listener, listenErrs := newHTTPListener(
|
|
|
|
listenCtx,
|
2021-10-05 13:13:04 -04:00
|
|
|
srv.Addrs,
|
2023-05-03 17:12:25 -04:00
|
|
|
srv.TCPOptions,
|
2017-07-12 19:33:21 -04:00
|
|
|
)
|
2023-06-12 12:09:28 -04:00
|
|
|
|
|
|
|
var interfaceFound bool
|
|
|
|
for i := range listenErrs {
|
|
|
|
if listenErrs[i] != nil {
|
|
|
|
listenErrCallback(srv.Addrs[i], listenErrs[i])
|
|
|
|
} else {
|
|
|
|
interfaceFound = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if !interfaceFound {
|
|
|
|
return nil, errors.New("no available interface found")
|
2017-07-12 19:33:21 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Wrap given handler to do additional
|
|
|
|
// * return 503 (service unavailable) if the server in shutdown.
|
|
|
|
wrappedHandler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
2020-01-17 08:48:39 -05:00
|
|
|
// If server is in shutdown.
|
2017-07-12 19:33:21 -04:00
|
|
|
if atomic.LoadUint32(&srv.inShutdown) != 0 {
|
2024-02-09 16:25:16 -05:00
|
|
|
// To indicate disable keep-alive, server is shutting down.
|
2020-01-17 08:48:39 -05:00
|
|
|
w.Header().Set("Connection", "close")
|
2024-02-09 16:25:16 -05:00
|
|
|
|
|
|
|
// Add 1 minute retry header, incase-client wants to honor it
|
|
|
|
w.Header().Set(RetryAfter, "60")
|
|
|
|
|
2021-10-22 04:30:27 -04:00
|
|
|
w.WriteHeader(http.StatusServiceUnavailable)
|
2020-01-17 08:48:39 -05:00
|
|
|
w.Write([]byte(http.ErrServerClosed.Error()))
|
2017-07-12 19:33:21 -04:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-01-17 08:48:39 -05:00
|
|
|
atomic.AddInt32(&srv.requestCount, 1)
|
|
|
|
defer atomic.AddInt32(&srv.requestCount, -1)
|
|
|
|
|
2017-07-12 19:33:21 -04:00
|
|
|
// Handle request using passed handler.
|
|
|
|
handler.ServeHTTP(w, r)
|
|
|
|
})
|
|
|
|
|
|
|
|
srv.listenerMutex.Lock()
|
|
|
|
srv.Handler = wrappedHandler
|
|
|
|
srv.listener = listener
|
|
|
|
srv.listenerMutex.Unlock()
|
|
|
|
|
2023-06-12 12:09:28 -04:00
|
|
|
var l net.Listener = listener
|
2019-02-14 20:53:46 -05:00
|
|
|
if tlsConfig != nil {
|
2023-06-12 12:09:28 -04:00
|
|
|
l = tls.NewListener(listener, tlsConfig)
|
2019-02-14 20:53:46 -05:00
|
|
|
}
|
2023-06-12 12:09:28 -04:00
|
|
|
|
|
|
|
serve = func() error {
|
|
|
|
return srv.Server.Serve(l)
|
|
|
|
}
|
|
|
|
|
|
|
|
return
|
2017-07-12 19:33:21 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Shutdown - shuts down HTTP server.
|
|
|
|
func (srv *Server) Shutdown() error {
|
2018-01-12 22:42:11 -05:00
|
|
|
srv.listenerMutex.Lock()
|
2017-08-04 13:44:46 -04:00
|
|
|
if srv.listener == nil {
|
2018-01-12 22:42:11 -05:00
|
|
|
srv.listenerMutex.Unlock()
|
2020-01-17 08:48:39 -05:00
|
|
|
return http.ErrServerClosed
|
2017-08-04 13:44:46 -04:00
|
|
|
}
|
2018-01-12 22:42:11 -05:00
|
|
|
srv.listenerMutex.Unlock()
|
2017-08-04 13:44:46 -04:00
|
|
|
|
2017-07-12 19:33:21 -04:00
|
|
|
if atomic.AddUint32(&srv.inShutdown, 1) > 1 {
|
|
|
|
// shutdown in progress
|
2020-01-17 08:48:39 -05:00
|
|
|
return http.ErrServerClosed
|
2017-07-12 19:33:21 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Close underneath HTTP listener.
|
|
|
|
srv.listenerMutex.Lock()
|
|
|
|
err := srv.listener.Close()
|
|
|
|
srv.listenerMutex.Unlock()
|
2020-09-28 22:39:32 -04:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2017-07-12 19:33:21 -04:00
|
|
|
|
2023-07-12 10:18:30 -04:00
|
|
|
pollIntervalBase := time.Millisecond
|
|
|
|
nextPollInterval := func() time.Duration {
|
|
|
|
// Add 10% jitter.
|
|
|
|
interval := pollIntervalBase + time.Duration(rand.Intn(int(pollIntervalBase/10)))
|
|
|
|
// Double and clamp for next time.
|
|
|
|
pollIntervalBase *= 2
|
|
|
|
if pollIntervalBase > shutdownPollIntervalMax {
|
|
|
|
pollIntervalBase = shutdownPollIntervalMax
|
|
|
|
}
|
|
|
|
return interval
|
|
|
|
}
|
|
|
|
|
2017-07-12 19:33:21 -04:00
|
|
|
// Wait for opened connection to be closed up to Shutdown timeout.
|
|
|
|
shutdownTimeout := srv.ShutdownTimeout
|
|
|
|
shutdownTimer := time.NewTimer(shutdownTimeout)
|
2023-07-12 10:18:30 -04:00
|
|
|
defer shutdownTimer.Stop()
|
|
|
|
|
|
|
|
timer := time.NewTimer(nextPollInterval())
|
|
|
|
defer timer.Stop()
|
2017-07-12 19:33:21 -04:00
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-shutdownTimer.C:
|
2023-07-12 10:18:30 -04:00
|
|
|
if atomic.LoadInt32(&srv.requestCount) <= 0 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-11-29 09:10:08 -05:00
|
|
|
// Write all running goroutines.
|
2022-09-19 14:05:16 -04:00
|
|
|
tmp, err := os.CreateTemp("", "minio-goroutines-*.txt")
|
2019-11-29 09:10:08 -05:00
|
|
|
if err == nil {
|
|
|
|
_ = pprof.Lookup("goroutine").WriteTo(tmp, 1)
|
|
|
|
tmp.Close()
|
2020-09-28 22:39:32 -04:00
|
|
|
return errors.New("timed out. some connections are still active. goroutines written to " + tmp.Name())
|
2019-11-29 09:10:08 -05:00
|
|
|
}
|
2020-09-28 22:39:32 -04:00
|
|
|
return errors.New("timed out. some connections are still active")
|
2023-07-12 10:18:30 -04:00
|
|
|
case <-timer.C:
|
2017-07-12 19:33:21 -04:00
|
|
|
if atomic.LoadInt32(&srv.requestCount) <= 0 {
|
2020-09-28 22:39:32 -04:00
|
|
|
return nil
|
2017-07-12 19:33:21 -04:00
|
|
|
}
|
2023-07-12 10:18:30 -04:00
|
|
|
timer.Reset(nextPollInterval())
|
2017-07-12 19:33:21 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-29 12:06:56 -05:00
|
|
|
// UseShutdownTimeout configure server shutdown timeout
|
|
|
|
func (srv *Server) UseShutdownTimeout(d time.Duration) *Server {
|
|
|
|
srv.ShutdownTimeout = d
|
|
|
|
return srv
|
|
|
|
}
|
2021-10-11 17:23:51 -04:00
|
|
|
|
2022-05-30 09:24:51 -04:00
|
|
|
// UseIdleTimeout configure idle connection timeout
|
|
|
|
func (srv *Server) UseIdleTimeout(d time.Duration) *Server {
|
|
|
|
srv.IdleTimeout = d
|
|
|
|
return srv
|
|
|
|
}
|
|
|
|
|
|
|
|
// UseReadHeaderTimeout configure read header timeout
|
|
|
|
func (srv *Server) UseReadHeaderTimeout(d time.Duration) *Server {
|
|
|
|
srv.ReadHeaderTimeout = d
|
|
|
|
return srv
|
|
|
|
}
|
|
|
|
|
2021-11-29 12:06:56 -05:00
|
|
|
// UseHandler configure final handler for this HTTP *Server
|
|
|
|
func (srv *Server) UseHandler(h http.Handler) *Server {
|
|
|
|
srv.Handler = h
|
|
|
|
return srv
|
|
|
|
}
|
2021-10-11 17:23:51 -04:00
|
|
|
|
2021-11-29 12:06:56 -05:00
|
|
|
// UseTLSConfig pass configured TLSConfig for this HTTP *Server
|
|
|
|
func (srv *Server) UseTLSConfig(cfg *tls.Config) *Server {
|
|
|
|
srv.TLSConfig = cfg
|
|
|
|
return srv
|
|
|
|
}
|
|
|
|
|
|
|
|
// UseBaseContext use custom base context for this HTTP *Server
|
|
|
|
func (srv *Server) UseBaseContext(ctx context.Context) *Server {
|
|
|
|
srv.BaseContext = func(listener net.Listener) context.Context {
|
|
|
|
return ctx
|
2020-07-13 17:20:21 -04:00
|
|
|
}
|
2021-11-29 12:06:56 -05:00
|
|
|
return srv
|
|
|
|
}
|
2020-07-13 17:20:21 -04:00
|
|
|
|
2021-11-29 12:06:56 -05:00
|
|
|
// UseCustomLogger use customized logger for this HTTP *Server
|
|
|
|
func (srv *Server) UseCustomLogger(l *log.Logger) *Server {
|
|
|
|
srv.ErrorLog = l
|
|
|
|
return srv
|
|
|
|
}
|
|
|
|
|
2023-05-03 17:12:25 -04:00
|
|
|
// UseTCPOptions use custom TCP options on raw socket
|
|
|
|
func (srv *Server) UseTCPOptions(opts TCPOptions) *Server {
|
|
|
|
srv.TCPOptions = opts
|
|
|
|
return srv
|
|
|
|
}
|
|
|
|
|
2021-11-29 12:06:56 -05:00
|
|
|
// NewServer - creates new HTTP server using given arguments.
|
|
|
|
func NewServer(addrs []string) *Server {
|
2017-07-12 19:33:21 -04:00
|
|
|
httpServer := &Server{
|
2021-11-29 12:06:56 -05:00
|
|
|
Addrs: addrs,
|
2017-07-12 19:33:21 -04:00
|
|
|
}
|
2021-11-29 12:06:56 -05:00
|
|
|
// This is not configurable for now.
|
2017-07-12 19:33:21 -04:00
|
|
|
httpServer.MaxHeaderBytes = DefaultMaxHeaderBytes
|
|
|
|
return httpServer
|
|
|
|
}
|
2022-02-23 16:36:01 -05:00
|
|
|
|
|
|
|
// SetMinIOVersion -- MinIO version from the main package is set here
|
2022-05-30 09:24:51 -04:00
|
|
|
func SetMinIOVersion(version string) {
|
|
|
|
GlobalMinIOVersion = version
|
2022-02-23 16:36:01 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// SetDeploymentID -- Deployment Id from the main package is set here
|
|
|
|
func SetDeploymentID(deploymentID string) {
|
|
|
|
GlobalDeploymentID = deploymentID
|
|
|
|
}
|