package config import ( "errors" "reflect" "time" ) // JSCKServerTimeouts is the server timeouts section's configuration key. const JSCKServerTimeouts string = "timeouts" // JSCServerTimeouts configures the server's HTTP timeouts, as those of // http.Server: ReadHeaderTimeout for reading a request's headers, // ReadTimeout for the whole request, WriteTimeout for writing the response // and IdleTimeout for waiting for the next request on a keep-alive // connection. 0 or less means no limit, except that ReadHeaderTimeout must be // greater than 0 (see validate). ShutdownTimeout is how long shutdown waits // for clients to close; 0 or less means it waits until they have. type JSCServerTimeouts struct { ReadHeaderTimeout time.Duration `cfg:"read_header" default:"5s"` ReadTimeout time.Duration `cfg:"read" default:"15s"` WriteTimeout time.Duration `cfg:"write" default:"30s"` IdleTimeout time.Duration `cfg:"idle" default:"2m"` ShutdownTimeout time.Duration `cfg:"shutdown" default:"15s"` } // ---------- Section methods (see Section) ---------- // Map implements Section. func (c JSCServerTimeouts) Map() map[string]any { return mapOf(c) } // Flatten implements Section. func (c JSCServerTimeouts) Flatten() map[string]any { return flattenOf(JSCKServerTimeouts, c) } // TypeOf implements Section. func (c JSCServerTimeouts) TypeOf(key string) (reflect.Kind, error) { return typeOf(c, key) } // ValueOf implements Section. func (c JSCServerTimeouts) ValueOf(key string) (any, error) { return valueOf(c, key) } // SetValue implements Section. func (c *JSCServerTimeouts) SetValue(key string, value any) error { return setValue(c, key, value) } // ---------- Section specific methods ---------- // validate checks values before they are stored (see validator). // read_header must be greater than 0: without it a client could hold a // connection open for ever by sending its headers slowly (slowloris). func (c *JSCServerTimeouts) validate(key string, value any) error { if key == "read_header" && value.(time.Duration) <= 0 { return errors.New( "config: read_header must be greater than 0, as a client could otherwise hold a connection open for ever", ) } return nil }