Skip to content

Commit 66436bf

Browse files
authored
Add limit to list workflows (#377)
1 parent 15a3142 commit 66436bf

File tree

3 files changed

+21
-1
lines changed

3 files changed

+21
-1
lines changed

src/Temporalio/Client/TemporalClient.Workflow.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -458,6 +458,7 @@ private async IAsyncEnumerable<WorkflowExecution> ListWorkflowsInternalAsync(
458458
// Need to combine cancellation token
459459
var rpcOptsAndCancelSource = DefaultRetryOptions(input.Options?.Rpc).
460460
WithAdditionalCancellationToken(cancellationToken);
461+
var yielded = 0;
461462
try
462463
{
463464
var req = new ListWorkflowExecutionsRequest()
@@ -472,6 +473,11 @@ private async IAsyncEnumerable<WorkflowExecution> ListWorkflowsInternalAsync(
472473
req, rpcOptsAndCancelSource.Item1).ConfigureAwait(false);
473474
foreach (var exec in resp.Executions)
474475
{
476+
if (input.Options != null && input.Options.Limit > 0 &&
477+
yielded++ >= input.Options.Limit)
478+
{
479+
yield break;
480+
}
475481
yield return new(exec, Client.Options.DataConverter);
476482
}
477483
req.NextPageToken = resp.NextPageToken;

src/Temporalio/Client/WorkflowListOptions.cs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,11 @@ public class WorkflowListOptions : ICloneable
1313
/// </summary>
1414
public RpcOptions? Rpc { get; set; }
1515

16+
/// <summary>
17+
/// Gets or sets the maximum number of workflows to return. A zero value means no limit.
18+
/// </summary>
19+
public int Limit { get; set; }
20+
1621
/// <summary>
1722
/// Create a shallow copy of these options.
1823
/// </summary>
@@ -28,4 +33,4 @@ public virtual object Clone()
2833
}
2934
}
3035
}
31-
#endif
36+
#endif

tests/Temporalio.Tests/Client/TemporalClientWorkflowTests.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -272,6 +272,15 @@ await Client.ExecuteWorkflowAsync(
272272
actualResults.Add(result);
273273
}
274274
Assert.Equal(expectedResults, actualResults);
275+
276+
// Verify limit option works
277+
var limitedResults = 0;
278+
await foreach (var wf in Client.ListWorkflowsAsync(
279+
$"WorkflowId = '{workflowId}'", new() { Limit = 3 }))
280+
{
281+
limitedResults++;
282+
}
283+
Assert.Equal(3, limitedResults);
275284
}
276285

277286
[Workflow]

0 commit comments

Comments
 (0)