/// <summary> /// Binds the given creation parameters and creates a <see cref="AsyncResourcePoolObservable{TResource}"/> which will always create new instance of resource when acquiring resource from <see cref="AsyncResourcePool{TResource}"/>. /// </summary> /// <typeparam name="TResource">The type of resource.</typeparam> /// <param name="factory">This <see cref="AsyncResourceFactory{TResource}"/>.</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> public static AsyncResourcePoolObservable <TResource> CreateOneTimeUseResourcePool <TResource>( this AsyncResourceFactory <TResource> factory ) => new OneTimeUseAsyncResourcePool <TResource, AsyncResourceAcquireInfo <TResource> >( ArgumentValidator.ValidateNotNullReference(factory), instance => instance, acquireInfo => acquireInfo );
/// <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 );
/// <summary> /// Creates a new instance of <see cref="OneTimeUseAsyncResourcePool{TResource, TResourceInstance}"/> /// </summary> /// <param name="factory">The <see cref="AsyncResourceFactory{TResource, TParams}"/> to use for creation of new resources.</param> /// <param name="resourceExtractor">The callback to extract <see cref="AsyncResourceAcquireInfo{TResource}"/> from instances of <typeparamref name="TResourceInstance"/>.</param> /// <param name="instanceCreator">The callback to create a new instance of <typeparamref name="TResourceInstance"/> 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 OneTimeUseAsyncResourcePool( AsyncResourceFactory <TResource> factory, Func <TResourceInstance, AsyncResourceAcquireInfo <TResource> > resourceExtractor, Func <AsyncResourceAcquireInfo <TResource>, TResourceInstance> instanceCreator ) { //this.FactoryParameters = factoryParameters; this.Factory = ArgumentValidator.ValidateNotNull(nameof(factory), factory); this.ResourceExtractor = ArgumentValidator.ValidateNotNull(nameof(resourceExtractor), resourceExtractor); this.InstanceCreator = ArgumentValidator.ValidateNotNull(nameof(instanceCreator), instanceCreator); }
/// <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)); }); }
/// <summary> /// Binds the given creation parameters and creates a <see cref="AsyncResourcePoolObservable{TResource, TCleanUpParameter}"/> which will cache the resources it manages, and also will have a dynamic upper bound on how many resources it caches. 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="getMaximumConcurrentlyUsedResources">The callback to get maximum amount of resources that can be concurrently in use.</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> /// <exception cref="ArgumentNullException">If <paramref name="getMaximumConcurrentlyUsedResources"/> is <c>null</c>.</exception> public static AsyncResourcePoolObservable <TResource, TimeSpan> CreateTimeoutingAndLimitedResourcePool <TResource>( this AsyncResourceFactory <TResource> factory, Func <Int32> getMaximumConcurrentlyUsedResources ) { ArgumentValidator.ValidateNotNullReference(factory); var state = new PoolLimitState(getMaximumConcurrentlyUsedResources); return(factory.CreateGenericTimeoutingResourcePool( state.GetFromPoolLimited, state.ReturnToPoolLimited )); }
/// <summary> /// Binds the given creation parameters and creates a <see cref="AsyncResourcePoolObservable{TResource, TCleanUpParameter}"/> which will cache the resources it manages, and also will have a static upper bound on how many resources it caches. 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="maximumConcurrentlyUsedResources">The static integer indicating the maximum amount of resources that can be concurrently in use.</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> public static AsyncResourcePoolObservable <TResource, TimeSpan> CreateTimeoutingAndLimitedResourcePool <TResource>( this AsyncResourceFactory <TResource> factory, Int32 maximumConcurrentlyUsedResources ) => factory.CreateTimeoutingAndLimitedResourcePool(() => maximumConcurrentlyUsedResources);
/// <summary> /// Binds the given creation parameters and creates a <see cref="AsyncResourcePoolObservable{TResource, TCleanUpParameter}"/> which will cache the resources it manages, but will not have upper bound on how many resources it caches. 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> /// <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> public static AsyncResourcePoolObservable <TResource, TimeSpan> CreateTimeoutingResourcePool <TResource>( this AsyncResourceFactory <TResource> factory ) => factory.CreateGenericTimeoutingResourcePool(null, null);