Exemple #1
0
        protected virtual IThreadPool NewThreadPool(int queueCapacity)
        {
            IBuilder Builder = ThreadPools.NewThreadPoolConfigBuilder();

            Builder.SetMinSize(_minSize).SetMaxSize(_maxSize).SetQueueCapacity(queueCapacity);
            return(ThreadPools.NewThreadPool(Builder.Build()));
        }
        protected override void Init()
        {
            base.Init();

            _taskScheduler = new Thread(AutoCheck)
            {
                IsBackground = true
            };
            _taskScheduler.Start();

            _scaleOutTask = () =>
            {
                try
                {
                    TryAddNewEntry(Config.ScaleFactor - 1);
                    _logger.Info("scaleOut success");
                }
                catch (Exception ex)
                {
                    _logger.Error(ex, "scaleOut failed");
                }
                finally
                {
                    Interlocked.CompareExchange(ref _scalingOut, 1, 0);
                }
            };

            ThreadPool.IBuilder builder = ThreadPools.NewThreadPoolConfigBuilder();
            builder.SetMinSize(1).SetMaxSize(1);
            _taskExecutor = ThreadPools.NewThreadPool(builder.Build());
        }
Exemple #3
0
        internal static IHystrixThreadPool GetInstance(IHystrixThreadPoolKey threadPoolKey, IHystrixThreadPoolOptions propertiesBuilder)
        {
            // get the key to use instead of using the object itself so that if people forget to implement equals/hashcode things will still work
            var key = threadPoolKey.Name;

            // if we get here this is the first time so we need to initialize
            return(ThreadPools.GetOrAddEx(key, (k) => new HystrixThreadPoolDefault(threadPoolKey, propertiesBuilder)));
        }
Exemple #4
0
        private static void ThreadPoolExample()
        {
            var threadPool = new ThreadPools();

            threadPool.Run();

            Console.ReadKey();
        }
        public void Add(EoiThread thread)
        {
            lock (ThreadPools)
            {
                if (!ThreadPools.ContainsKey(thread.ThreadPool))
                {
                    ThreadPools.Add(thread.ThreadPool, new EoiThreadPool());
                }

                ThreadPools[thread.ThreadPool].WaitingThreads.AddLast(thread);
            }
        }
 protected virtual IThreadPool NewThreadPool(long checkInterval, long maxIdleTime, long ttl, int queueCapacity)
 {
     ThreadPool.AutoScale.IBuilder builder = ThreadPools.NewAutoScaleThreadPoolConfigBuilder();
     builder.SetMinSize(_minSize).SetMaxSize(_maxSize).SetScaleFactor(5).SetCheckInterval(checkInterval)
     .SetMaxIdleTime(maxIdleTime).SetQueueCapacity(queueCapacity);
     _objectTtl = ttl;
     try
     {
         return(new TestAutoScaleThreadPool(builder.Build()));
     }
     finally
     {
         _objectTtl = long.MaxValue;
     }
 }
        protected override void Test(IQuantileEstimator <int> quantileEstimator, List <Double> quantiles, int count, int upperBound,
                                     double errorRate, Func <int, int, List <int> > dataProvider)
        {
            int        maxError = (int)(upperBound * errorRate);
            List <int> items    = dataProvider(count, upperBound);

            int      concurrency = 20;
            IBuilder builder     = ThreadPools.NewThreadPoolConfigBuilder();

            using (IThreadPool threadPool = ThreadPools.NewThreadPool(builder.SetMaxSize(concurrency).SetMinSize(concurrency).Build()))
            {
                object         @lock = new object();
                CountdownEvent latch = new CountdownEvent(concurrency);
                for (int i = 0; i < concurrency; i++)
                {
                    threadPool.Submit(() =>
                    {
                        lock (@lock)
                        {
                            Monitor.Wait(@lock);
                        }

                        items.ForEach(item => quantileEstimator.Add(item));
                        quantileEstimator.Get(quantiles);
                        latch.Signal();
                    });
                }

                Thread.Sleep(1 * 1000);

                lock (@lock)
                {
                    Monitor.PulseAll(@lock);
                }

                latch.Wait();
            }

            Console.WriteLine("data: " + items);
            Console.WriteLine();

            items.Sort();

            Console.WriteLine("sorted: " + items);
            Console.WriteLine();

            Dictionary <Double, int> quantileResults = new Dictionary <Double, int>();

            foreach (Double quantile in quantiles)
            {
                int pos = (int)(count * quantile) - 1;
                if (pos < 0)
                {
                    pos = 0;
                }

                int item = items[pos];
                quantileEstimator.Add(item);
                quantileResults[quantile] = item;
            }

            Dictionary <Double, int> results = quantileEstimator.Get(quantiles);

            for (int i = 0; i < quantiles.Count; i++)
            {
                Double quantile    = quantiles[i];
                int    expected    = quantileResults[quantile];
                int    actual      = results[quantile];
                int    actualError = Math.Abs(actual - expected);
                Console.WriteLine("quantile " + quantile + ", expected: " + expected + ", actual: " + actual + ", error: "
                                  + actualError);
                Console.WriteLine();

                Assert.True(actualError <= maxError);
            }
        }