@@ -51,6 +51,48 @@ extension CassandraClient {
5151 /// Sets the cluster's consistency level. Default is `.localOne`.
5252 public var consistency : CassandraClient . Consistency ?
5353
54+ /// The load balancing strategy to use. Default is `nil` which uses ``LoadBalancingStrategy/dataCenterAware(_:)``.
55+ public var loadBalancingStrategy : LoadBalancingStrategy ?
56+
57+ /// A struct representing the load balancing strategy.
58+ public struct LoadBalancingStrategy : Hashable {
59+ enum Backing : Hashable {
60+ case roundRobin( RoundRobin )
61+ case dataCenterAware( DataCenterAware )
62+ }
63+ public struct RoundRobin : Hashable {
64+ public init ( ) { }
65+ }
66+ public struct DataCenterAware : Hashable {
67+ /// Sets the local data center name for DC-aware routing policy.
68+ /// When set, a DC-aware load balancing policy will be used that prioritizes hosts from this data center.
69+ public var localDataCenter : String ?
70+
71+ /// Creates a new data center aware load balancing strategy.
72+ ///
73+ /// - Parameters:
74+ /// - localDataCenter: Sets the local data center name for DC-aware routing policy.
75+ public init (
76+ localDataCenter: String ? = nil
77+ ) {
78+ self . localDataCenter = localDataCenter
79+ }
80+ }
81+
82+ var backing : Backing
83+
84+ /// Returns a new round robin load balancing strategy.
85+ public static func roundRobin( _ roundRobin: RoundRobin = . init( ) ) -> Self {
86+ . init( backing: . roundRobin( roundRobin) )
87+ }
88+
89+ /// Returns a new data center aware load balancing strategy.
90+ public static func dataCenterAware( _ dataCenterAware: DataCenterAware = . init( ) ) -> Self {
91+ . init( backing: . dataCenterAware( dataCenterAware) )
92+ }
93+
94+ }
95+
5496 public enum SpeculativeExecutionPolicy : Hashable {
5597 case constant( delayInMillseconds: Int64 , maxExecutions: Int32 )
5698 case disabled
@@ -70,7 +112,8 @@ extension CassandraClient {
70112 }
71113
72114 public init (
73- contactPointsProvider: @escaping ( @escaping ( Result < ContactPoints , Swift . Error > ) -> Void ) ->
115+ contactPointsProvider:
116+ @escaping ( @escaping ( Result < ContactPoints , Swift . Error > ) -> Void ) ->
74117 Void ,
75118 port: Int32 ,
76119 protocolVersion: ProtocolVersion
@@ -165,6 +208,9 @@ extension CassandraClient {
165208 if let value = self . hostnameResolution {
166209 try cluster. setUseHostnameResolution ( value)
167210 }
211+ if let loadBalancingStrategy = self . loadBalancingStrategy {
212+ try cluster. setLoadBalancingStrategy ( loadBalancingStrategy)
213+ }
168214 if let value = self . randomizedContactPoints {
169215 try cluster. setUseRandomizedContactPoints ( value)
170216 }
@@ -334,6 +380,20 @@ internal final class Cluster {
334380 }
335381 }
336382
383+ func setLoadBalancingStrategy( _ strategy: CassandraClient . Configuration . LoadBalancingStrategy ) throws {
384+ switch strategy. backing {
385+ case . roundRobin( let roundRobin) :
386+ cass_cluster_set_load_balance_round_robin ( self . rawPointer)
387+ case . dataCenterAware( let dataCenterAware) :
388+ cass_cluster_set_load_balance_dc_aware (
389+ self . rawPointer,
390+ dataCenterAware. localDataCenter,
391+ 0 , // This is deprecated so we are using 0
392+ cass_false // This is deprecated so we are using false
393+ )
394+ }
395+ }
396+
337397 func setConsistency( _ consistency: CassConsistency ) throws {
338398 try self . checkResult { cass_cluster_set_consistency ( self . rawPointer, consistency) }
339399 }
0 commit comments