From 15d484552140eafe491e01d7f7bbfb558515801b Mon Sep 17 00:00:00 2001 From: yeliheng-010 <81519560+yeliheng-010@users.noreply.github.com> Date: Fri, 25 Sep 2026 21:34:36 +0800 Subject: [PATCH] =?UTF-8?q?fix(rss):=20=E4=BF=AE=E5=A4=8D=E5=85=A8?= =?UTF-8?q?=E6=96=87=E5=9B=9E=E9=80=80=E5=90=8E=E5=A2=9E=E9=87=8F=E5=90=8C?= =?UTF-8?q?=E6=AD=A5=E4=B8=8D=E9=87=8D=E8=AF=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../connector/rss/article_retry_test.go | 134 ++++++++++++++++++ .../datasource/connector/rss/connector.go | 16 ++- 2 files changed, 146 insertions(+), 4 deletions(-) create mode 100644 internal/datasource/connector/rss/article_retry_test.go diff --git a/internal/datasource/connector/rss/article_retry_test.go b/internal/datasource/connector/rss/article_retry_test.go new file mode 100644 index 00000000..8cd5d7a4 --- /dev/null +++ b/internal/datasource/connector/rss/article_retry_test.go @@ -0,0 +1,134 @@ +package rss + +import ( + "context" + "fmt" + "net/http" + "net/http/httptest" + "sync/atomic" + "testing" + + "github.com/Tencent/WeKnora/internal/types" + "github.com/stretchr/testify/require" +) + +type articleRetryFeed struct { + server *httptest.Server + status atomic.Int32 + articleReads atomic.Int32 + healthyReads atomic.Int32 + summary string + includeLink bool +} + +func newArticleRetryFeed(t *testing.T) *articleRetryFeed { + t.Helper() + f := &articleRetryFeed{summary: "Temporary summary", includeLink: true} + f.status.Store(http.StatusServiceUnavailable) + f.server = httptest.NewServer(http.HandlerFunc(f.serveHTTP)) + t.Cleanup(f.server.Close) + return f +} + +func (f *articleRetryFeed) serveHTTP(w http.ResponseWriter, r *http.Request) { + switch r.URL.Path { + case "/feed.xml": + link := "" + if f.includeLink { + link = "http://" + r.Host + "/article" + } + w.Header().Set("Content-Type", "application/rss+xml") + _, _ = fmt.Fprintf(w, `Retry feed +retryRetry article%s +healthyHealthy articlehttp://%s/healthy +`, link, f.summary, r.Host) + case "/article": + f.articleReads.Add(1) + if status := int(f.status.Load()); status != http.StatusOK { + w.WriteHeader(status) + return + } + writeRetryArticle(w) + case "/healthy": + f.healthyReads.Add(1) + writeRetryArticle(w) + default: + http.NotFound(w, r) + } +} + +func writeRetryArticle(w http.ResponseWriter) { + w.Header().Set("Content-Type", "text/html; charset=utf-8") + _, _ = fmt.Fprintf(w, "Article
%s
", + longArticleBody) +} + +func (f *articleRetryFeed) sync(t *testing.T, cursor *types.SyncCursor) ([]types.FetchedItem, *types.SyncCursor) { + t.Helper() + url := f.server.URL + "/feed.xml" + cfg := makeConfig(url, "") + cfg.ResourceIDs = []string{url} + items, next, err := NewConnector().FetchIncremental(context.Background(), cfg, cursor) + require.NoError(t, err) + require.NotNil(t, next) + return items, next +} + +func TestIncrementalRetriesArticleAfterFallback(t *testing.T) { + for _, status := range []int{http.StatusNotFound, http.StatusServiceUnavailable} { + t.Run(http.StatusText(status), func(t *testing.T) { + f := newArticleRetryFeed(t) + f.status.Store(int32(status)) + items, cursor := f.sync(t, nil) + require.Len(t, items, 2) + require.Equal(t, f.summary, string(items[0].Content)) + items, cursor = f.sync(t, cursor) + require.Empty(t, items, "unchanged fallback must not be ingested again") + require.EqualValues(t, 2, f.articleReads.Load(), "failed article must be retried on the next sync") + require.EqualValues(t, 1, f.healthyReads.Load(), "successful sibling should stay cached") + f.status.Store(http.StatusOK) + items, cursor = f.sync(t, cursor) + require.Len(t, items, 1) + require.Contains(t, string(items[0].Content), "reasonably long article") + items, _ = f.sync(t, cursor) + require.Empty(t, items) + require.EqualValues(t, 3, f.articleReads.Load(), "recovered article should now stay cached") + require.EqualValues(t, 1, f.healthyReads.Load()) + }) + } +} + +func TestIncrementalRetriesArticleWithEmptyFallback(t *testing.T) { + f := newArticleRetryFeed(t) + f.summary = "" + _, cursor := f.sync(t, nil) + f.status.Store(http.StatusOK) + items, _ := f.sync(t, cursor) + require.Len(t, items, 1) + require.NotEmpty(t, items[0].Content) + require.EqualValues(t, 2, f.articleReads.Load()) +} + +func TestIncrementalCachesRecoveryWithIdenticalContent(t *testing.T) { + f := newArticleRetryFeed(t) + f.summary = longArticleBody + _, cursor := f.sync(t, nil) + f.status.Store(http.StatusOK) + items, cursor := f.sync(t, cursor) + require.Empty(t, items, "identical recovered content does not need ingestion") + require.EqualValues(t, 2, f.articleReads.Load(), "the article must have been fetched successfully") + items, _ = f.sync(t, cursor) + require.Empty(t, items) + require.EqualValues(t, 2, f.articleReads.Load(), "success should cache the feed signal despite equal content") +} + +func TestIncrementalCachesEntryWithoutArticleLink(t *testing.T) { + f := newArticleRetryFeed(t) + f.includeLink = false + items, cursor := f.sync(t, nil) + require.Len(t, items, 2) + items, _ = f.sync(t, cursor) + require.Empty(t, items) + require.Zero(t, f.articleReads.Load()) + require.EqualValues(t, 1, f.healthyReads.Load()) +} diff --git a/internal/datasource/connector/rss/connector.go b/internal/datasource/connector/rss/connector.go index b84dc6ac..98e7f2cb 100644 --- a/internal/datasource/connector/rss/connector.go +++ b/internal/datasource/connector/rss/connector.go @@ -260,7 +260,11 @@ func (c *Connector) walk( resolved := c.resolveItem(ctx, cli, feed, item, feedURL, itemID, feedContent) newCursor.FeedItems[feedURL][itemID] = resolved.fingerprint - newCursor.FeedSignals[feedURL][itemID] = feedSig + // Keep fallback deduplication, but retry failed full-text extraction on + // the next sync even when the feed entry itself has not changed. + if !resolved.articleFailed { + newCursor.FeedSignals[feedURL][itemID] = feedSig + } if incremental && prevItems != nil && prevItems[itemID] == resolved.fingerprint { skipped++ @@ -285,8 +289,9 @@ func (c *Connector) walk( } type resolvedFeedItem struct { - item types.FetchedItem - fingerprint string + item types.FetchedItem + fingerprint string + articleFailed bool } // resolveItem assembles a FetchedItem for a single feed entry, resolving the @@ -303,6 +308,7 @@ func (c *Connector) resolveItem( // Prefer full article text; fall back to feed-provided content on failure. contentHTML := feedContent + articleFailed := false if strings.TrimSpace(item.Link) != "" { if articleHTML, articleTitle, err := cli.extractArticle(ctx, item.Link); err == nil { contentHTML = articleHTML @@ -310,6 +316,7 @@ func (c *Connector) resolveItem( title = articleTitle } } else { + articleFailed = true logger.Warnf(ctx, "[RSS] full-text fetch failed for %s (using feed content): %v", item.Link, err) } } @@ -330,7 +337,8 @@ func (c *Connector) resolveItem( } return resolvedFeedItem{ - fingerprint: contentFingerprint(content), + fingerprint: contentFingerprint(content), + articleFailed: articleFailed, item: types.FetchedItem{ ExternalID: itemExternalID(feedURL, itemID), Title: title,