Files
routewatch/internal/database/utils.go
T
sneak 539d15c0a5
check / check (push) Waiting to run
Serve the /api/v1/stats prefix distribution from memory (closes #30)
The prefix distribution was the last query on the stats path. It read one
index entry per live route on every request, so on a large database it took
the whole 4-second deadline and /api/v1/stats answered 500.

The distribution now lives next to the in-memory counts: seeded once at
startup from the same query, then kept exact by the live-route writes. A new
route whose prefix had no live route adds one at its mask length; a delete
that leaves a prefix with no live route takes one away. Each check is one
lookup on the prefix index, made only for new and removed routes, inside the
write.

Model: opus-5-5
2026-09-29 11:05:28 +00:00

34 lines
662 B
Go

package database
import (
"fmt"
"net"
"strings"
"github.com/google/uuid"
)
func generateUUID() uuid.UUID {
return uuid.New()
}
// detectIPVersion determines if a prefix is IPv4 (returns 4) or IPv6 (returns 6)
func detectIPVersion(prefix string) int {
if strings.Contains(prefix, ":") {
return ipVersionV6
}
return ipVersionV4
}
// prefixMaskLength returns the mask length of a prefix such as 192.0.2.0/24.
func prefixMaskLength(prefix string) (int, error) {
_, network, err := net.ParseCIDR(prefix)
if err != nil {
return 0, fmt.Errorf("invalid prefix %s: %w", prefix, err)
}
maskLength, _ := network.Mask.Size()
return maskLength, nil
}