Skip to content

Commit

Permalink
include pool settings in cache key
Browse files Browse the repository at this point in the history
Signed-off-by: Rob Pickerill <[email protected]>
  • Loading branch information
robpickerill committed Nov 24, 2024
1 parent 79304e9 commit 25d68fe
Showing 1 changed file with 32 additions and 10 deletions.
42 changes: 32 additions & 10 deletions pkg/scalers/mysql_scaler.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,14 @@ import (
)

var (
// A map that holds MySQL connection pools, keyed by connection string
connectionPools *kedautil.RefMap[string, *sql.DB]
// A map that holds MySQL connection pools, keyed by connection string,
// max open connections, max idle connections, and max idle time
connectionPools *kedautil.RefMap[mySQLConnectionPoolKey, *sql.DB]
)

func init() {
// Initialize the global connectionPools map
connectionPools = kedautil.NewRefMap[string, *sql.DB]()
connectionPools = kedautil.NewRefMap[mySQLConnectionPoolKey, *sql.DB]()
}

type mySQLScaler struct {
Expand Down Expand Up @@ -53,6 +54,25 @@ type mySQLMetadata struct {
ConnMaxIdleTime int `keda:"name=connMaxIdleTime, order=triggerMetadata, optional"` // seconds
}

// mySQLConnectionPoolKey is used as a key to store MySQL connection pools in
// the global map
type mySQLConnectionPoolKey struct {
connectionString string
maxOpenConns int
maxIdleConns int
connMaxIdleTime int
}

// newMySQLConnectionPoolKey creates a new mySQLConnectionPoolKey
func newMySQLConnectionPoolKey(meta *mySQLMetadata) mySQLConnectionPoolKey {
return mySQLConnectionPoolKey{
connectionString: metadataToConnectionStr(meta),
maxOpenConns: meta.MaxOpenConns,
maxIdleConns: meta.MaxIdleConns,
connMaxIdleTime: meta.ConnMaxIdleTime,
}
}

// NewMySQLScaler creates a new MySQL scaler
func NewMySQLScaler(config *scalersconfig.ScalerConfig) (Scaler, error) {
metricType, err := GetMetricTargetType(config)
Expand Down Expand Up @@ -127,10 +147,11 @@ func metadataToConnectionStr(meta *mySQLMetadata) string {
// been created, it will create a new connection pool and store it in the
// connectionPools map.
func getConnectionPool(meta *mySQLMetadata, logger logr.Logger) (*sql.DB, error) {
connStr := metadataToConnectionStr(meta)
key := newMySQLConnectionPoolKey(meta)

// Try to load an existing pool and increment its reference count if found
if pool, ok := connectionPools.Load(connStr); ok {
err := connectionPools.AddRef(connStr)
if pool, ok := connectionPools.Load(key); ok {
err := connectionPools.AddRef(key)
if err != nil {
logger.Error(err, "Error increasing connection pool reference count")
return nil, err
Expand All @@ -144,8 +165,8 @@ func getConnectionPool(meta *mySQLMetadata, logger logr.Logger) (*sql.DB, error)
if err != nil {
return nil, err
}
err = connectionPools.Store(connStr, newPool, func(db *sql.DB) error {
logger.Info("Closing MySQL connection pool", "connectionString", connStr)
err = connectionPools.Store(key, newPool, func(db *sql.DB) error {
logger.Info("Closing MySQL connection pool", "connectionString", key.connectionString)
return db.Close()
})
if err != nil {
Expand Down Expand Up @@ -222,8 +243,9 @@ func (s *mySQLScaler) Close(ctx context.Context) error {

// closeGlobalPool closes all MySQL connections in the global pool
func (s *mySQLScaler) closeGlobalPool(_ context.Context) error {
connStr := metadataToConnectionStr(s.metadata)
if err := connectionPools.RemoveRef(connStr); err != nil {
key := newMySQLConnectionPoolKey(s.metadata)

if err := connectionPools.RemoveRef(key); err != nil {
s.logger.Error(err, "Error decreasing connection pool reference count")
return err
}
Expand Down

0 comments on commit 25d68fe

Please sign in to comment.