feat: healthcheck requires minimum ASN count

This commit is contained in:
2026-01-19 19:24:17 +01:00
parent 623c81d406
commit 34e85bb6cd
4 changed files with 47 additions and 3 deletions

View File

@@ -12,3 +12,6 @@ UPDATE_INTERVAL_SECONDS=2592000
# PeeringDB info_type filter (should match PeeringDB values) # PeeringDB info_type filter (should match PeeringDB values)
PDB_INFO_TYPE=Educational/Research PDB_INFO_TYPE=Educational/Research
# minimum ASN entries for healthy /healthz
MIN_ASN_COUNT=10

View File

@@ -76,6 +76,13 @@ Der Service ist **nicht öffentlich exponiert** und kommuniziert ausschließlich
--- ---
## Healthcheck
- `GET /healthz` liefert `200`, wenn mindestens `MIN_ASN_COUNT` ASNs geladen sind
- Standard: `MIN_ASN_COUNT=10` (konfigurierbar via Env)
---
## Einschränkungen ## Einschränkungen
- Die Erkennung ist **heuristisch** - Die Erkennung ist **heuristisch**

View File

@@ -76,6 +76,13 @@ Der Service ist **nicht öffentlich exponiert** und kommuniziert ausschließlich
--- ---
## Healthcheck
- `GET /healthz` liefert `200`, wenn mindestens `MIN_ASN_COUNT` ASNs geladen sind
- Standard: `MIN_ASN_COUNT=10` (konfigurierbar via Env)
---
## Einschränkungen ## Einschränkungen
- Die Erkennung ist **heuristisch** - Die Erkennung ist **heuristisch**

33
main.go
View File

@@ -24,6 +24,8 @@ type server struct {
nrenASNs map[uint]struct{} nrenASNs map[uint]struct{}
ready atomic.Bool ready atomic.Bool
versionTag string versionTag string
minASN int
asnCount int
} }
func loadASNSet(path string) (map[uint]struct{}, error) { func loadASNSet(path string) (map[uint]struct{}, error) {
@@ -119,6 +121,7 @@ func main() {
asnListPath := getenv("ASN_LIST_PATH", "/data/nren_asns.txt") asnListPath := getenv("ASN_LIST_PATH", "/data/nren_asns.txt")
addr := getenv("ADDR", ":8080") addr := getenv("ADDR", ":8080")
version := getenv("VERSION_TAG", "asn-header-service") version := getenv("VERSION_TAG", "asn-header-service")
minASN := getenvInt("MIN_ASN_COUNT", 10)
db, err := maxminddb.Open(mmdbPath) db, err := maxminddb.Open(mmdbPath)
if err != nil { if err != nil {
@@ -130,13 +133,26 @@ func main() {
if err != nil { if err != nil {
log.Fatalf("failed to load asn list: %v", err) log.Fatalf("failed to load asn list: %v", err)
} }
asnCount := len(set)
s := &server{db: db, nrenASNs: set, versionTag: version} s := &server{
db: db,
nrenASNs: set,
versionTag: version,
minASN: minASN,
asnCount: asnCount,
}
s.ready.Store(true) s.ready.Store(true)
mux := http.NewServeMux() mux := http.NewServeMux()
mux.HandleFunc("/auth", s.authHandler) mux.HandleFunc("/auth", s.authHandler)
mux.HandleFunc("/healthz", func(w http.ResponseWriter, _ *http.Request) { w.WriteHeader(http.StatusOK) }) mux.HandleFunc("/healthz", func(w http.ResponseWriter, _ *http.Request) {
if s.asnCount < s.minASN {
w.WriteHeader(http.StatusServiceUnavailable)
return
}
w.WriteHeader(http.StatusOK)
})
srv := &http.Server{ srv := &http.Server{
Addr: addr, Addr: addr,
@@ -144,7 +160,7 @@ func main() {
ReadHeaderTimeout: 2 * time.Second, ReadHeaderTimeout: 2 * time.Second,
} }
log.Printf("listening on %s (asn_count=%d)", addr, len(set)) log.Printf("listening on %s (asn_count=%d, min_asn=%d)", addr, asnCount, minASN)
log.Fatal(srv.ListenAndServe()) log.Fatal(srv.ListenAndServe())
} }
@@ -156,3 +172,14 @@ func getenv(k, def string) string {
return v return v
} }
func getenvInt(k string, def int) int {
v := strings.TrimSpace(os.Getenv(k))
if v == "" {
return def
}
parsed, err := strconv.Atoi(v)
if err != nil {
return def
}
return parsed
}