aws / aws/aws-sdk-net

CancellationToken support for Paginators

Open
#2,553 1 comment 0 reactions 0 assignees View on GitHub
feature-request module/sdk-core p2 queued smithy
Dominant language
C#
Stars
140
Forks
891
Avg merge
21h 51m
Merged PRs (30d)
10

Description

### Describe the feature

Add support for CancellationTokens with [Paginators](https://docs.aws.amazon.com/sdk-for-net/v3/developer-guide/paginators.html) so that paginated requests can be canceled.

### Use Case

I'm building a CLI that uses CancellationTokens to cancel requests when the user presses Ctrl-C (SIGINT). I'd like to be able to use the AWS SDK's [Paginators](https://docs.aws.amazon.com/sdk-for-net/v3/developer-guide/paginators.html) feature in my CLI and still allow users to cancel requests, but it doesn't look like there is an easy way to provide a CancellationToken for a request with Paginators.

### Proposed Solution

In the [Paginators example](https://docs.aws.amazon.com/sdk-for-net/v3/developer-guide/paginators.html#paginators-example), you can find the following example code:

```csharp
var paginatorForLogGroups = cwClient.Paginators.DescribeLogGroups(new DescribeLogGroupsRequest());
await foreach(var logGroup in paginatorForLogGroups.LogGroups)
{
Console.WriteLine(logGroup.LogGroupName);
}
```

Ideally we would simply be able to pass in a cancellation token to the request like the existing [DescribeLogGroupsAsync(CancellationToken)](https://docs.aws.amazon.com/sdkfornet/v3/apidocs/items/CloudWatchLogs/MICloudWatchLogsDescribeLogGroupsAsyncCancellationToken.html) and [DescribeLogGroupsAsync(DescribeLogGroupsRequest, CancellationToken)](https://docs.aws.amazon.com/sdkfornet/v3/apidocs/items/CloudWatchLogs/MICloudWatchLogsDescribeLogGroupsAsyncDescribeLogGroupsRequestCancellationToken.html) functions. Something like:

```csharp
var paginatorForLogGroups = cwClient.Paginators.DescribeLogGroups(new DescribeLogGroupsRequest(), cancellationToken);
await foreach(var logGroup in paginatorForLogGroups.LogGroups)
{
Console.WriteLine(logGroup.LogGroupName);
}
```

### Other Information

I'm certainly no C# expert, but I thing the cancellation token would need to both be passed through to the http request being made by the AWS SDK and it would need need to be configured on the [IAsyncEnumerable Interface](https://learn.microsoft.com/en-us/dotnet/api/system.collections.generic.iasyncenumerable-1), perhaps using the [WithCancellation extension method](https://learn.microsoft.com/en-us/dotnet/api/system.threading.tasks.taskasyncenumerableextensions.withcancellation).

### Acknowledgements

- [ ] I may be able to implement this feature request
- [ ] This feature might incur a breaking change

### AWS .NET SDK and/or Package version used

Any of the latest AWS SDK for .NET Version 3 clients that support Paginators

### Targeted .NET Platform

.NET Core 3.1, .NET 6, .NET 7

### Operating System and version

Any

Contributor guide

Open the contributing guide

Research direction

Start with the Paginators.DescribeLogGroups entry point and compare the existing DescribeLogGroupsAsync(CancellationToken) and DescribeLogGroupsAsync(DescribeLogGroupsRequest, CancellationToken) overloads. Check how IAsyncEnumerable and WithCancellation apply to the paginator example, and consider the .NET Core 3.1, .NET 6, and .NET 7 targets. Done means paginated requests can receive and honor a CancellationToken.

Written by the indexing model from the issue text.

Assessment

Tech stack
aws, csharp
Domain
api, backend-api-design
Issue type
Feature
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.