aboutsummaryrefslogtreecommitdiff
path: root/lib/VNLib.Data.Caching.Extensions/src/Clustering/CacheNodeConfiguration.cs
blob: 6b7ab48bcb22277ad9bd49b15f756d06ab4ee512 (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
/*
* Copyright (c) 2023 Vaughn Nugent
* 
* Library: VNLib
* Package: VNLib.Data.Caching.Extensions
* File: CacheNodeConfiguration.cs 
*
* CacheNodeConfiguration.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;

namespace VNLib.Data.Caching.Extensions.Clustering
{

    /// <summary>
    /// A cache configuration for cache servers (nodes)
    /// </summary>
    public class CacheNodeConfiguration : CacheClientConfiguration
    {
        /// <summary>
        /// The address for clients to connect to
        /// </summary>
        public Uri? ConnectEndpoint { get; private set; }

        /// <summary>
        /// Whether or not to advertise ourself to peer nodes
        /// </summary>
        public bool BroadcastAdverisment { get; private set; }

        /// <summary>
        /// Define the endpoint for clients to connect to to discover
        /// other discovertable nodes
        /// </summary>
        public Uri? DiscoveryEndpoint { get; private set; }

        /// <summary>
        /// Gets the configuration for this node as an advertisment
        /// </summary>
        public CacheNodeAdvertisment Advertisment
        {
            get
            {
                return new CacheNodeAdvertisment()
                {
                    DiscoveryEndpoint = DiscoveryEndpoint,
                    ConnectEndpoint = ConnectEndpoint,
                    NodeId = NodeId
                };
            }
        }

        /// <summary>
        /// Sets the full address of our cache endpoint for clients to connect to
        /// </summary>
        /// <param name="connectUri">The uri clients will attempt to connect to</param>
        public CacheNodeConfiguration WithCacheEndpoint(Uri connectUri)
        {
            ConnectEndpoint = connectUri;
            return this;
        }

        /// <summary>
        /// Enables or disables the advertisement of this node to other nodes
        /// </summary>
        /// <param name="discoveryEndpoint">The absolute endpoint clients will use to connect to</param>
        public CacheNodeConfiguration EnableAdvertisment(Uri? discoveryEndpoint)
        {
            BroadcastAdverisment = discoveryEndpoint != null;
            DiscoveryEndpoint = discoveryEndpoint;
            return this;
        }

        ///<inheritdoc/>
        public string NodeId { get; private set; } = null!;

        /// <summary>
        /// Specifies the current server's cluster node id. If this 
        /// is a server connection attempting to listen for changes on the
        /// remote server, this id must be set and unique
        /// </summary>
        /// <param name="nodeId">The cluster node id of the current server</param>
        /// <returns>Chainable fluent object</returns>
        /// <exception cref="ArgumentNullException"></exception>
        public CacheNodeConfiguration WithNodeId(string nodeId)
        {
            NodeId = nodeId ?? throw new ArgumentNullException(nameof(nodeId));

            //Update the node id in the node collection
            (NodeCollection as NodeDiscoveryCollection)!.SetSelfId(nodeId);

            return this;
        }

    }
}