From 655e4f337c58beaa7bbb5cbf4cfa791c49f1539f Mon Sep 17 00:00:00 2001 From: Gregorius Soedharmo Date: Fri, 25 Feb 2022 03:24:09 +0700 Subject: [PATCH] Make PipeTo ConfigureAwait() optional (#5684) * Remove ConfigureAwait() from PipeTo() * Remove ConfigureAwait() from PipeTo() * Add ConfigureAwait back to PipeTo, make it configurable instead * Update API Approval list * Add function overload for backward compatibility * Update API Approval list Co-authored-by: Gregorius Soedharmo --- .../CoreAPISpec.ApproveCore.approved.txt | 2 + src/core/Akka/Actor/PipeToSupport.cs | 95 +++++++++++++++---- 2 files changed, 80 insertions(+), 17 deletions(-) diff --git a/src/core/Akka.API.Tests/CoreAPISpec.ApproveCore.approved.txt b/src/core/Akka.API.Tests/CoreAPISpec.ApproveCore.approved.txt index c06404e477b..c4798d4ee33 100644 --- a/src/core/Akka.API.Tests/CoreAPISpec.ApproveCore.approved.txt +++ b/src/core/Akka.API.Tests/CoreAPISpec.ApproveCore.approved.txt @@ -1387,7 +1387,9 @@ namespace Akka.Actor public class static PipeToSupport { public static System.Threading.Tasks.Task PipeTo(this System.Threading.Tasks.Task taskToPipe, Akka.Actor.ICanTell recipient, Akka.Actor.IActorRef sender = null, System.Func success = null, System.Func failure = null) { } + public static System.Threading.Tasks.Task PipeTo(this System.Threading.Tasks.Task taskToPipe, Akka.Actor.ICanTell recipient, bool useConfigureAwait, Akka.Actor.IActorRef sender = null, System.Func success = null, System.Func failure = null) { } public static System.Threading.Tasks.Task PipeTo(this System.Threading.Tasks.Task taskToPipe, Akka.Actor.ICanTell recipient, Akka.Actor.IActorRef sender = null, System.Func success = null, System.Func failure = null) { } + public static System.Threading.Tasks.Task PipeTo(this System.Threading.Tasks.Task taskToPipe, Akka.Actor.ICanTell recipient, bool useConfigureAwait, Akka.Actor.IActorRef sender = null, System.Func success = null, System.Func failure = null) { } } public sealed class PoisonPill : Akka.Actor.IAutoReceivedMessage, Akka.Actor.IPossiblyHarmful, Akka.Event.IDeadLetterSuppression { diff --git a/src/core/Akka/Actor/PipeToSupport.cs b/src/core/Akka/Actor/PipeToSupport.cs index a78a95c5d84..6305ccb51d9 100644 --- a/src/core/Akka/Actor/PipeToSupport.cs +++ b/src/core/Akka/Actor/PipeToSupport.cs @@ -21,20 +21,49 @@ public static class PipeToSupport /// Pipes the output of a Task directly to the 's mailbox once /// the task completes /// - /// TBD - /// TBD - /// TBD - /// TBD - /// TBD - /// TBD + /// The type of result of the Task + /// The Task that result needs to be piped to an actor + /// The actor that will receive the Task result + /// The IActorRef that will be used as the sender of the result. Defaults to + /// A callback function that will be called on Task success. Defaults to null for no callback + /// A callback function that will be called on Task failure. Defaults to null for no callback /// A detached task - public static async Task PipeTo(this Task taskToPipe, ICanTell recipient, IActorRef sender = null, Func success = null, Func failure = null) + public static Task PipeTo( + this Task taskToPipe, + ICanTell recipient, + IActorRef sender = null, + Func success = null, + Func failure = null) + => PipeTo(taskToPipe, recipient, true, sender, success, failure); + + /// + /// Pipes the output of a Task directly to the 's mailbox once + /// the task completes + /// + /// The type of result of the Task + /// The Task that result needs to be piped to an actor + /// The actor that will receive the Task result + /// The IActorRef that will be used as the sender of the result. Defaults to + /// A callback function that will be called on Task success. Defaults to null for no callback + /// A callback function that will be called on Task failure. Defaults to null for no callback + /// If set to true, taskToPipe will be awaited using ConfigureAwait(false) + /// A detached task + public static async Task PipeTo( + this Task taskToPipe, + ICanTell recipient, + bool useConfigureAwait, + IActorRef sender = null, + Func success = null, + Func failure = null) { sender = sender ?? ActorRefs.NoSender; try { - var result = await taskToPipe.ConfigureAwait(false); + var result = useConfigureAwait + ? await taskToPipe.ConfigureAwait(false) + : await taskToPipe; + recipient.Tell(success != null ? success(result) : result, sender); @@ -51,19 +80,51 @@ public static async Task PipeTo(this Task taskToPipe, ICanTell recipient, /// Pipes the output of a Task directly to the 's mailbox once /// the task completes. As this task has no result, only exceptions will be piped to the /// - /// TBD - /// TBD - /// TBD - /// TBD - /// TBD - /// TBD - public static async Task PipeTo(this Task taskToPipe, ICanTell recipient, IActorRef sender = null, Func success = null, Func failure = null) + /// The Task that result needs to be piped to an actor + /// The actor that will receive the Task result + /// The IActorRef that will be used as the sender of the result. Defaults to + /// A callback function that will be called on Task success. Defaults to null for no callback + /// A callback function that will be called on Task failure. Defaults to null for no callback + /// A detached task + public static Task PipeTo( + this Task taskToPipe, + ICanTell recipient, + IActorRef sender = null, + Func success = null, + Func failure = null) + => PipeTo(taskToPipe, recipient, true, sender, success, failure); + + /// + /// Pipes the output of a Task directly to the 's mailbox once + /// the task completes. As this task has no result, only exceptions will be piped to the + /// + /// The Task that result needs to be piped to an actor + /// The actor that will receive the Task result + /// The IActorRef that will be used as the sender of the result. Defaults to + /// A callback function that will be called on Task success. Defaults to null for no callback + /// A callback function that will be called on Task failure. Defaults to null for no callback + /// If set to true, taskToPipe will be awaited using ConfigureAwait(false) + /// A detached task + public static async Task PipeTo( + this Task taskToPipe, + ICanTell recipient, + bool useConfigureAwait, + IActorRef sender = null, + Func success = null, + Func failure = null) { sender = sender ?? ActorRefs.NoSender; try - { - await taskToPipe.ConfigureAwait(false); + { + if (useConfigureAwait) + { + await taskToPipe.ConfigureAwait(false); + } + else + { + await taskToPipe; + } if (success != null) {