|
| 1 | +//===----------------------------------------------------------------------===// |
| 2 | +// |
| 3 | +// This source file is part of the swift-valkey project |
| 4 | +// |
| 5 | +// Copyright (c) 2025 the swift-valkey authors |
| 6 | +// Licensed under Apache License v2.0 |
| 7 | +// |
| 8 | +// See LICENSE.txt for license information |
| 9 | +// See swift-valkey/CONTRIBUTORS.txt for the list of swift-valkey authors |
| 10 | +// |
| 11 | +// SPDX-License-Identifier: Apache-2.0 |
| 12 | +// |
| 13 | +//===----------------------------------------------------------------------===// |
| 14 | + |
| 15 | +/// Allows the cluster client to initially find at least one node in the cluster or find the |
| 16 | +/// nodes again if connection to the has been lost. |
| 17 | +public protocol ValkeyClusterDiscovery { |
| 18 | + /// A type that describes a single node in a valkey cluster |
| 19 | + associatedtype NodeDescription: ValkeyNodeDescriptionProtocol |
| 20 | + |
| 21 | + /// A type that describes multiple nodes in a valkey cluster |
| 22 | + associatedtype NodeDescriptions: Collection<NodeDescription> |
| 23 | + |
| 24 | + /// Lookup all the nodes within the cluster. |
| 25 | + func lookupNodes() async throws -> NodeDescriptions |
| 26 | +} |
| 27 | + |
| 28 | +/// A description of a single node that is part of a valkey cluster |
| 29 | +public protocol ValkeyNodeDescriptionProtocol: Sendable, Equatable { |
| 30 | + /// The node's host name. |
| 31 | + var host: String? { get } |
| 32 | + /// The node's ip address. |
| 33 | + var ip: String? { get } |
| 34 | + /// The nodes endpoint. This should normally be the ``host`` if the node has a routable hostname. |
| 35 | + /// Otherwise it is the ``ip``. This property is used to create connections to the node. |
| 36 | + var endpoint: String { get } |
| 37 | + /// The node's redis port |
| 38 | + var port: Int { get } |
| 39 | + /// Defines if TLS shall be used to create a connection to the node |
| 40 | + var useTLS: Bool { get } |
| 41 | +} |
| 42 | + |
| 43 | +public struct ValkeyStaticClusterDiscovery: ValkeyClusterDiscovery { |
| 44 | + |
| 45 | + public struct NodeDescription: ValkeyNodeDescriptionProtocol { |
| 46 | + public var host: String? |
| 47 | + public var ip: String? |
| 48 | + public var endpoint: String |
| 49 | + public var port: Int |
| 50 | + public var useTLS: Bool |
| 51 | + |
| 52 | + public init(host: String, ip: String? = nil, port: Int = 5678, useTLS: Bool = true) { |
| 53 | + self.host = host |
| 54 | + self.ip = ip |
| 55 | + self.endpoint = host |
| 56 | + self.port = port |
| 57 | + self.useTLS = useTLS |
| 58 | + } |
| 59 | + |
| 60 | + public init(ip: String, port: Int = 5678, useTLS: Bool = false) { |
| 61 | + self.host = nil |
| 62 | + self.ip = ip |
| 63 | + self.endpoint = ip |
| 64 | + self.port = port |
| 65 | + self.useTLS = useTLS |
| 66 | + } |
| 67 | + } |
| 68 | + |
| 69 | + private var nodes: [NodeDescription] |
| 70 | + |
| 71 | + public init(_ nodes: [NodeDescription]) { |
| 72 | + self.nodes = nodes |
| 73 | + } |
| 74 | + |
| 75 | + public func lookupNodes() -> [NodeDescription] { |
| 76 | + self.nodes |
| 77 | + } |
| 78 | +} |
0 commit comments