interal/api: replace liner array scan with hashmap lookup in domain validation

Signed-off-by: NotAShelf <raf@notashelf.dev>
Change-Id: Iac969e7dc6e4ca3f93410fccac1995636a6a6964
This commit is contained in:
raf 2026-03-01 20:08:50 +03:00
commit 4e0b8f0d0a
Signed by: NotAShelf
GPG key ID: 29D95B64378DB4BF
3 changed files with 77 additions and 9 deletions

View file

@ -15,6 +15,7 @@ import (
// Handles incoming analytics events
type IngestionHandler struct {
cfg *config.Config
domainMap map[string]bool // O(1) domain validation
pathNorm *normalize.PathNormalizer
pathRegistry *aggregate.PathRegistry
refRegistry *normalize.ReferrerRegistry
@ -40,8 +41,15 @@ func NewIngestionHandler(
)
}
// Build domain map for O(1) validation
domainMap := make(map[string]bool, len(cfg.Site.Domains))
for _, domain := range cfg.Site.Domains {
domainMap[domain] = true
}
return &IngestionHandler{
cfg: cfg,
domainMap: domainMap,
pathNorm: pathNorm,
pathRegistry: pathRegistry,
refRegistry: refRegistry,
@ -95,8 +103,8 @@ func (h *IngestionHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
return
}
// Validate event
if err := event.Validate(h.cfg.Site.Domains); err != nil {
// Validate event via map lookup (also O(1))
if err := event.ValidateWithMap(h.domainMap); err != nil {
http.Error(w, "Bad request", http.StatusBadRequest)
return
}