v7.3.2018.0614   重构高性能资源池,减少GC压力,增加线程池,让异步任务得到平等竞争CPU的机会
大石头 authored at 2018-06-14 17:56:44
1.37 KiB
X
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using NewLife.Remoting;

namespace NewLife.Queue.Broker.Controllers
{
    public class ProducerInfoController : IApi
    {
        public IApiSession Session { get; set; }

        public bool RegisterProducer(string user,string pass)
        {
            var host = Session.GetService<ApiServer>();
            var producerInfoDict = host["ProducerInfoDict"] as ConcurrentDictionary<string, ProducerInfo>;
            if (producerInfoDict == null) return false;
            producerInfoDict.AddOrUpdate(Session.Remote.EndPoint.ToAddress(), key =>
            {
                var producerInfo = new ProducerInfo
                {
                    ProducerId = Session.Remote.EndPoint.ToAddress(),
                    HeartbeatInfo = new ClientHeartbeatInfo(Session) { LastHeartbeatTime = DateTime.Now }
                };
                host.Log.Info("Producer registered, producerId: {0}, connectionId: {1}", Session.Remote.EndPoint.ToAddress(), key);
                return producerInfo;
            }, (key, existingProducerInfo) =>
            {
                existingProducerInfo.HeartbeatInfo.LastHeartbeatTime = DateTime.Now;
                return existingProducerInfo;
            });
            return true;
        }
    }
}