diff --git a/src/Temporalio/Client/TemporalClient.Workflow.cs b/src/Temporalio/Client/TemporalClient.Workflow.cs index 95eb5055..507503e4 100644 --- a/src/Temporalio/Client/TemporalClient.Workflow.cs +++ b/src/Temporalio/Client/TemporalClient.Workflow.cs @@ -458,6 +458,7 @@ private async IAsyncEnumerable ListWorkflowsInternalAsync( // Need to combine cancellation token var rpcOptsAndCancelSource = DefaultRetryOptions(input.Options?.Rpc). WithAdditionalCancellationToken(cancellationToken); + var yielded = 0; try { var req = new ListWorkflowExecutionsRequest() @@ -472,6 +473,11 @@ private async IAsyncEnumerable ListWorkflowsInternalAsync( req, rpcOptsAndCancelSource.Item1).ConfigureAwait(false); foreach (var exec in resp.Executions) { + if (input.Options != null && input.Options.Limit > 0 && + yielded++ >= input.Options.Limit) + { + yield break; + } yield return new(exec, Client.Options.DataConverter); } req.NextPageToken = resp.NextPageToken; diff --git a/src/Temporalio/Client/WorkflowListOptions.cs b/src/Temporalio/Client/WorkflowListOptions.cs index 04d9e844..1475f98e 100644 --- a/src/Temporalio/Client/WorkflowListOptions.cs +++ b/src/Temporalio/Client/WorkflowListOptions.cs @@ -13,6 +13,11 @@ public class WorkflowListOptions : ICloneable /// public RpcOptions? Rpc { get; set; } + /// + /// Gets or sets the maximum number of workflows to return. A zero value means no limit. + /// + public int Limit { get; set; } + /// /// Create a shallow copy of these options. /// @@ -28,4 +33,4 @@ public virtual object Clone() } } } -#endif \ No newline at end of file +#endif diff --git a/tests/Temporalio.Tests/Client/TemporalClientWorkflowTests.cs b/tests/Temporalio.Tests/Client/TemporalClientWorkflowTests.cs index 638a8368..0c98160a 100644 --- a/tests/Temporalio.Tests/Client/TemporalClientWorkflowTests.cs +++ b/tests/Temporalio.Tests/Client/TemporalClientWorkflowTests.cs @@ -272,6 +272,15 @@ await Client.ExecuteWorkflowAsync( actualResults.Add(result); } Assert.Equal(expectedResults, actualResults); + + // Verify limit option works + var limitedResults = 0; + await foreach (var wf in Client.ListWorkflowsAsync( + $"WorkflowId = '{workflowId}'", new() { Limit = 3 })) + { + limitedResults++; + } + Assert.Equal(3, limitedResults); } [Workflow]