@@ -7,6 +7,7 @@ package gocql
77import (
88 "context"
99 "errors"
10+ "fmt"
1011 "net"
1112 "time"
1213)
@@ -91,6 +92,24 @@ type ClusterConfig struct {
9192 // Default: 128 for older CQL versions
9293 MaxRequestsPerConn int
9394
95+ // Threshold for the number of inflight requests per connection
96+ // after which the connection is considered as heavy loaded
97+ // Default: 512
98+ HeavyLoadedConnectionThreshold int
99+
100+ // When a connection is considered as heavy loaded, the driver
101+ // could switch to the least loaded connection for the same node.
102+ // The switch will happen if the other connection is at least
103+ // HeavyLoadedSwitchConnectionPercentage percentage less busy
104+ // (in terms of inflight requests).
105+ //
106+ // For the default value of 20%, if the heavy loaded connection
107+ // has 100 inflight requests, the switch will happen only if the
108+ // least busy connection has less than 80 inflight requests.
109+ //
110+ // Default: 20%
111+ HeavyLoadedSwitchConnectionPercentage int
112+
94113 // Default consistency level.
95114 // Default: Quorum
96115 Consistency Consistency
@@ -275,23 +294,25 @@ type Dialer interface {
275294// the same host, and will not mark the node being down or up from events.
276295func NewCluster (hosts ... string ) * ClusterConfig {
277296 cfg := & ClusterConfig {
278- Hosts : hosts ,
279- CQLVersion : "3.0.0" ,
280- Timeout : 11 * time .Second ,
281- ConnectTimeout : 11 * time .Second ,
282- Port : 9042 ,
283- NumConns : 2 ,
284- Consistency : Quorum ,
285- MaxPreparedStmts : defaultMaxPreparedStmts ,
286- MaxRoutingKeyInfo : 1000 ,
287- PageSize : 5000 ,
288- DefaultTimestamp : true ,
289- MaxWaitSchemaAgreement : 60 * time .Second ,
290- ReconnectInterval : 60 * time .Second ,
291- ConvictionPolicy : & SimpleConvictionPolicy {},
292- ReconnectionPolicy : & ConstantReconnectionPolicy {MaxRetries : 3 , Interval : 1 * time .Second },
293- SocketKeepalive : 15 * time .Second ,
294- WriteCoalesceWaitTime : 200 * time .Microsecond ,
297+ Hosts : hosts ,
298+ CQLVersion : "3.0.0" ,
299+ Timeout : 11 * time .Second ,
300+ ConnectTimeout : 11 * time .Second ,
301+ Port : 9042 ,
302+ NumConns : 2 ,
303+ Consistency : Quorum ,
304+ MaxPreparedStmts : defaultMaxPreparedStmts ,
305+ MaxRoutingKeyInfo : 1000 ,
306+ PageSize : 5000 ,
307+ DefaultTimestamp : true ,
308+ MaxWaitSchemaAgreement : 60 * time .Second ,
309+ ReconnectInterval : 60 * time .Second ,
310+ ConvictionPolicy : & SimpleConvictionPolicy {},
311+ ReconnectionPolicy : & ConstantReconnectionPolicy {MaxRetries : 3 , Interval : 1 * time .Second },
312+ SocketKeepalive : 15 * time .Second ,
313+ WriteCoalesceWaitTime : 200 * time .Microsecond ,
314+ HeavyLoadedConnectionThreshold : 512 ,
315+ HeavyLoadedSwitchConnectionPercentage : 20 ,
295316 }
296317
297318 return cfg
@@ -329,6 +350,26 @@ func (cfg *ClusterConfig) filterHost(host *HostInfo) bool {
329350 return ! (cfg .HostFilter == nil || cfg .HostFilter .Accept (host ))
330351}
331352
353+ func (cfg * ClusterConfig ) Validate () error {
354+ if len (cfg .Hosts ) == 0 {
355+ return ErrNoHosts
356+ }
357+
358+ if cfg .Authenticator != nil && cfg .AuthProvider != nil {
359+ return errors .New ("Can't use both Authenticator and AuthProvider in cluster config." )
360+ }
361+
362+ if cfg .HeavyLoadedSwitchConnectionPercentage > 100 || cfg .HeavyLoadedSwitchConnectionPercentage < 0 {
363+ return fmt .Errorf ("HeavyLoadedSwitchConnectionPercentage must be between 0 and 100, got %d" , cfg .HeavyLoadedSwitchConnectionPercentage )
364+ }
365+
366+ if cfg .HeavyLoadedConnectionThreshold < 0 {
367+ return fmt .Errorf ("HeavyLoadedConnectionThreshold must be greater than or equal to 0, got %d" , cfg .HeavyLoadedConnectionThreshold )
368+ }
369+
370+ return nil
371+ }
372+
332373var (
333374 ErrNoHosts = errors .New ("no hosts provided" )
334375 ErrNoConnectionsStarted = errors .New ("no connections were made when creating the session" )
0 commit comments