Пример #1
0
        /// <summary>
        ///		Initializes a new instance of the <see cref="OnTheFlyObjectPool&lt;T&gt;"/> class.
        /// </summary>
        /// <param name="factory">
        ///		The factory delegate to create <typeparamref name="T"/> type instance using <see cref="ObjectPoolConfiguration"/>.
        ///	</param>
        /// <param name="configuration">
        ///		The <see cref="ObjectPoolConfiguration"/> which contains various settings of this object pool.
        /// </param>
        /// <exception cref="ArgumentNullException">
        ///		<paramref name="factory"/> is <c>null</c>.
        ///		Or <paramref name="configuration"/> is <c>null</c>.
        /// </exception>
        public OnTheFlyObjectPool(Func <ObjectPoolConfiguration, T> factory, ObjectPoolConfiguration configuration)
        {
            Contract.EndContractBlock();

            _factory       = factory ?? throw new ArgumentNullException(nameof(factory));
            _configuration = configuration ?? throw new ArgumentNullException(nameof(configuration));
        }
Пример #2
0
        /// <summary>
        /// Initializes a new instance of the <see cref="StandardObjectPool&lt;T&gt;"/> class.
        /// </summary>
        /// <param name="factory">
        ///		The factory delegate to create <typeparamref name="T"/> type instance.
        ///	</param>
        /// <param name="configuration">
        ///		The <see cref="ObjectPoolConfiguration"/> which contains various settings of this object pool.
        /// </param>
        /// <exception cref="ArgumentNullException">
        ///		<paramref name="factory"/> is <c>null</c>.
        /// </exception>
        public StandardObjectPool(Func <T> factory, ObjectPoolConfiguration configuration)
        {
            Contract.EndContractBlock();

            var safeConfiguration = (configuration ?? ObjectPoolConfiguration.Default).AsFrozen();

            if (string.IsNullOrWhiteSpace(safeConfiguration.Name))
            {
                TraceSource = new TraceSource(GetType().FullName);
                name        = GetType().FullName + "@" + GetHashCode().ToString("X", CultureInfo.InvariantCulture);
            }
            else
            {
                TraceSource = new TraceSource(safeConfiguration.Name);
                name        = safeConfiguration.Name;
            }

            if (configuration == null && TraceSource.ShouldTrace(StandardObjectPoolTrace.InitializedWithDefaultConfiguration))
            {
                TraceSource.TraceEvent(
                    StandardObjectPoolTrace.InitializedWithDefaultConfiguration,
                    "Initialized with default configuration. { \"Name\" : \"{0}\", \"Type\" : \"{1}\", \"HashCode\" : 0x{2:X} }",
                    name,
                    GetType(),
                    GetHashCode()
                    );
            }
            else if (TraceSource.ShouldTrace(StandardObjectPoolTrace.InitializedWithConfiguration))
            {
                TraceSource.TraceEvent(
                    StandardObjectPoolTrace.InitializedWithConfiguration,
                    "Initialized with specified configuration. { \"Name\" : \"{0}\", \"Type\" : \"{1}\", \"HashCode\" : 0x{2:X}, \"Configuration\" : {3} }",
                    name,
                    GetType(),
                    GetHashCode(),
                    configuration
                    );
            }

            this.configuration = safeConfiguration;
            this.factory       = factory ?? throw new ArgumentNullException(nameof(factory));
            leasesLock         = new ReaderWriterLockSlim(LockRecursionPolicy.NoRecursion);
            borrowTimeout      = safeConfiguration.BorrowTimeout ?? TimeSpan.FromMilliseconds(Timeout.Infinite);
            pool =
                new BlockingCollection <T>(
                    new ConcurrentStack <T>()
                    );

            if (safeConfiguration.MaximumPooled == null)
            {
                leases = new BlockingCollection <WeakReference>(new ConcurrentQueue <WeakReference>());
            }
            else
            {
                leases = new BlockingCollection <WeakReference>(new ConcurrentQueue <WeakReference>(), safeConfiguration.MaximumPooled.Value);
            }

            for (var i = 0; i < safeConfiguration.MinimumReserved; i++)
            {
                if (!AddToPool(factory(), 0))
                {
                    TraceSource.TraceEvent(
                        StandardObjectPoolTrace.FailedToAddPoolInitially,
                        "Failed to add item. {{ \"Name\" : \"{0}\", \"Type\" : \"{1}\", \"HashCode\" : 0x{2:X} }}",
                        name,
                        GetType(),
                        GetHashCode()
                        );
                }
            }

            evictionIntervalMilliseconds = safeConfiguration.EvitionInterval == null ? default(int?) : unchecked ((int)safeConfiguration.EvitionInterval.Value.TotalMilliseconds);

            if (safeConfiguration.MaximumPooled != null &&
                safeConfiguration.MinimumReserved != safeConfiguration.MaximumPooled.GetValueOrDefault() &&
                evictionIntervalMilliseconds != null)
            {
                evictionTimer = new Timer(OnEvictionTimerElapsed, null, evictionIntervalMilliseconds.Value, Timeout.Infinite);
            }
            else
            {
                evictionTimer = null;
            }
        }