5.5 KiB
limits
import "github.com/effective-security/porto/xhttp/limits"
Package limits defines shared server network limits. Zero read deadlines and body sizes select conservative defaults; negative values explicitly disable individual limits. The handler deadline (Timeouts.Handler) has no default: it bounds each request's context only when set. Configure limits before starting a server:
timeouts := limits.Timeouts{Handler: 2 * time.Minute}
srv := &http.Server{Handler: timeouts.ApplyHandler(handler)}
timeouts.ApplyHTTP(srv)
Index
Constants
const (
// DefaultHeaderTimeout bounds the time spent reading HTTP request headers.
DefaultHeaderTimeout = 10 * time.Second
// DefaultReadTimeout bounds the time spent reading an entire HTTP request.
DefaultReadTimeout = 30 * time.Second
// DefaultIdleTimeout bounds the wait for the next keepalive request.
DefaultIdleTimeout = 60 * time.Second
// DefaultHandshakeTimeout bounds TLS handshakes and protocol detection.
DefaultHandshakeTimeout = 10 * time.Second
// DefaultMaxRequestBody is the HTTP request body limit in bytes (10 MiB).
DefaultMaxRequestBody = 10 * 1024 * 1024
)
type Timeouts
Timeouts configures server deadlines. Header, Read, Idle and Handshake are read deadlines: zero selects the default for that field and a negative duration disables it. Handler is the server-side bound on a request's execution; it has no default and is off unless set. There is no response write deadline. Streaming callers may need to raise or disable Read and leave Handler unset, or set it above their longest stream.
type Timeouts struct {
// Header sets net/http ReadHeaderTimeout for HTTP/1.x headers.
// HTTP/2 has no per-stream header deadline in net/http.
Header time.Duration `json:"header,omitempty" yaml:"header,omitempty"`
// Read limits reading the whole HTTP request, including its body.
Read time.Duration `json:"read,omitempty" yaml:"read,omitempty"`
// Idle limits waiting for the next HTTP keepalive request.
Idle time.Duration `json:"idle,omitempty" yaml:"idle,omitempty"`
// Handshake limits cmux detection and transport's eager TLS handshake.
// net/http TLS listeners use the smaller positive Header or Read deadline.
Handshake time.Duration `json:"handshake,omitempty" yaml:"handshake,omitempty"`
// Handler bounds the execution of each request: its context expires
// Handler after the server starts handling it, unless the client asked
// for an earlier deadline, which is kept. Zero and negative leave
// requests bounded only by the client's deadline (the default). It
// applies to REST requests and to gRPC and gRPC-Web calls, streams
// included; a handler that ignores its context outlives the deadline.
// It is not a net/http setting, so ApplyHTTP does not apply it: servers
// wrap their handler chain with ApplyHandler and gRPC interceptors use
// HandlerContext.
Handler time.Duration `json:"handler,omitempty" yaml:"handler,omitempty"`
}
func (Timeouts) ApplyHTTP
func (t Timeouts) ApplyHTTP(s *http.Server)
ApplyHTTP sets the read deadlines on a server before it starts serving. Negative values are preserved to disable net/http's fallback deadlines. Handler is not a server setting; see ApplyHandler.
func (Timeouts) ApplyHandler
func (t Timeouts) ApplyHandler(handler http.Handler) http.Handler
ApplyHandler returns handler served with each request's context bounded by t.Handler (see HandlerContext). When Handler is not positive it returns handler itself. Servers call it on their handler chain before serving.
func (Timeouts) HandlerContext
func (t Timeouts) HandlerContext(ctx context.Context) (context.Context, context.CancelFunc)
HandlerContext returns ctx bounded by t.Handler: when Handler is positive, the returned context expires after Handler unless ctx already has an earlier deadline, which is kept. Otherwise ctx is returned unchanged with a cancel function that does nothing. The caller must call cancel when the request ends to release the timer.
func (Timeouts) WithDefaults
func (t Timeouts) WithDefaults() Timeouts
WithDefaults returns a copy with zero read deadlines replaced by their defaults. Handler has no default and is returned as is.
Generated by gomarkdoc