Exemple #1
0
 /// <summary>
 /// Binds the given creation parameters and creates a <see cref="AsyncResourcePoolObservable{TResource, TCleanUp}"/> which will cache the resources, and call given callbacks when acquiring and returning resources of the <see cref="AsyncResourcePool{TResource}"/>. By calling <see cref="AsyncResourcePoolCleanUp{TCleanUpParameter}.CleanUpAsync"/> method it will close all resources which have been opened for too long.
 /// </summary>
 /// <typeparam name="TResource">The type of resource.</typeparam>
 /// <param name="factory">This <see cref="AsyncResourceFactory{TResource}"/>.</param>
 /// <param name="takeFromPool">The callback to call when taking the resource from the pool.</param>
 /// <param name="returnToPool">The callback to call when resource to the pool.</param>
 /// <returns>An <see cref="AsyncResourcePoolObservable{TResource, TCleanUp}"/> using this <see cref="AsyncResourceFactory{TResource}"/> to create new resources.</returns>
 /// <exception cref="NullReferenceException">If this <see cref="AsyncResourceFactory{TResource}"/> is <c>null</c>.</exception>
 /// <seealso cref="TakeFromInstancePoolDelegate{TInstance}"/>
 /// <seealso cref="ReturnToInstancePoolDelegate{TInstance}"/>
 public static AsyncResourcePoolObservable <TResource, TimeSpan> CreateGenericTimeoutingResourcePool <TResource>(
     this AsyncResourceFactory <TResource> factory,
     TakeFromInstancePoolDelegate <InstanceHolderWithTimestamp <AsyncResourceAcquireInfo <TResource> > > takeFromPool,
     ReturnToInstancePoolDelegate <InstanceHolderWithTimestamp <AsyncResourceAcquireInfo <TResource> > > returnToPool
     ) => new CachingAsyncResourcePoolWithTimeout <TResource>(
     ArgumentValidator.ValidateNotNullReference(factory),
     takeFromPool,
     returnToPool
     );
Exemple #2
0
 /// <summary>
 /// Binds the given creation parameters and creates a <see cref="AsyncResourcePoolObservable{TResource}"/> which will call given callbacks when acquiring and returning resources of the <see cref="AsyncResourcePool{TResource}"/>.
 /// </summary>
 /// <typeparam name="TResource">The type of resource.</typeparam>
 /// <param name="factory">This <see cref="AsyncResourceFactory{TResource}"/>.</param>
 /// <param name="takeFromPool">The callback to call when taking the resource from the pool.</param>
 /// <param name="returnToPool">The callback to call when resource to the pool.</param>
 /// <returns>An <see cref="AsyncResourcePoolObservable{TResource}"/> using this <see cref="AsyncResourceFactory{TResource}"/> to create new resources.</returns>
 /// <exception cref="NullReferenceException">If this <see cref="AsyncResourceFactory{TResource}"/> is <c>null</c>.</exception>
 /// <seealso cref="TakeFromInstancePoolDelegate{TInstance}"/>
 /// <seealso cref="ReturnToInstancePoolDelegate{TInstance}"/>
 public static AsyncResourcePoolObservable <TResource> CreateGenericCachingResourcePool <TResource>(
     this AsyncResourceFactory <TResource> factory,
     TakeFromInstancePoolDelegate <InstanceHolder <AsyncResourceAcquireInfo <TResource> > > takeFromPool,
     ReturnToInstancePoolDelegate <InstanceHolder <AsyncResourceAcquireInfo <TResource> > > returnToPool
     ) => new CachingAsyncResourcePool <TResource, InstanceHolder <AsyncResourceAcquireInfo <TResource> > >(
     ArgumentValidator.ValidateNotNullReference(factory),
     holder => holder.Instance,
     info => new InstanceHolder <AsyncResourceAcquireInfo <TResource> >(info),
     takeFromPool,
     returnToPool
     );
 ///// <summary>
 ///// Creates a new instance of <see cref="CachingAsyncResourcePool{TResource, TResourceInstance, TResourceCreationParams}"/> with given parameters.
 ///// </summary>
 ///// <param name="factory">The <see cref="AsyncResourceFactory{TResource, TParams}"/> to use for creation of new resources.</param>
 ///// <param name="factoryParameters">The parameters to passe to <see cref="AsyncResourceFactory{TResource, TParams}"/> when creating new instances of resources.</param>
 ///// <param name="resourceExtractor">The callback to extract <see cref="AsyncResourceAcquireInfo{TResource}"/> from instances of <typeparamref name="TCachedResource"/>.</param>
 ///// <param name="instanceCreator">The callback to create a new instance of <typeparamref name="TCachedResource"/> from existing <see cref="AsyncResourceAcquireInfo{TResource}"/>.</param>
 ///// <exception cref="ArgumentNullException">If any of <paramref name="factory"/>, <paramref name="resourceExtractor"/>, or <paramref name="instanceCreator"/> is <c>null</c>.</exception>
 public CachingAsyncResourcePool(
     AsyncResourceFactory <TResource> factory,
     Func <TCachedResource, AsyncResourceAcquireInfo <TResource> > resourceExtractor,
     Func <AsyncResourceAcquireInfo <TResource>, TCachedResource> instanceCreator,
     TakeFromInstancePoolDelegate <TCachedResource> takeFromPool,
     ReturnToInstancePoolDelegate <TCachedResource> returnToPool
     ) : base(factory, resourceExtractor, instanceCreator)
 {
     this.Pool          = new LocklessInstancePoolForClassesNoHeapAllocations <TCachedResource>();
     this._takeFromPool = takeFromPool ?? ((p, t) => new ValueTask <TCachedResource>(p.TakeInstance()));
     this._returnToPool = returnToPool ?? ((p, t, i) => { p?.ReturnInstance(i); return(new ValueTask <Boolean>(p != null && i != null)); });
 }