Improve HTTP timeout config

This commit is contained in:
2025-01-05 15:35:27 -05:00
parent 4a99f06987
commit c6514e0590
5 changed files with 80 additions and 28 deletions

View File

@ -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