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

DocumentDb optimizing the resume of a non-partitioned change feed

我正在创建一个未分区的更改源,我想恢复它,例如每 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" 块。这样做是因为如果我不这样做,responseContinuation 将设置为 null,这将基本上重新启动轮询周期,因为将请求继续设置为 null 将在更改提要中提取第一组文档。

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

为了读取更改提要,您必须使用 CreateDocumentChangeFeedQuery(永远不会重置 ResponseContinuation),而不是 ReadDocumentFeed(当没有更多结果时设置为 null)。

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