private static Task <object> Ask(ICanTell self, object message, IActorRefProvider provider, TimeSpan?timeout) { var result = new TaskCompletionSource <object>(); CancellationTokenSource timeoutCancellation = null; timeout = timeout ?? provider.Settings.AskTimeout; if (timeout != Timeout.InfiniteTimeSpan && timeout.Value > default(TimeSpan)) { timeoutCancellation = new CancellationTokenSource(); timeoutCancellation.Token.Register(() => result.TrySetCanceled()); timeoutCancellation.CancelAfter(timeout.Value); } //create a new tempcontainer path ActorPath path = provider.TempPath(); //callback to unregister from tempcontainer Action unregister = () => { // cancelling timeout (if any) in order to prevent memory leaks // (a reference to 'result' variable in CancellationToken's callback) timeoutCancellation?.Cancel(); provider.UnregisterTempActor(path); }; var future = new FutureActorRef(result, unregister, path); //The future actor needs to be registered in the temp container provider.RegisterTempActor(future, path); self.Tell(message, future); return(result.Task); }
private static Task <object> Ask(ICanTell self, object message, IActorRefProvider provider, TimeSpan?timeout) { var result = new TaskCompletionSource <object>(); timeout = timeout ?? provider.Settings.AskTimeout; if (timeout != Timeout.InfiniteTimeSpan && timeout.Value > default(TimeSpan)) { var cancellationSource = new CancellationTokenSource(); cancellationSource.Token.Register(() => result.TrySetCanceled()); cancellationSource.CancelAfter(timeout.Value); } //create a new tempcontainer path ActorPath path = provider.TempPath(); //callback to unregister from tempcontainer Action unregister = () => provider.UnregisterTempActor(path); var future = new FutureActorRef(result, unregister, path); //The future actor needs to be registered in the temp container provider.RegisterTempActor(future, path); self.Tell(message, future); return(result.Task); }
public static AskRefs Create(IActorRefProvider provider, TimeSpan timeout) { var tcs = new TaskCompletionSource <object>(); // logic copied from Ask (Akka.Actor -> Futures.cs) if (timeout != System.Threading.Timeout.InfiniteTimeSpan && timeout > default(TimeSpan)) { var cancellationSource = new CancellationTokenSource(); cancellationSource.Token.Register(() => tcs.TrySetCanceled()); cancellationSource.CancelAfter(timeout); } //create a new tempcontainer path ActorPath path = provider.TempPath(); //callback to unregister from tempcontainer Action unregister = () => provider.UnregisterTempActor(path); var future = new FutureActorRef(tcs, unregister, path); //The future actor needs to be registered in the temp container provider.RegisterTempActor(future, path); return(new AskRefs(future, tcs)); }
private static async Task <object> Ask(ICanTell self, Func <IActorRef, object> messageFactory, IActorRefProvider provider, TimeSpan?timeout, CancellationToken cancellationToken) { TaskCompletionSource <object> result = TaskEx.NonBlockingTaskCompletionSource <object>(); CancellationTokenSource timeoutCancellation = null; timeout = timeout ?? provider.Settings.AskTimeout; var ctrList = new List <CancellationTokenRegistration>(2); if (timeout != Timeout.InfiniteTimeSpan && timeout.Value > default(TimeSpan)) { timeoutCancellation = new CancellationTokenSource(); ctrList.Add(timeoutCancellation.Token.Register(() => { result.TrySetException(new AskTimeoutException($"Timeout after {timeout} seconds")); })); timeoutCancellation.CancelAfter(timeout.Value); } if (cancellationToken.CanBeCanceled) { ctrList.Add(cancellationToken.Register(() => result.TrySetCanceled())); } //create a new tempcontainer path ActorPath path = provider.TempPath(); var future = new FutureActorRef(result, () => { }, path, TaskEx.IsRunContinuationsAsynchronouslyAvailable); //The future actor needs to be registered in the temp container provider.RegisterTempActor(future, path); var message = messageFactory(future); self.Tell(message, future); try { return(await result.Task); } finally { //callback to unregister from tempcontainer provider.UnregisterTempActor(path); for (var i = 0; i < ctrList.Count; i++) { ctrList[i].Dispose(); } if (timeoutCancellation != null) { timeoutCancellation.Dispose(); } } }
private static Task <object> Ask(ICanTell self, object message, IActorRefProvider provider, TimeSpan?timeout, CancellationToken cancellationToken) { TaskCompletionSource <object> result; if (isRunContinuationsAsynchronouslyAvailable) { result = new TaskCompletionSource <object>((TaskCreationOptions)RunContinuationsAsynchronously); } else { result = new TaskCompletionSource <object>(); } CancellationTokenSource timeoutCancellation = null; timeout = timeout ?? provider.Settings.AskTimeout; List <CancellationTokenRegistration> ctrList = new List <CancellationTokenRegistration>(2); if (timeout != Timeout.InfiniteTimeSpan && timeout.Value > default(TimeSpan)) { timeoutCancellation = new CancellationTokenSource(); ctrList.Add(timeoutCancellation.Token.Register(() => result.TrySetCanceled())); timeoutCancellation.CancelAfter(timeout.Value); } if (cancellationToken.CanBeCanceled) { ctrList.Add(cancellationToken.Register(() => result.TrySetCanceled())); } //create a new tempcontainer path ActorPath path = provider.TempPath(); //callback to unregister from tempcontainer Action unregister = () => { // cancelling timeout (if any) in order to prevent memory leaks // (a reference to 'result' variable in CancellationToken's callback) if (timeoutCancellation != null) { timeoutCancellation.Cancel(); timeoutCancellation.Dispose(); } for (var i = 0; i < ctrList.Count; i++) { ctrList[i].Dispose(); } provider.UnregisterTempActor(path); }; var future = new FutureActorRef(result, unregister, path, isRunContinuationsAsynchronouslyAvailable); //The future actor needs to be registered in the temp container provider.RegisterTempActor(future, path); self.Tell(message, future); return(result.Task); }
/// <summary> /// TBD /// </summary> /// <typeparam name="T">TBD</typeparam> /// <param name="self">TBD</param> /// <param name="messageFactory">Factory method that creates a message that can encapsulate the 'Sender' IActorRef</param> /// <param name="timeout">TBD</param> /// <param name="cancellationToken">TBD</param> /// <exception cref="ArgumentException"> /// This exception is thrown if the system can't resolve the target provider. /// </exception> /// <returns>TBD</returns> public static Task <T> Ask <T>(this ICanTell self, Func <IActorRef, object> messageFactory, TimeSpan?timeout, CancellationToken cancellationToken) { IActorRefProvider provider = ResolveProvider(self); if (provider == null) { throw new ArgumentException("Unable to resolve the target Provider", nameof(self)); } var result = TaskEx.NonBlockingTaskCompletionSource <T>(); CancellationTokenSource timeoutCancellation = null; timeout = timeout ?? provider.Settings.AskTimeout; CancellationTokenRegistration?ctr1 = null; CancellationTokenRegistration?ctr2 = null; if (timeout != Timeout.InfiniteTimeSpan && timeout.Value > default(TimeSpan)) { timeoutCancellation = new CancellationTokenSource(); ctr1 = timeoutCancellation.Token.Register(() => { result.TrySetException(new AskTimeoutException($"Timeout after {timeout} seconds")); }); timeoutCancellation.CancelAfter(timeout.Value); } if (cancellationToken.CanBeCanceled) { ctr2 = cancellationToken.Register(() => result.TrySetCanceled()); } var future = provider.CreateFutureRef(result); var path = future.Path; //The future actor needs to be unregistered in the temp container _ = result.Task.ContinueWith(t => { provider.UnregisterTempActor(path); ctr1?.Dispose(); ctr2?.Dispose(); timeoutCancellation?.Dispose(); }, TaskContinuationOptions.ExecuteSynchronously); //The future actor needs to be registered in the temp container provider.RegisterTempActor(future, path); var message = messageFactory(future); self.Tell(message, future); return(result.Task); }
public void UnregisterTempActor(ActorPath path) { _delegate.UnregisterTempActor(path); }
public void UnregisterTempActor(ActorPath path) { _localActorRefProvider.UnregisterTempActor(path); }