/*
* Copyright (c) 2023 Vaughn Nugent
*
* Library: VNLib
* Package: VNLib.Net.Rest.Client
* File: RestClientPool.cs
*
* RestClientPool.cs is part of VNLib.Net.Rest.Client which is part of the larger
* VNLib collection of libraries and utilities.
*
* VNLib.Net.Rest.Client is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published
* by the Free Software Foundation, either version 2 of the License,
* or (at your option) any later version.
*
* VNLib.Net.Rest.Client is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with VNLib.Net.Rest.Client. If not, see http://www.gnu.org/licenses/.
*/
using System;
using RestSharp;
using RestSharp.Authenticators;
using VNLib.Utils.Memory.Caching;
namespace VNLib.Net.Rest.Client
{
///
/// Maintains a pool of lazy loaded instances to allow for concurrent client usage
///
public class RestClientPool : ObjectRental
{
///
/// Creates a new instance and creates the specified
/// number of clients with the same number of concurrency.
///
/// The maximum number of clients to create and authenticate, should be the same as the number of maximum allowed tokens
/// A used to initialze the pool of clients
/// An optional authenticator for clients to use
/// An optional client initialzation callback
public RestClientPool(int maxClients, RestClientOptions options, Action? initCb = null, IAuthenticator? authenticator = null)
: base(() =>
{
//Add optional authenticator
options.Authenticator = authenticator;
//load client
RestClient client = new(options);
//Invoke init callback
initCb?.Invoke(client);
return client;
}, null, null, maxClients)
{
}
///
/// Obtains a new for a reused, or new, instance
///
/// The contract that manages the client
public ClientContract Lease() => new(base.Rent(), this);
}
}