示例#1
0
        /// <summary>
        /// Creates a new JavaScript engine pool.
        /// </summary>
        private ZeroJsPool CreatePool()
        {
            var poolConfig = new ZeroJsPoolConfig
            {
                EngineFactory      = EngineFactory,
                StartEngines       = _config.StartEngines,
                MaxEngines         = _config.MaxEngines,
                MaxUsagesPerEngine = _config.MaxUsagesPerEngine,
            };

            var pool = new ZeroJsPool(poolConfig);

            return(pool);
        }
示例#2
0
        public ZeroJsPool(ZeroJsPoolConfig config)
        {
            _config = config;

            new Thread(
                () =>
            {
                while (!_disposedFlag.IsSet())
                {
                    if (_engines.Count >= _config.StartEngines)
                    {
                        _enginePopulater?.WaitOne(500);
                    }

                    //pupulater
                    while (!_disposedFlag.IsSet() && _engines.Count < _config.StartEngines)
                    {
                        CreateEngine();
                    }

                    //MaxUsagesPerEngine
                    if (!_disposedFlag.IsSet() &&
                        _config.MaxUsagesPerEngine > 0 &&
                        _engines.Count < _config.MaxEngines)
                    {
                        var currentUsages = _engines.Values.Sum();
                        var maxUsages     = _engines.Count * _config.MaxUsagesPerEngine;

                        var engineAverageOverflow = currentUsages > maxUsages * 0.5;

                        if (engineAverageOverflow)
                        {
                            CreateEngine();
                            _enginePopulater?.Set();
                        }
                    }
                }
            })
            {
                IsBackground = true,
                Name         = "ZeroReact Engine creator"
            }
            .Start();

            new Thread(
                () =>
            {
                while (!_disposedFlag.IsSet() && _config.MaxUsagesPerEngine > 0)
                {
                    _engineMaintenance?.WaitOne();

                    var engineToDispose = _engines
                                          .Where(x => x.Value >= _config.MaxUsagesPerEngine)
                                          .OrderByDescending(x => x.Value)
                                          .Select(x => x.Key)
                                          .Take(2); //maxiumum 2 engines to dispose

                    bool anyDisposed = false;

                    foreach (var engine in engineToDispose)
                    {
                        if (!_disposedFlag.IsSet() && _engines.Count >= _config.StartEngines)
                        {
                            DisposeEngine(engine);
                            anyDisposed = true;
                        }
                        else
                        {
                            break;
                        }
                    }

                    if (!_disposedFlag.IsSet() && anyDisposed)
                    {
                        _enginePopulater?.Set();
                        _engineMaintenance?.Set();
                    }
                }
            })
            {
                IsBackground = true,
                Name         = "ZeroReact Engine destroyer"
            }
            .Start();
        }