static ConstructContext()
        {
            _pool = new Pool <ConstructContextImpl>(() =>
            {
                return(new ConstructContextImpl());
            }, (ctx, phase) =>
            {
                if (phase == PoolItemPhase.Returning)
                {
                    ctx.Clear();
                }
                return(true);
            }, new PoolConfig()
            {
                MaxRemainTime = 300 //5分钟之内未被使用,就移除
            });


            _itemPool = new PoolWrapper <Item>(() =>
            {
                return(new Item());
            }, (item, phase) =>
            {
                if (phase == PoolItemPhase.Returning)
                {
                    item.Clear();
                }
                return(true);
            }, new PoolConfig()
            {
                MaxRemainTime = 300 //5分钟之内未被使用,就移除
            });
        }
Exemplo n.º 2
0
        public void OverstepRemainTimeWrapper()
        {
            PoolWrapper <object> pool = new PoolWrapper <object>(() => { return(new object()); },
                                                                 (obj, phase) => { return(true); },
                                                                 new PoolConfig {
                MaxRemainTime = 3
            });

            var item0 = pool.Borrow();

            Thread.Sleep(1000); //使用一秒
            pool.Return(item0);

            Thread.Sleep(1000);//在池中停留一秒
            var item1 = pool.Borrow();

            Assert.AreEqual(item1, item0);
            pool.Return(item1);

            //在池中停留两秒
            Thread.Sleep(2000);
            var item2 = pool.Borrow();

            Assert.AreEqual(item2, item1);
            pool.Return(item2);

            //在池中停留4秒
            Thread.Sleep(4000);
            var item3 = pool.Borrow();

            Assert.AreNotEqual(item3, item2); //超时,所以新建,并销毁原有的项
            pool.Return(item3);
        }
Exemplo n.º 3
0
        public void TenItemByWrapper()
        {
            List <object>        array = new List <object>();
            PoolWrapper <object> pool  = new PoolWrapper <object>(() => { return(new object()); },
                                                                  (obj, phase) => { return(true); },
                                                                  new PoolConfig {
                LoanCapacity = 10
            });                                                                        //最多有10个可以借出
            int maxBorrowedCount = 0;
            int maxWaiterCount   = 0;

            //18个异步任务,访问池
            Parallel.For(0, 100, (i) =>
            {
                var obj = pool.Borrow();
                Thread.Sleep(5);
                lock (this) //List<object>不是线程安全的
                {
                    array.Add(obj);
                }
                maxBorrowedCount = pool.BorrowedCount > maxBorrowedCount ? pool.BorrowedCount : maxBorrowedCount;
                maxWaiterCount   = pool.WaiterCount > maxWaiterCount ? pool.WaiterCount : maxWaiterCount;
                pool.Return(obj);
            });
            Assert.AreEqual(100, array.Count);     //执行了100次操作
            Assert.IsTrue(maxBorrowedCount <= 10); //最多同时进行10个操作
            Assert.IsTrue(maxWaiterCount < 90);    //最多有90个操作等待池
        }
Exemplo n.º 4
0
 static ScheduledAction()
 {
     _pool = new PoolWrapper <ScheduledAction>(() =>
     {
         return(new ScheduledAction());
     }, (sa, phase) =>
     {
         return(true);
     }, new PoolConfig()
     {
         MaxRemainTime = 300 //5分钟之内未被使用,就移除
     });
 }
Exemplo n.º 5
0
 static Symbiosis()
 {
     _pool = new PoolWrapper <Symbiosis>(() =>
     {
         return(new Symbiosis());
     }, (sym, phase) =>
     {
         if (phase == PoolItemPhase.Returning)
         {
             sym.Clear();
         }
         return(true);
     }, new PoolConfig()
     {
         MaxRemainTime = 300 //5分钟之内未被使用,就移除
     });
 }
Exemplo n.º 6
0
        static RtpCallManager()
        {
            _pool = new PoolWrapper <CallIdentity>(() =>
            {
                return(new CallIdentity());
            },
                                                   (obj, phase) =>
            {
                if (phase == PoolItemPhase.Returning)
                {
                    obj.Reset();
                }
                return(true);
            }, new PoolConfig()
            {
                MaxRemainTime = 300 //闲置时间300秒
            });

            ClientEvents.Disconnected += OnDisconnected;
        }