/// <summary> /// This constructor should be used for asynchronous resource creation /// </summary> /// <param name="resourceTaskFactory">Function to create a task that returns a resource</param> /// <param name="options"></param> public AsyncResourcePool(Func <Task <TResource> > resourceTaskFactory, AsyncResourcePoolOptions options) { _minNumResources = options.MinNumResources; _maxNumResources = options.MaxNumResources; _resourcesExpireAfter = options.ResourcesExpireAfter; _maxNumResourceCreationAttempts = options.MaxNumResourceCreationAttempts; _resourceCreationRetryInterval = options.ResourceCreationRetryInterval; _availableResources = new Queue <TimestampedResource>(); _pendingResourceRequests = new Queue <ResourceRequestMessage>(); _resourceTaskFactory = resourceTaskFactory; // Important: These functions must be called after all instance members have been initialised! _messageHandler = GetMessageHandler(); SetupErrorHandling(); SetupPeriodicPurge(); _messageHandler.Post(new EnsureAvailableResourcesMessage()); }
/// <summary> /// This constructor should be used only if asynchronous resource creation is not available /// </summary> /// <param name="resourceFactory">Function to create a resource</param> /// <param name="options"></param> public AsyncResourcePool(Func <TResource> resourceFactory, AsyncResourcePoolOptions options) : this(() => Task.FromResult(resourceFactory()), options) { }