aboutsummaryrefslogtreecommitdiff
path: root/lib/VNLib.Data.Caching.Extensions/src/Clustering/CacheClientConfiguration.cs
blob: 5a15d338eac00194e4f1f43b33f08cba617e70e2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
/*
* 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
{

    /// <summary>
    /// A fluent api configuration object for configuring a <see cref="FBMClient"/>
    /// to connect to cache servers.
    /// </summary>
    public class CacheClientConfiguration
    {
        /// <summary>
        /// Stores available cache servers to be used for discovery, and connections
        /// </summary>
        public INodeDiscoveryCollection NodeCollection { get; } = new NodeDiscoveryCollection();

        /// <summary>
        /// The authentication manager to use for signing and verifying messages to and from the cache servers
        /// </summary>
        public ICacheAuthManager AuthManager { get; private set; }

        /// <summary>
        /// The error handler to use for handling errors that occur during the discovery process
        /// </summary>
        public ICacheDiscoveryErrorHandler? ErrorHandler { get; private set; }

        /// <summary>
        /// Specifies if all connections should use TLS
        /// </summary>
        public bool UseTls { get; private set; }

        internal Uri[]? WellKnownNodes { get; set; }

        /// <summary>
        /// Specifies the JWT authentication manager to use for signing and verifying JWTs
        /// </summary>
        /// <param name="manager">The authentication manager</param>
        /// <returns>Chainable fluent object</returns>
        public CacheClientConfiguration WithAuthenticator(ICacheAuthManager manager)
        {
            AuthManager = manager;
            return this;
        }

        /// <summary>
        /// Specifies if all connections should be using TLS
        /// </summary>
        /// <param name="useTls">A value that indicates if connections should use TLS</param>
        public CacheClientConfiguration WithTls(bool useTls)
        {
            UseTls = useTls;
            return this;
        }

        /// <summary>
        /// Specifies the initial cache peers to connect to
        /// </summary>
        /// <param name="peers">The collection of servers to discover peers from and connect to</param>
        /// <returns>Chainable fluent object</returns>
        /// <exception cref="ArgumentException"></exception>
        /// <exception cref="ArgumentNullException"></exception>
        public CacheClientConfiguration WithInitialPeers(IEnumerable<Uri> 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;
        }

        /// <summary>
        /// Specifies the error handler to use for handling errors that occur during the discovery process
        /// </summary>
        /// <param name="handler">The error handler to use during a discovery</param>
        /// <returns>Chainable fluent object</returns>
        public CacheClientConfiguration WithErrorHandler(ICacheDiscoveryErrorHandler handler)
        {
            ErrorHandler = handler;
            return this;
        }
    }
}