Exemple #1
0
        public WorkerEventLoopGroup(DispatcherEventLoopGroup eventLoopGroup, int nThreads, IThreadFactory threadFactory, IEventExecutorChooserFactory <WorkerEventLoop> chooserFactory, IRejectedExecutionHandler rejectedHandler, TimeSpan breakoutInterval)
        {
            if (eventLoopGroup is null)
            {
                ThrowHelper.ThrowArgumentNullException(ExceptionArgument.eventLoopGroup);
            }
            if (chooserFactory is null)
            {
                ThrowHelper.ThrowArgumentNullException(ExceptionArgument.chooserFactory);
            }
            if ((uint)(nThreads - 1) > SharedConstants.TooBigOrNegative)
            {
                ThrowHelper.ThrowArgumentException_Positive(nThreads, ExceptionArgument.nThreads);
            }

            _dispatcherLoop = eventLoopGroup.Dispatcher;
            PipeName        = _dispatcherLoop.PipeName;

            // Wait until the pipe is listening to connect
            _dispatcherLoop.WaitForLoopRun(StartTimeout);

            nThreads = 0u >= (uint)nThreads ? DefaultEventLoopThreadCount : nThreads;

            _eventLoops = new WorkerEventLoop[nThreads];
            var terminationTasks = new Task[nThreads];

            for (int i = 0; i < nThreads; i++)
            {
                WorkerEventLoop eventLoop = null;
                bool            success   = false;
                try
                {
                    eventLoop = new WorkerEventLoop(this, threadFactory, rejectedHandler, breakoutInterval);
                    success   = eventLoop.ConnectTask.Wait(StartTimeout);
                    if (!success)
                    {
                        ThrowHelper.ThrowTimeoutException(PipeName);
                    }
                }
                catch (Exception ex)
                {
                    ThrowHelper.ThrowInvalidOperationException_CreateChild(ex);
                }
                finally
                {
                    if (!success)
                    {
                        Task.WhenAll(_eventLoops.Take(i).Select(loop => loop.ShutdownGracefullyAsync())).Wait();
                    }
                }

                _eventLoops[i]      = eventLoop;
                terminationTasks[i] = eventLoop.TerminationCompletion;
            }

            _chooser = chooserFactory.NewChooser(_eventLoops);

            TerminationCompletion = Task.WhenAll(terminationTasks);
        }
Exemple #2
0
        public WorkerEventLoopGroup(DispatcherEventLoopGroup eventLoopGroup, int eventLoopCount)
        {
            if (eventLoopGroup is null)
            {
                ThrowHelper.ThrowArgumentNullException(ExceptionArgument.eventLoopGroup);
            }

            _dispatcherLoop = eventLoopGroup.Dispatcher;
            PipeName        = _dispatcherLoop.PipeName;

            // Wait until the pipe is listening to connect
            _dispatcherLoop.WaitForLoopRun(StartTimeout);

            _eventLoops = new WorkerEventLoop[eventLoopCount];
            var terminationTasks = new Task[eventLoopCount];

            for (int i = 0; i < eventLoopCount; i++)
            {
                WorkerEventLoop eventLoop = null;
                bool            success   = false;
                try
                {
                    eventLoop = new WorkerEventLoop(this);
                    success   = eventLoop.ConnectTask.Wait(StartTimeout);
                    if (!success)
                    {
                        ThrowHelper.ThrowTimeoutException(PipeName);
                    }
                }
                catch (Exception ex)
                {
                    ThrowHelper.ThrowInvalidOperationException_CreateChild(ex);
                }
                finally
                {
                    if (!success)
                    {
                        Task.WhenAll(_eventLoops.Take(i).Select(loop => loop.ShutdownGracefullyAsync())).Wait();
                    }
                }

                _eventLoops[i]      = eventLoop;
                terminationTasks[i] = eventLoop.TerminationCompletion;
            }

            TerminationCompletion = Task.WhenAll(terminationTasks);
        }
Exemple #3
0
        public WorkerEventLoopGroup(DispatcherEventLoopGroup eventLoopGroup, int eventLoopCount)
        {
            Contract.Requires(eventLoopGroup != null);

            this.dispatcherLoop = eventLoopGroup.Dispatcher;
            this.PipeName       = this.dispatcherLoop.PipeName;

            // Wait until the pipe is listening to connect
            this.dispatcherLoop.WaitForLoopRun(StartTimeout);

            this.eventLoops = new WorkerEventLoop[eventLoopCount];
            var terminationTasks = new Task[eventLoopCount];

            for (int i = 0; i < eventLoopCount; i++)
            {
                WorkerEventLoop eventLoop;
                bool            success = false;
                try
                {
                    eventLoop = new WorkerEventLoop(this);
                    success   = eventLoop.ConnectTask.Wait(StartTimeout);
                    if (!success)
                    {
                        throw new TimeoutException($"Connect to dispatcher pipe {this.PipeName} timed out.");
                    }
                }
                catch (Exception ex)
                {
                    throw new InvalidOperationException($"Failed to create a child {nameof(WorkerEventLoop)}.", ex.Unwrap());
                }
                finally
                {
                    if (!success)
                    {
                        Task.WhenAll(this.eventLoops.Take(i).Select(loop => loop.ShutdownGracefullyAsync())).Wait();
                    }
                }

                this.eventLoops[i]  = eventLoop;
                terminationTasks[i] = eventLoop.TerminationCompletion;
            }

            this.TerminationCompletion = Task.WhenAll(terminationTasks);
        }
        public WorkerEventLoopGroup(DispatcherEventLoop dispatcherLoop, int eventLoopCount)
        {
            Contract.Requires(dispatcherLoop != null);

            this.dispatcherLoop = dispatcherLoop;
            this.dispatcherLoop.PipeStartTask.Wait(StartTimeout);
            this.PipeName = this.dispatcherLoop.PipeName;

            this.eventLoops = new IEventLoop[eventLoopCount];
            var terminationTasks = new Task[eventLoopCount];

            for (int i = 0; i < eventLoopCount; i++)
            {
                WorkerEventLoop eventLoop;
                bool            success = false;
                try
                {
                    eventLoop = new WorkerEventLoop(this);
                    eventLoop.StartAsync().Wait(StartTimeout);

                    success = true;
                }
                catch (Exception ex)
                {
                    throw new InvalidOperationException("failed to create a child event loop.", ex);
                }
                finally
                {
                    if (!success)
                    {
                        Task.WhenAll(this.eventLoops.Take(i).Select(loop => loop.ShutdownGracefullyAsync()))
                        .Wait();
                    }
                }

                this.eventLoops[i]  = eventLoop;
                terminationTasks[i] = eventLoop.TerminationCompletion;
            }

            this.TerminationCompletion = Task.WhenAll(terminationTasks);
        }
 public DispatcherEventLoopGroup()
 {
     _dispatcherEventLoop = new DispatcherEventLoop(this);
 }
 public WorkerEventLoopGroup(DispatcherEventLoop dispatcherLoop)
     : this(dispatcherLoop, DefaultEventLoopThreadCount)
 {
 }