예제 #1
0
        public Task ShutdownAsync()
        {
            if (_timer != null)
            {
                // .NET-specific
                ExecutorService.Cancel(_timer);
            }
            var tcsShutdown = new TaskCompletionSource <object>();

            lock (_lock)
            {
                _shutdown = true;
                int max = _runningTasks.Count;
                if (max == 0)
                {
                    tcsShutdown.SetResult(null);
                    return(tcsShutdown.Task);
                }
                var counter = new VolatileInteger(0);
                foreach (var task in _runningTasks.Keys)
                {
                    task.ContinueWith(t =>
                    {
                        if (counter.IncrementAndGet() == max)
                        {
                            tcsShutdown.SetResult(null); // complete
                        }
                    });
                }
            }

            return(tcsShutdown.Task);
        }
예제 #2
0
        private void Run(object state)
        {
            //Console.WriteLine("Maintenance Thread {0}: Maintenance triggered on peer {1}.", Thread.CurrentThread.ManagedThreadId, _peer.PeerId);
            lock (_lock)
            {
                // make sure we only have 5 pings in parallel
                if (_shutdown || Counter.Get() > MaxPing)
                {
                    return;
                }
                foreach (var maintainable in _maintainables)
                {
                    var peerStatistic = maintainable.NextForMaintenance(_runningTasks.Values);
                    if (peerStatistic == null)
                    {
                        return;
                    }
                    var task = _peer.Ping().SetPeerAddress(peerStatistic.PeerAddress).Start();
                    Logger.Debug("Maintenance ping from {0} to {1}.", _peer.PeerAddress, peerStatistic.PeerAddress);

                    _peer.NotifyAutomaticFutures(task);
                    _runningTasks.Add(task, peerStatistic.PeerAddress);
                    Counter.IncrementAndGet();
                    task.ContinueWith(t =>
                    {
                        lock (_lock)
                        {
                            _runningTasks.Remove(task);
                            Counter.Decrement();
                        }
                    });
                }
            }
        }
예제 #3
0
        /// <summary>
        /// Shuts down all the channel creators.
        /// </summary>
        /// <returns></returns>
        public Task ShutdownAsync()
        {
            _readWriteLock.EnterWriteLock();
            try
            {
                if (_shutdown)
                {
                    _tcsReservationDone.SetException(new TaskFailedException("Already shutting down."));
                    return(_tcsReservationDone.Task);
                }
                _shutdown = true;
            }
            finally
            {
                _readWriteLock.ExitWriteLock();
            }

            // Fast shutdown for those that are in the queue is not required.
            // Let the executor finish since the shutdown-flag is set and the
            // future will be set as well to "shutting down".

            // .NET-specific: queued tasks cannot be aborted, but the flag indicates
            // the shutdown and the result will be set to failed

            // the channel creator doesn't change anymore from here on
            int size = _channelCreators.Count;

            if (size == 0)
            {
                _tcsReservationDone.SetResult(null);
            }
            else
            {
                var completeCounter = new VolatileInteger(0);
                foreach (var channelCreator in _channelCreators)
                {
                    // It's important to set the listener before calling shutdown.
                    channelCreator.ShutdownTask.ContinueWith(delegate
                    {
                        if (completeCounter.IncrementAndGet() == size)
                        {
                            // we can block here
                            _semaphoreUdp.Acquire(_maxPermitsUdp);
                            _semaphoreTcp.Acquire(_maxPermitsTcp);
                            _semaphorePermanentTcp.Acquire(_maxPermitsPermanentTcp);
                            _tcsReservationDone.SetResult(null);
                        }
                    });
                    channelCreator.ShutdownAsync();
                }
            }
            return(_tcsReservationDone.Task);
        }
        public override void ChannelActive(ChannelHandlerContext ctx)
        {
            base.ChannelActive(ctx);
            int current;

            if ((current = _counter.IncrementAndGet()) > _limit)
            {
                ctx.Channel.Close();
                Logger.Warn("Dropped connection because {0} > {1} connections active.", current, _limit);
            }
            // fireChannelActive // TODO needed?
        }
예제 #5
0
        public AttributeKey(string name)
        {
            if (Names == null)
            {
                throw new NullReferenceException("Names");
            }
            if (name == null)
            {
                throw new NullReferenceException("name");
            }

            Names.AddOrUpdate(name, true, delegate
            {
                throw new ArgumentException(String.Format("'{0}' is already in use.", name));
            });

            Id   = NextId.IncrementAndGet();
            Name = name;
        }
예제 #6
0
        /// <summary>
        /// Adds a listener to the response tasks and releases all acquired channels in the channel creator.
        /// </summary>
        /// <param name="channelCreator">The channel creator that will be shutdown and all connections will be closed.</param>
        /// <param name="tasks">The tasks to listen to. If all the tasks finished, then the channel creator is shut down.
        /// If null is provided, then the channel crator is shut down immediately.</param>
        /// <returns>The task that completes when all listeners have completed.</returns>
        public static Task AddReleaseListener(ChannelCreator channelCreator, params Task[] tasks)
        {
            if (tasks == null)
            {
                return(channelCreator.ShutdownAsync());
            }
            var continuationTasks = new List <Task>(tasks.Length);
            int count             = tasks.Count();
            var finished          = new VolatileInteger(0);

            foreach (var task in tasks)
            {
                var contTask = task.ContinueWith(delegate
                {
                    if (finished.IncrementAndGet() == count)
                    {
                        channelCreator.ShutdownAsync();
                    }
                });
                continuationTasks.Add(contTask);
            }
            return(Task.WhenAll(continuationTasks));
        }
 //.NET-specific: use channel activation instead of connection
 public override void ChannelActive(ChannelHandlerContext ctx)
 {
     _counterCurrent.IncrementAndGet();
     _counterTotal.IncrementAndGet();
 }
예제 #8
0
 /// <summary>
 /// Increases the failed counter.
 /// </summary>
 /// <returns>The number of failed checks.</returns>
 public int Failed()
 {
     return(_failed.IncrementAndGet());
 }
예제 #9
0
 /// <summary>
 /// Sets the time when last seen online to now.
 /// </summary>
 /// <returns>The number of successful checks.</returns>
 public int SuccessfullyChecked()
 {
     _lastSeenOnline.Set(Convenient.CurrentTimeMillis());
     _failed.Set(0);
     return(_successfullyChecked.IncrementAndGet());
 }