Improve HTTP timeout config
This commit is contained in:
@ -2,10 +2,12 @@ package config
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"flag"
|
||||
"fmt"
|
||||
"os"
|
||||
"runtime/debug"
|
||||
"time"
|
||||
|
||||
"github.com/caarlos0/env/v11"
|
||||
"gopkg.in/yaml.v3"
|
||||
@ -58,9 +60,52 @@ func loadConfig(configPath string) (*AppConfig, error) {
|
||||
return nil, fmt.Errorf("could not parse environment variables: %w", err)
|
||||
}
|
||||
|
||||
// Perform updates / enrichments
|
||||
err := prepareConfig(&cfg)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return &cfg, nil
|
||||
}
|
||||
|
||||
func prepareConfig(cfg *AppConfig) error {
|
||||
var errs error
|
||||
|
||||
// Set timeouts
|
||||
if cfg.HTTP.ReadTimeout != "" {
|
||||
if rT, err := time.ParseDuration(cfg.HTTP.ReadTimeout); err == nil {
|
||||
cfg.HTTP.rT = &rT
|
||||
} else {
|
||||
errs = errors.Join(errs, err)
|
||||
}
|
||||
}
|
||||
|
||||
if cfg.HTTP.ReadTimeout != "" {
|
||||
if wT, err := time.ParseDuration(cfg.HTTP.WriteTimeout); err == nil {
|
||||
cfg.HTTP.wT = &wT
|
||||
} else {
|
||||
errs = errors.Join(errs, err)
|
||||
}
|
||||
}
|
||||
|
||||
if cfg.HTTP.IdleTimeout != "" {
|
||||
if iT, err := time.ParseDuration(cfg.HTTP.IdleTimeout); err == nil {
|
||||
cfg.HTTP.iT = &iT
|
||||
} else {
|
||||
errs = errors.Join(errs, err)
|
||||
}
|
||||
}
|
||||
|
||||
return errs
|
||||
}
|
||||
|
||||
// Returns read timeout, write timeout, and idle timeout, in that order
|
||||
// nil if unset
|
||||
func (h *HTTPConfig) Timeouts() (*time.Duration, *time.Duration, *time.Duration) {
|
||||
return h.rT, h.wT, h.iT
|
||||
}
|
||||
|
||||
func getVersion() string {
|
||||
if info, ok := debug.ReadBuildInfo(); ok && info.Main.Version != "(devel)" {
|
||||
return info.Main.Version
|
||||
|
Reference in New Issue
Block a user