dotnet / dotnet/orleans

Proposed additions to GrainCancellationTokenSource

Open
#7,977 11 comments 9 reactions 0 assignees View on GitHub
area-grains
Dominant language
C#
Stars
10.9k
Forks
2.1k
Avg merge
14h 42m
Merged PRs (30d)
354

Description

It's good practice to make grain methods cancelable by passing a `GrainCancellationToken` to each grain method. However, if we are calling a grain method via an Orleans client, then we have a `CancellationToken`, not a `GrainCancellationToken`.

Ideally, grain methods would just accept a `CancellationToken` rather than a `GrainCancellationToken`, which would make grains more "POCO". However, in the absence of that, either or both of the following two additions would be helpful:
1. Add a `GrainCancellationTokenSource.CreateLinkedTokenSource(CancellationToken)` method like what exists on the `CancellationTokenSource` class. This would allow us to more easily create a `GrainCancellationTokenSource` linked to an existing `CancellationToken`).
2. Add an implicit converter from a `CancellationToken` to a `GrainCancellationToken` so we can pass an existing `CancellationToken` directly to a grain method accepting a `GrainCancellationToken`.

In the absence of either of the above, we are forced to write code like below.

```csharp
using var cts = new GrainCancellationTokenSource();
using (context.CancellationToken.Register(
cts => ((GrainCancellationTokenSource)cts!).Cancel(), cts))
{
await grain.Method(..., cts.Token);
}
```

Contributor guide

Open the contributing guide

Research direction

Start with GrainCancellationTokenSource and its Token and Cancel usage, then compare the requested overload and conversion with CancellationTokenSource behavior. Review the existing 11-comment discussion before choosing whether to pursue either proposal; done means a client CancellationToken can reach a grain method without the manual registration shown.

Written by the indexing model from the issue text.

Assessment

Tech stack
csharp
Domain
distributed-systems
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.