示例#1
0
        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);
        }
示例#2
0
        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);
        }
示例#3
0
        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));
        }
示例#4
0
        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();
                }
            }
        }
示例#5
0
        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);
        }
示例#6
0
 public ActorPath TempPath()
 {
     return(_delegate.TempPath());
 }
 public ActorPath TempPath()
 {
     return(_localActorRefProvider.TempPath());
 }