forked from Mirrors/minio
Make DeadlineConn http.Listener compatible (#20635)
HTTP likes to slap an infinite read deadline on a connection and do a blocking read while the response is being written. This effectively means that a reading deadline becomes the request-response deadline. Instead of enforcing our timeout, we pass it through and keep "infinite deadline" is sticky on connections. However, we still "record" when reads are aborted, so we never overwrite that. The HTTP server should have `ReadTimeout` and `IdleTimeout` set for the deadline to be effective. Use --idle-timeout for incoming connections.
This commit is contained in:
@@ -23,6 +23,8 @@ import (
|
||||
"net"
|
||||
"syscall"
|
||||
"time"
|
||||
|
||||
"github.com/minio/minio/internal/deadlineconn"
|
||||
)
|
||||
|
||||
type acceptResult struct {
|
||||
@@ -73,7 +75,7 @@ func (listener *httpListener) Accept() (conn net.Conn, err error) {
|
||||
select {
|
||||
case result, ok := <-listener.acceptCh:
|
||||
if ok {
|
||||
return result.conn, result.err
|
||||
return deadlineconn.New(result.conn).WithReadDeadline(listener.opts.IdleTimeout).WithWriteDeadline(listener.opts.IdleTimeout), result.err
|
||||
}
|
||||
case <-listener.ctx.Done():
|
||||
}
|
||||
@@ -128,6 +130,7 @@ type TCPOptions struct {
|
||||
NoDelay bool // Indicates callers to enable TCP_NODELAY on the net.Conn
|
||||
Interface string // This is a VRF device passed via `--interface` flag
|
||||
Trace func(msg string) // Trace when starting.
|
||||
IdleTimeout time.Duration // Incoming TCP read/write timeout
|
||||
}
|
||||
|
||||
// ForWebsocket returns TCPOptions valid for websocket net.Conn
|
||||
|
||||
@@ -167,12 +167,24 @@ func (srv *Server) UseIdleTimeout(d time.Duration) *Server {
|
||||
return srv
|
||||
}
|
||||
|
||||
// UseReadTimeout configure connection request read timeout.
|
||||
func (srv *Server) UseReadTimeout(d time.Duration) *Server {
|
||||
srv.ReadTimeout = d
|
||||
return srv
|
||||
}
|
||||
|
||||
// UseReadHeaderTimeout configure read header timeout
|
||||
func (srv *Server) UseReadHeaderTimeout(d time.Duration) *Server {
|
||||
srv.ReadHeaderTimeout = d
|
||||
return srv
|
||||
}
|
||||
|
||||
// UseWriteTimeout configure connection response write timeout.
|
||||
func (srv *Server) UseWriteTimeout(d time.Duration) *Server {
|
||||
srv.WriteTimeout = d
|
||||
return srv
|
||||
}
|
||||
|
||||
// UseHandler configure final handler for this HTTP *Server
|
||||
func (srv *Server) UseHandler(h http.Handler) *Server {
|
||||
srv.Handler = h
|
||||
|
||||
Reference in New Issue
Block a user