feat: healthcheck requires minimum ASN count
This commit is contained in:
33
main.go
33
main.go
@@ -24,6 +24,8 @@ type server struct {
|
||||
nrenASNs map[uint]struct{}
|
||||
ready atomic.Bool
|
||||
versionTag string
|
||||
minASN int
|
||||
asnCount int
|
||||
}
|
||||
|
||||
func loadASNSet(path string) (map[uint]struct{}, error) {
|
||||
@@ -119,6 +121,7 @@ func main() {
|
||||
asnListPath := getenv("ASN_LIST_PATH", "/data/nren_asns.txt")
|
||||
addr := getenv("ADDR", ":8080")
|
||||
version := getenv("VERSION_TAG", "asn-header-service")
|
||||
minASN := getenvInt("MIN_ASN_COUNT", 10)
|
||||
|
||||
db, err := maxminddb.Open(mmdbPath)
|
||||
if err != nil {
|
||||
@@ -130,13 +133,26 @@ func main() {
|
||||
if err != nil {
|
||||
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)
|
||||
|
||||
mux := http.NewServeMux()
|
||||
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{
|
||||
Addr: addr,
|
||||
@@ -144,7 +160,7 @@ func main() {
|
||||
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())
|
||||
}
|
||||
|
||||
@@ -156,3 +172,14 @@ func getenv(k, def string) string {
|
||||
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
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user