Skip to content

Commit 25d68fe

Browse files
committed
include pool settings in cache key
Signed-off-by: Rob Pickerill <[email protected]>
1 parent 79304e9 commit 25d68fe

File tree

1 file changed

+32
-10
lines changed

1 file changed

+32
-10
lines changed

pkg/scalers/mysql_scaler.go

+32-10
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,14 @@ import (
1818
)
1919

2020
var (
21-
// A map that holds MySQL connection pools, keyed by connection string
22-
connectionPools *kedautil.RefMap[string, *sql.DB]
21+
// A map that holds MySQL connection pools, keyed by connection string,
22+
// max open connections, max idle connections, and max idle time
23+
connectionPools *kedautil.RefMap[mySQLConnectionPoolKey, *sql.DB]
2324
)
2425

2526
func init() {
2627
// Initialize the global connectionPools map
27-
connectionPools = kedautil.NewRefMap[string, *sql.DB]()
28+
connectionPools = kedautil.NewRefMap[mySQLConnectionPoolKey, *sql.DB]()
2829
}
2930

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

57+
// mySQLConnectionPoolKey is used as a key to store MySQL connection pools in
58+
// the global map
59+
type mySQLConnectionPoolKey struct {
60+
connectionString string
61+
maxOpenConns int
62+
maxIdleConns int
63+
connMaxIdleTime int
64+
}
65+
66+
// newMySQLConnectionPoolKey creates a new mySQLConnectionPoolKey
67+
func newMySQLConnectionPoolKey(meta *mySQLMetadata) mySQLConnectionPoolKey {
68+
return mySQLConnectionPoolKey{
69+
connectionString: metadataToConnectionStr(meta),
70+
maxOpenConns: meta.MaxOpenConns,
71+
maxIdleConns: meta.MaxIdleConns,
72+
connMaxIdleTime: meta.ConnMaxIdleTime,
73+
}
74+
}
75+
5676
// NewMySQLScaler creates a new MySQL scaler
5777
func NewMySQLScaler(config *scalersconfig.ScalerConfig) (Scaler, error) {
5878
metricType, err := GetMetricTargetType(config)
@@ -127,10 +147,11 @@ func metadataToConnectionStr(meta *mySQLMetadata) string {
127147
// been created, it will create a new connection pool and store it in the
128148
// connectionPools map.
129149
func getConnectionPool(meta *mySQLMetadata, logger logr.Logger) (*sql.DB, error) {
130-
connStr := metadataToConnectionStr(meta)
150+
key := newMySQLConnectionPoolKey(meta)
151+
131152
// Try to load an existing pool and increment its reference count if found
132-
if pool, ok := connectionPools.Load(connStr); ok {
133-
err := connectionPools.AddRef(connStr)
153+
if pool, ok := connectionPools.Load(key); ok {
154+
err := connectionPools.AddRef(key)
134155
if err != nil {
135156
logger.Error(err, "Error increasing connection pool reference count")
136157
return nil, err
@@ -144,8 +165,8 @@ func getConnectionPool(meta *mySQLMetadata, logger logr.Logger) (*sql.DB, error)
144165
if err != nil {
145166
return nil, err
146167
}
147-
err = connectionPools.Store(connStr, newPool, func(db *sql.DB) error {
148-
logger.Info("Closing MySQL connection pool", "connectionString", connStr)
168+
err = connectionPools.Store(key, newPool, func(db *sql.DB) error {
169+
logger.Info("Closing MySQL connection pool", "connectionString", key.connectionString)
149170
return db.Close()
150171
})
151172
if err != nil {
@@ -222,8 +243,9 @@ func (s *mySQLScaler) Close(ctx context.Context) error {
222243

223244
// closeGlobalPool closes all MySQL connections in the global pool
224245
func (s *mySQLScaler) closeGlobalPool(_ context.Context) error {
225-
connStr := metadataToConnectionStr(s.metadata)
226-
if err := connectionPools.RemoveRef(connStr); err != nil {
246+
key := newMySQLConnectionPoolKey(s.metadata)
247+
248+
if err := connectionPools.RemoveRef(key); err != nil {
227249
s.logger.Error(err, "Error decreasing connection pool reference count")
228250
return err
229251
}

0 commit comments

Comments
 (0)