package pipeline import ( "context" "net/http" "net/netip" "os" "strconv" "strings" "time" "evobgp/internal/store" ) func domainCacheTTL() time.Duration { sec := 300 if s := strings.TrimSpace(os.Getenv("EVOBGP_DOMAIN_CACHE_TTL_SEC")); s != "" { if v, err := strconv.Atoi(s); err == nil && v > 0 { sec = v } } return time.Duration(sec) * time.Second } func resolveDomainIPsCached(ctx context.Context, st store.Backend, hc *http.Client, profiles []*store.DohProfile, policy, fqdn string) ([]netip.Addr, error) { key := strings.TrimSpace(fqdn) ttl := domainCacheTTL() if st != nil && ttl > 0 { if ent, ok, err := st.GetDomainResolveCache(key); err == nil && ok && ent != nil { if time.Since(ent.ResolvedAt) < ttl { if addrs := parseCachedDomainAddrs(ent.Addrs); len(addrs) > 0 { return addrs, nil } } } } addrs, err := resolveDomainIPsWithPolicy(ctx, hc, profiles, policy, key) if err != nil { return nil, err } if st != nil { _ = st.SetDomainResolveCache(key, domainAddrsToStrings(addrs)) } return addrs, nil } func parseCachedDomainAddrs(raw []string) []netip.Addr { out := make([]netip.Addr, 0, len(raw)) for _, s := range raw { a, err := netip.ParseAddr(strings.TrimSpace(s)) if err != nil { continue } out = append(out, a) } return out } func domainAddrsToStrings(addrs []netip.Addr) []string { out := make([]string, 0, len(addrs)) for _, a := range addrs { if a.IsValid() { out = append(out, a.String()) } } return out }