Add config option idle_timeout

This commit is contained in:
Holger Obermaier 2023-09-20 16:39:03 +02:00
parent 0c95db50ad
commit f6b5f7fb07

View File

@ -23,6 +23,11 @@ type HttpReceiverConfig struct {
Addr string `json:"address"` Addr string `json:"address"`
Port string `json:"port"` Port string `json:"port"`
Path string `json:"path"` Path string `json:"path"`
// Maximum amount of time to wait for the next request when keep-alives are enabled
// should be larger than the measurement interval to keep the connection open
IdleTimeout string `json:"idle_timeout"`
idleTimeout time.Duration
} }
type HttpReceiver struct { type HttpReceiver struct {
@ -36,7 +41,13 @@ type HttpReceiver struct {
func (r *HttpReceiver) Init(name string, config json.RawMessage) error { func (r *HttpReceiver) Init(name string, config json.RawMessage) error {
r.name = fmt.Sprintf("HttpReceiver(%s)", name) r.name = fmt.Sprintf("HttpReceiver(%s)", name)
// Set default values
r.config.Port = HTTP_RECEIVER_PORT r.config.Port = HTTP_RECEIVER_PORT
// should be larger than the measurement interval to keep the connection open
r.config.IdleTimeout = "120s"
// Read config
if len(config) > 0 { if len(config) > 0 {
err := json.Unmarshal(config, &r.config) err := json.Unmarshal(config, &r.config)
if err != nil { if err != nil {
@ -47,6 +58,13 @@ func (r *HttpReceiver) Init(name string, config json.RawMessage) error {
if len(r.config.Port) == 0 { if len(r.config.Port) == 0 {
return errors.New("not all configuration variables set required by HttpReceiver") return errors.New("not all configuration variables set required by HttpReceiver")
} }
if len(r.config.IdleTimeout) > 0 {
t, err := time.ParseDuration(r.config.IdleTimeout)
if err == nil {
cclog.ComponentDebug(r.name, "idleTimeout", t)
r.config.idleTimeout = t
}
}
r.meta = map[string]string{"source": r.name} r.meta = map[string]string{"source": r.name}
p := r.config.Path p := r.config.Path
if !strings.HasPrefix(p, "/") { if !strings.HasPrefix(p, "/") {
@ -62,8 +80,9 @@ func (r *HttpReceiver) Init(name string, config json.RawMessage) error {
// Create http server, with router as handler // Create http server, with router as handler
r.server = &http.Server{ r.server = &http.Server{
Addr: addr, Addr: addr,
Handler: r.router, Handler: r.router,
IdleTimeout: r.config.idleTimeout,
} }
return nil return nil
} }