@@ -18,13 +18,14 @@ import (
18
18
)
19
19
20
20
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 ]
23
24
)
24
25
25
26
func init () {
26
27
// Initialize the global connectionPools map
27
- connectionPools = kedautil .NewRefMap [string , * sql.DB ]()
28
+ connectionPools = kedautil .NewRefMap [mySQLConnectionPoolKey , * sql.DB ]()
28
29
}
29
30
30
31
type mySQLScaler struct {
@@ -53,6 +54,25 @@ type mySQLMetadata struct {
53
54
ConnMaxIdleTime int `keda:"name=connMaxIdleTime, order=triggerMetadata, optional"` // seconds
54
55
}
55
56
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
+
56
76
// NewMySQLScaler creates a new MySQL scaler
57
77
func NewMySQLScaler (config * scalersconfig.ScalerConfig ) (Scaler , error ) {
58
78
metricType , err := GetMetricTargetType (config )
@@ -127,10 +147,11 @@ func metadataToConnectionStr(meta *mySQLMetadata) string {
127
147
// been created, it will create a new connection pool and store it in the
128
148
// connectionPools map.
129
149
func getConnectionPool (meta * mySQLMetadata , logger logr.Logger ) (* sql.DB , error ) {
130
- connStr := metadataToConnectionStr (meta )
150
+ key := newMySQLConnectionPoolKey (meta )
151
+
131
152
// 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 )
134
155
if err != nil {
135
156
logger .Error (err , "Error increasing connection pool reference count" )
136
157
return nil , err
@@ -144,8 +165,8 @@ func getConnectionPool(meta *mySQLMetadata, logger logr.Logger) (*sql.DB, error)
144
165
if err != nil {
145
166
return nil , err
146
167
}
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 )
149
170
return db .Close ()
150
171
})
151
172
if err != nil {
@@ -222,8 +243,9 @@ func (s *mySQLScaler) Close(ctx context.Context) error {
222
243
223
244
// closeGlobalPool closes all MySQL connections in the global pool
224
245
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 {
227
249
s .logger .Error (err , "Error decreasing connection pool reference count" )
228
250
return err
229
251
}
0 commit comments