various: extract magic numbers into named constants

Signed-off-by: NotAShelf <raf@notashelf.dev>
Change-Id: I854b2f9b5f39e4629c32e5681e6322826a6a6964
This commit is contained in:
raf 2026-03-01 21:14:07 +03:00
commit 7b06c4f2ca
Signed by: NotAShelf
GPG key ID: 29D95B64378DB4BF
4 changed files with 22 additions and 9 deletions

View file

@ -11,13 +11,13 @@ import (
"path/filepath" "path/filepath"
"strings" "strings"
"syscall" "syscall"
"time"
"github.com/prometheus/client_golang/prometheus" "github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp" "github.com/prometheus/client_golang/prometheus/promhttp"
"notashelf.dev/watchdog/internal/aggregate" "notashelf.dev/watchdog/internal/aggregate"
"notashelf.dev/watchdog/internal/api" "notashelf.dev/watchdog/internal/api"
"notashelf.dev/watchdog/internal/config" "notashelf.dev/watchdog/internal/config"
"notashelf.dev/watchdog/internal/limits"
"notashelf.dev/watchdog/internal/normalize" "notashelf.dev/watchdog/internal/normalize"
) )
@ -91,9 +91,9 @@ func Run(cfg *config.Config) error {
srv := &http.Server{ srv := &http.Server{
Addr: cfg.Server.ListenAddr, Addr: cfg.Server.ListenAddr,
Handler: mux, Handler: mux,
ReadTimeout: 10 * time.Second, ReadTimeout: limits.HTTPReadTimeout,
WriteTimeout: 10 * time.Second, WriteTimeout: limits.HTTPWriteTimeout,
IdleTimeout: 60 * time.Second, IdleTimeout: limits.HTTPIdleTimeout,
} }
// Start server in goroutine // Start server in goroutine
@ -115,8 +115,8 @@ func Run(cfg *config.Config) error {
case sig := <-shutdown: case sig := <-shutdown:
log.Printf("Received signal: %v, starting graceful shutdown", sig) log.Printf("Received signal: %v, starting graceful shutdown", sig)
// Give outstanding requests 30 seconds to complete // Give outstanding requests time to complete
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second) ctx, cancel := context.WithTimeout(context.Background(), limits.ShutdownTimeout)
defer cancel() defer cancel()
// Shutdown metrics aggregator. // Shutdown metrics aggregator.

View file

@ -9,6 +9,7 @@ import (
"github.com/prometheus/client_golang/prometheus" "github.com/prometheus/client_golang/prometheus"
"notashelf.dev/watchdog/internal/config" "notashelf.dev/watchdog/internal/config"
"notashelf.dev/watchdog/internal/limits"
) )
var prometheusLabelPattern = regexp.MustCompile(`^[a-zA-Z0-9_/:.-]*$`) var prometheusLabelPattern = regexp.MustCompile(`^[a-zA-Z0-9_/:.-]*$`)
@ -121,11 +122,11 @@ func NewMetricsAggregator(
return m return m
} }
// Background goroutine to update the unique visitors gauge every 10 seconds // Background goroutine to update the unique visitors gauge periodically
// instead of on every request. This should help with performance. // instead of on every request. This should help with performance.
func (m *MetricsAggregator) updateUniquesGauge() { func (m *MetricsAggregator) updateUniquesGauge() {
defer m.wg.Done() defer m.wg.Done()
ticker := time.NewTicker(10 * time.Second) ticker := time.NewTicker(limits.UniquesUpdatePeriod)
defer ticker.Stop() defer ticker.Stop()
for { for {

View file

@ -1,5 +1,7 @@
package limits package limits
import "time"
// Size limits for request processing // Size limits for request processing
const ( const (
MaxEventSize = 4 * 1024 // 4KB max event payload MaxEventSize = 4 * 1024 // 4KB max event payload
@ -7,3 +9,12 @@ const (
MaxRefLen = 2048 // max referrer length MaxRefLen = 2048 // max referrer length
MaxWidth = 10000 // max reasonable screen width MaxWidth = 10000 // max reasonable screen width
) )
// Timeout constants
const (
HTTPReadTimeout = 10 * time.Second // HTTP server read timeout
HTTPWriteTimeout = 10 * time.Second // HTTP server write timeout
HTTPIdleTimeout = 60 * time.Second // HTTP server idle timeout
ShutdownTimeout = 30 * time.Second // graceful shutdown timeout
UniquesUpdatePeriod = 10 * time.Second // HLL gauge update interval
)

View file

@ -4,6 +4,7 @@ import (
"strings" "strings"
"notashelf.dev/watchdog/internal/config" "notashelf.dev/watchdog/internal/config"
"notashelf.dev/watchdog/internal/limits"
) )
type PathNormalizer struct { type PathNormalizer struct {
@ -14,7 +15,7 @@ type PathNormalizer struct {
func NewPathNormalizer(cfg config.PathConfig) *PathNormalizer { func NewPathNormalizer(cfg config.PathConfig) *PathNormalizer {
return &PathNormalizer{ return &PathNormalizer{
cfg: cfg, cfg: cfg,
maxLength: 2048, maxLength: limits.MaxPathLen,
} }
} }