/// <summary> /// Constructor with the intialSize and maxSize. <see cref="ArgumentException"/> is thrown when <paramref name="initialSize"/> is larger than <paramref name="maxSize"/> /// <see cref="IPooledObjectFactory{T}.Create"/> is called to create objects with the number of <paramref name="initialSize"/>. If it returns the object with null, it is not /// added to the pool. /// </summary> /// <param name="initialSize">The initial object number of the pool.</param> /// <param name="maxSize">The max object number of the pool.</param> /// <param name="factory">The factory create and destroy the pooled object.</param> /// <param name="validator">Validator instance. Can be <see langword="null"/>.</param> public GenericObjectPool(int initialSize, int maxSize, IPooledObjectFactory <T> factory, IPooledObjectValidator <T> validator, int acquiredInvalidLimit) { if (initialSize < 0) { throw new ArgumentException("The pool initial size is invalid"); } if (maxSize != -1 && maxSize < initialSize) { throw new ArgumentException("The pool max size is invalid"); } if (factory == null) { throw new ArgumentNullException(nameof(factory)); } this.initialSize = initialSize; this.maxSize = maxSize; this.factory = factory; this.validator = validator ?? new NeverValidateValidator(); this.acquiredInvalidLimit = acquiredInvalidLimit; createdCount = 0; objectReturned = new AutoResetEvent(false); locker = new ReaderWriterLockSlim(); objQueue = new ConcurrentQueue <T>(); idleObjects = new Dictionary <T, bool>(); for (var i = 0; i < initialSize; i++) { objQueue.Enqueue(factory.Create()); createdCount++; } }
public GenericObjectPool(IPooledObjectFactory <T> factory, GenericObjectPoolConfig genericObjectPoolConfig) : base(genericObjectPoolConfig) { if (factory == null) { throw new ArgumentNullException(nameof(factory)); } this._factory = factory; this._idleObjects = new BlockingList <IPooledObject <T> >(BorrowStrategy.LIFO); this.SetConfig(genericObjectPoolConfig); StartEvictor(genericObjectPoolConfig.TimeBetweenEvictionRunsMillis); }
public GenericObjectPool(IPooledObjectFactory <T> factory, GenericObjectPoolConfig config) : base(config) { if (factory == null) { throw new Exception("factory may not be null"); } this.factory = factory; idleObjects = new LinkedBlockingDeque <IPooledObject <T> >(); SetConfig(config); // TODO startEvictor(getTimeBetweenEvictionRunsMillis()); }
public IObjectPool <T> Instance() { if (objectFactory == null) { if (creator == null) { throw new InvalidOperationException("The object pool cannot be instantiated as the object creation method is not defined."); } objectFactory = new ObjectFactory <T> { Creator = creator, Destroyer = destroyer ?? new Action <T>((obj) => { (obj as IDisposable)?.Dispose(); }) }; } if (maxSize > 0 && maxSize < initialSize) { throw new ArgumentException("The maximum size of the pool shall not be smaller than its initial size."); } if (acquiredInvalidLimit < 0) { throw new ArgumentException($"The limit of acquired invalid objects must not be negative. Actual value is {acquiredInvalidLimit}."); } var newPool = new GenericObjectPool <T>(initialSize, maxSize, objectFactory, validator, acquiredInvalidLimit); if (string.IsNullOrWhiteSpace(key)) { poolManager.AddPool(newPool); } else { poolManager.AddKeyedPool(key, newPool); } return(newPool); }
public GenericObjectPool(IPooledObjectFactory <T> factory, GenericObjectPoolConfig config, AbandonedConfig abandonedConfig) : this(factory, config) { this.SetAbandonedConfig(abandonedConfig); }
public GenericObjectPool(IPooledObjectFactory <T> factory) : this(factory, new GenericObjectPoolConfig()) { }
public IPoolBuilder <T> WithFactory(IPooledObjectFactory <T> factory) { objectFactory = factory; return(this); }
public ObjectPool(IPooledObjectFactory <T> objectFactory) { this.objectFactory = objectFactory; free = new Stack <T>(); Spawned = new List <T>(); }
public IPoolDescriptor <T> WithFactory(IPooledObjectFactory <T> factory) { objectFactory = factory; return(this); }
public void ByFactory(IPooledObjectFactory <T> factory) { ObjectFactory = factory; }