Skip to content

Commit

Permalink
Add limit to list workflows (temporalio#377)
Browse files Browse the repository at this point in the history
  • Loading branch information
Sushisource authored Dec 6, 2024
1 parent 15a3142 commit 66436bf
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 1 deletion.
6 changes: 6 additions & 0 deletions src/Temporalio/Client/TemporalClient.Workflow.cs
Original file line number Diff line number Diff line change
Expand Up @@ -458,6 +458,7 @@ private async IAsyncEnumerable<WorkflowExecution> ListWorkflowsInternalAsync(
// Need to combine cancellation token
var rpcOptsAndCancelSource = DefaultRetryOptions(input.Options?.Rpc).
WithAdditionalCancellationToken(cancellationToken);
var yielded = 0;
try
{
var req = new ListWorkflowExecutionsRequest()
Expand All @@ -472,6 +473,11 @@ private async IAsyncEnumerable<WorkflowExecution> 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;
Expand Down
7 changes: 6 additions & 1 deletion src/Temporalio/Client/WorkflowListOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,11 @@ public class WorkflowListOptions : ICloneable
/// </summary>
public RpcOptions? Rpc { get; set; }

/// <summary>
/// Gets or sets the maximum number of workflows to return. A zero value means no limit.
/// </summary>
public int Limit { get; set; }

/// <summary>
/// Create a shallow copy of these options.
/// </summary>
Expand All @@ -28,4 +33,4 @@ public virtual object Clone()
}
}
}
#endif
#endif
9 changes: 9 additions & 0 deletions tests/Temporalio.Tests/Client/TemporalClientWorkflowTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down

0 comments on commit 66436bf

Please sign in to comment.