diff --git a/main.go b/main.go index 7f0577c..50c8c38 100644 --- a/main.go +++ b/main.go @@ -95,7 +95,7 @@ func main() { // Admin load all blocklists (on demand or scheduled) go func() { - if getenv("IMPORT_RIRS", "1") == "1" { + if getenv("IMPORT_RIRS", "0") == "1" { log.Println("Lade IP-Ranges aus RIRs...") if err := importRIRDataToRedis(); err != nil { log.Fatalf("import error: %v", err) @@ -193,38 +193,40 @@ func supernets(ip netip.Addr) []string { } func checkIP(ip netip.Addr, cats []string) ([]string, error) { + // 1) Cache-Treffer? if res, ok := ipCache.Get(ip.String()); ok { hits.Add(1) return res, nil } + // 2) alle Supernetze der IP (≤32 bzw. ≤128 Stück) supers := supernets(ip) - pipe := rdb.Pipeline() - cmds := make([]*redis.IntCmd, 0, len(cats)*len(supers)) - // *einen* Redis-Roundtrip bauen - for _, cat := range cats { - for _, pfx := range supers { - key := "bl:" + cat + ":" + pfx - cmds = append(cmds, pipe.Exists(ctx, key)) + // 3) Pipeline – jeweils *eine* EXISTS-Abfrage pro Kategorie + pipe := rdb.Pipeline() + existsCmds := make([]*redis.IntCmd, len(cats)) + + for i, cat := range cats { + keys := make([]string, len(supers)) + for j, pfx := range supers { + keys[j] = "bl:" + cat + ":" + pfx } + existsCmds[i] = pipe.Exists(ctx, keys...) } + if _, err := pipe.Exec(ctx); err != nil && err != redis.Nil { return nil, err } + // 4) Ergebnis auswerten matches := make([]string, 0, len(cats)) - idx := 0 - for _, cat := range cats { - for range supers { - if cmds[idx].Val() == 1 { - matches = append(matches, cat) - break // Kategorie gefunden → nächste - } - idx++ + for i, cat := range cats { + if existsCmds[i].Val() > 0 { + matches = append(matches, cat) } } + // 5) Cache befüllen und zurück misses.Add(1) ipCache.Add(ip.String(), matches) return matches, nil