From f56b352c906055249893c8e8fb4648f96ac9add2 Mon Sep 17 00:00:00 2001 From: Phillip Couto Date: Wed, 2 Jul 2014 22:07:48 -0400 Subject: [PATCH] Added missing cluster configuration option to adjust the size of the cache. --- cluster.go | 60 +++++++++++++++++++++++++++++++----------------------- 1 file changed, 35 insertions(+), 25 deletions(-) diff --git a/cluster.go b/cluster.go index 4795b2f21..2eb292e53 100644 --- a/cluster.go +++ b/cluster.go @@ -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 } @@ -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)