From 5b9253290170ea354218fe758aefe46e64d12cea Mon Sep 17 00:00:00 2001 From: AJ ONeal Date: Tue, 8 Sep 2026 22:17:22 -0600 Subject: [PATCH] fix(iplist): preserve cache after invalid refresh --- net/iplist/iplist.go | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/net/iplist/iplist.go b/net/iplist/iplist.go index b106a9a..d0285a7 100644 --- a/net/iplist/iplist.go +++ b/net/iplist/iplist.go @@ -94,12 +94,13 @@ func readSource(ctx context.Context, source, cacheDir string, seen map[string]st } digest := sha256.Sum256([]byte(key)) path := filepath.Join(cacheDir, "ip-sources", hex.EncodeToString(digest[:8])+".tsv") + previous, previousErr := os.ReadFile(path) cache := httpcache.NewWith(parsed.String(), path, https.NewInternalClient()) cache.Header = header cache.MaxAge = defaultMaxAge cache.MaxBytes = defaultMaxBytes if _, fetchErr := cache.Fetch(ctx); fetchErr != nil { - if _, statErr := os.Stat(path); statErr != nil { + if previousErr != nil { return nil, fmt.Errorf("fetch %s: %w", safeURL(source), fetchErr) } } @@ -107,7 +108,15 @@ func readSource(ctx context.Context, source, cacheDir string, seen map[string]st if err != nil { return nil, fmt.Errorf("read cached source %s: %w", safeURL(source), err) } - return Parse(strings.NewReader(string(body))) + entries, err := Parse(strings.NewReader(string(body))) + if err != nil { + if previousErr == nil { + _ = os.WriteFile(path, previous, 0o600) + _ = os.Remove(path + ".meta") + } + return nil, fmt.Errorf("parse cached source %s: %w", safeURL(source), err) + } + return entries, nil } // Parse reads the first TSV column. Blank lines, comments, and an optional