Skip to content

Commit

Permalink
Added missing cluster configuration option to adjust the size of the …
Browse files Browse the repository at this point in the history
…cache.
  • Loading branch information
phillipCouto committed Jul 3, 2014
1 parent 7d8a91b commit f56b352
Showing 1 changed file with 35 additions and 25 deletions.
60 changes: 35 additions & 25 deletions cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,36 +14,38 @@ import (
// behavior to fit the most common use cases. Applications that requre a
// different setup must implement their own cluster.
type ClusterConfig struct {
Hosts []string // addresses for the initial connections
CQLVersion string // CQL version (default: 3.0.0)
ProtoVersion int // version of the native protocol (default: 2)
Timeout time.Duration // connection timeout (default: 600ms)
DefaultPort int // default port (default: 9042)
Keyspace string // initial keyspace (optional)
NumConns int // number of connections per host (default: 2)
NumStreams int // number of streams per connection (default: 128)
Consistency Consistency // default consistency level (default: Quorum)
Compressor Compressor // compression algorithm (default: nil)
Authenticator Authenticator // authenticator (default: nil)
RetryPolicy RetryPolicy // Default retry policy to use for queries (default: 0)
SocketKeepalive time.Duration // The keepalive period to use, enabled if > 0 (default: 0)
ConnPoolType NewPoolFunc // The function used to create the connection pool for the session (default: NewSimplePool)
DiscoverHosts bool // If set, gocql will attempt to automatically discover other members of the Cassandra cluster (default: false)
Hosts []string // addresses for the initial connections
CQLVersion string // CQL version (default: 3.0.0)
ProtoVersion int // version of the native protocol (default: 2)
Timeout time.Duration // connection timeout (default: 600ms)
DefaultPort int // default port (default: 9042)
Keyspace string // initial keyspace (optional)
NumConns int // number of connections per host (default: 2)
NumStreams int // number of streams per connection (default: 128)
Consistency Consistency // default consistency level (default: Quorum)
Compressor Compressor // compression algorithm (default: nil)
Authenticator Authenticator // authenticator (default: nil)
RetryPolicy RetryPolicy // Default retry policy to use for queries (default: 0)
SocketKeepalive time.Duration // The keepalive period to use, enabled if > 0 (default: 0)
ConnPoolType NewPoolFunc // The function used to create the connection pool for the session (default: NewSimplePool)
DiscoverHosts bool // If set, gocql will attempt to automatically discover other members of the Cassandra cluster (default: false)
MaxPreparedStmts int // Sets the maximum cache size for prepared statements globally for gocql (default: 1000)
}

// NewCluster generates a new config for the default cluster implementation.
func NewCluster(hosts ...string) *ClusterConfig {
cfg := &ClusterConfig{
Hosts: hosts,
CQLVersion: "3.0.0",
ProtoVersion: 2,
Timeout: 600 * time.Millisecond,
DefaultPort: 9042,
NumConns: 2,
NumStreams: 128,
Consistency: Quorum,
ConnPoolType: NewSimplePool,
DiscoverHosts: false,
Hosts: hosts,
CQLVersion: "3.0.0",
ProtoVersion: 2,
Timeout: 600 * time.Millisecond,
DefaultPort: 9042,
NumConns: 2,
NumStreams: 128,
Consistency: Quorum,
ConnPoolType: NewSimplePool,
DiscoverHosts: false,
MaxPreparedStmts: 1000,
}
return cfg
}
Expand All @@ -58,6 +60,14 @@ func (cfg *ClusterConfig) CreateSession() (*Session, error) {
}
pool := cfg.ConnPoolType(cfg)

//Adjust the size of the prepared statements cache to match the latest configuration
stmtsLRU.mu.Lock()
for stmtsLRU.lru.Len() > cfg.MaxPreparedStmts {
stmtsLRU.lru.RemoveOldest()
}
stmtsLRU.lru.MaxEntries = cfg.MaxPreparedStmts
stmtsLRU.mu.Unlock()

//See if there are any connections in the pool
if pool.Size() > 0 {
s := NewSession(pool, *cfg)
Expand Down

0 comments on commit f56b352

Please sign in to comment.