/*
* Copyright (c) 2023 Vaughn Nugent
*
* Library: VNLib
* Package: VNLib.Data.Caching.Extensions
* File: CacheClientConfiguration.cs
*
* CacheClientConfiguration.cs is part of VNLib.Data.Caching.Extensions which is part of the larger
* VNLib collection of libraries and utilities.
*
* VNLib.Data.Caching.Extensions is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* VNLib.Data.Caching.Extensions 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 Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see https://www.gnu.org/licenses/.
*/
using System;
using System.Linq;
using System.Collections.Generic;
namespace VNLib.Data.Caching.Extensions.Clustering
{
///
/// A fluent api configuration object for configuring a
/// to connect to cache servers.
///
public class CacheClientConfiguration
{
///
/// Stores available cache servers to be used for discovery, and connections
///
public INodeDiscoveryCollection NodeCollection { get; } = new NodeDiscoveryCollection();
///
/// The authentication manager to use for signing and verifying messages to and from the cache servers
///
public ICacheAuthManager AuthManager { get; private set; }
///
/// The error handler to use for handling errors that occur during the discovery process
///
public ICacheDiscoveryErrorHandler? ErrorHandler { get; private set; }
///
/// Specifies if all connections should use TLS
///
public bool UseTls { get; private set; }
internal Uri[]? WellKnownNodes { get; set; }
///
/// Specifies the JWT authentication manager to use for signing and verifying JWTs
///
/// The authentication manager
/// Chainable fluent object
public CacheClientConfiguration WithAuthenticator(ICacheAuthManager manager)
{
AuthManager = manager;
return this;
}
///
/// Specifies if all connections should be using TLS
///
/// A value that indicates if connections should use TLS
public CacheClientConfiguration WithTls(bool useTls)
{
UseTls = useTls;
return this;
}
///
/// Specifies the initial cache peers to connect to
///
/// The collection of servers to discover peers from and connect to
/// Chainable fluent object
public CacheClientConfiguration WithInitialPeers(IEnumerable peers)
{
//Check null
_ = peers ?? throw new ArgumentNullException(nameof(peers));
//Store peer array
WellKnownNodes = peers.ToArray();
if (WellKnownNodes.Any(p => !p.IsAbsoluteUri))
{
WellKnownNodes = null;
throw new ArgumentException("All discoverable node uris must be in absolute form");
}
return this;
}
///
/// Specifies the error handler to use for handling errors that occur during the discovery process
///
/// The error handler to use during a discovery
/// Chainable fluent object
public CacheClientConfiguration WithErrorHandler(ICacheDiscoveryErrorHandler handler)
{
ErrorHandler = handler;
return this;
}
}
}