Skip to content

Instantly share code, notes, and snippets.

@jdom
Last active August 29, 2015 14:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jdom/6597e183eabd3251f4bd to your computer and use it in GitHub Desktop.
Save jdom/6597e183eabd3251f4bd to your computer and use it in GitHub Desktop.
Callback in a new Orleans non-reentrant request (suggestion)
public class MyCustomGrain : IMyCustomGrain // note that this grain is NOT reentrant
{
public async Task<int> MyPublicGrainMethod()
{
// do some async work in the "1st request"
await this.SomethingThatNeedsToRunDuringTheGrainCall();
// now start the async call that I don't want to await during the grain call, but I want the continuation to run in a full non-reentrant request.
var myIoTask = someClient.ExecuteIoCallAsync();
myIoTask.ContinueNonReentrant(this.MyCustomContinuation);
// returning a value just to show that MyPublicGrainMethod and MyCustomContinuation method signatures do not need to match.
return 42;
}
private async Task MyCustomContinuation(Task<MyCustomResponse> previousTask)
{
var response = await previousTask;
// use response here in a "2nd" Orleans request. No other request can interleave at this point, even if this method yields, and this
// is not interleaving any normal grain call requests either.
// Also, this executes not as soon as myIoTask completed. Instead it got enqueued and processed normally as a new independent request,
// as if this was a normal grain call, even if it's a private method not exposed to outsiders via the public grain interface.
// by the way, you cannot return a Task<T> with a result here, as nobody would be expecting a response from this request.
// The original grain call ("1st request") was already completed long time ago and the result sent back to the caller.
}
private Task SomethingThatNeedsToRunDuringTheGrainCall()
{
// doesn't matter what goes here... it will be executed before the grain call ends
}
}
// These are some of the method signatures that I'd see Orleans implementing:
public static class OrleansSchedulerExtensions
{
public static void ContinueNonReentrant(this Task previousTask, Func<Task> nonReentrantContinuation);
public static void ContinueNonReentrant(this Task previousTask, Func<Task, Task> nonReentrantContinuation);
public static void ContinueNonReentrant<TResult>(this Task<TResult> previousTask, Func<Task<TResult>, Task> nonReentrantContinuation);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment