DocumentDb 优化非分区更改源的恢复



我正在创建一个未分区的更改提要,我想恢复一次,例如每 X 秒轮询一次新更改。 下面的检查点变量保存最后一个响应延续响应。

    private string checkpoint;
    private async Task ReadEvents()
    {
            FeedResponse<dynamic> feedResponse;
            do
            {
                feedResponse = await client.ReadDocumentFeedAsync(commitsLink, new FeedOptions
                {
                    MaxItemCount = this.subscriptionOptions.MaxItemCount,
                    RequestContinuation = checkpoint
                });
                if (feedResponse.ResponseContinuation != null)
                {
                    checkpoint = feedResponse.ResponseContinuation;
                }
               // Code to process docs goes here...
            } while (feedResponse.ResponseContinuation != null);
    }

请注意在检查点周围使用"if"块。 这样做是因为如果我省略这一点,响应延续将设置为 null,这基本上将重新启动轮询周期,因为将请求延续设置为 null 将在更改提要中提取第一组文档。

但是,缺点是每个轮询循环都会重播前一组文档,而不仅仅是任何其他更改。 我可以做些什么来进一步优化它,或者这是更改源 API 的限制?

为了读取更改源,必须使用CreateDocumentChangeFeedQuery(永远不会重置响应延续),而不是ReadDocumentFeed(当没有更多结果时设置为 null)。

有关示例,请参阅 https://learn.microsoft.com/en-us/azure/documentdb/documentdb-change-feed#working-with-the-rest-api-and-sdk。

最新更新