예제 #1
0
파일: JsPool.cs 프로젝트: huan086/JSPool
        /// <summary>
        /// Returns an engine to the pool so it can be reused
        /// </summary>
        /// <param name="engine">Engine to return</param>
        public virtual void ReturnEngineToPool(IJsEngine engine)
        {
            EngineMetadata metadata;

            if (!_metadata.TryGetValue(engine, out metadata))
            {
                // This engine was from another pool. This could happen if a pool is recycled
                // and replaced with a different one (like what ReactJS.NET does when any
                // loaded files change). Let's just pretend we never saw it.
                engine.Dispose();
                return;
            }

            metadata.InUse = false;
            var usageCount = metadata.UsageCount;

            if (_config.MaxUsagesPerEngine > 0 && usageCount >= _config.MaxUsagesPerEngine)
            {
                // Engine has been reused the maximum number of times, recycle it.
                DisposeEngine(engine);
                return;
            }

            if (
                _config.GarbageCollectionInterval > 0 &&
                usageCount % _config.GarbageCollectionInterval == 0 &&
                engine.SupportsGarbageCollection()
                )
            {
                engine.CollectGarbage();
            }

            _availableEngines.Add(engine);
        }
예제 #2
0
        /// <summary>
        /// Collecs garbage in the specified engine.
        /// </summary>
        /// <param name="engine">Engine to collect garbage in</param>
        public static void CollectGarbage(this IJsEngine engine)
        {
            if (!engine.SupportsGarbageCollection())
            {
                throw new InvalidOperationException("Engine doesn't support garbage collection.");
            }

            V8CollectGarbage(engine);
        }
예제 #3
0
		/// <summary>
		/// Returns an engine to the pool so it can be reused
		/// </summary>
		/// <param name="engine">Engine to return</param>
		public virtual void ReturnEngineToPool(IJsEngine engine)
		{
			if (!_metadata.ContainsKey(engine))
			{
				// This engine was from another pool. This could happen if a pool is recycled
				// and replaced with a different one (like what ReactJS.NET does when any 
				// loaded files change). Let's just pretend we never saw it.
				engine.Dispose();
				return;
			}

			_metadata[engine].InUse = false;
			var usageCount = _metadata[engine].UsageCount;
            if (_config.MaxUsagesPerEngine > 0 && usageCount >= _config.MaxUsagesPerEngine)
			{
				// Engine has been reused the maximum number of times, recycle it.
				DisposeEngine(engine);
				return;
			}

			if (
				_config.GarbageCollectionInterval > 0 && 
				usageCount % _config.GarbageCollectionInterval == 0 &&
				engine.SupportsGarbageCollection()
			)
			{
				engine.CollectGarbage();
			}

			_availableEngines.Add(engine);
		}