6
6
"fmt"
7
7
"net"
8
8
"strings"
9
- "time"
10
9
11
10
"github.com/go-logr/logr"
12
11
"github.com/go-sql-driver/mysql"
@@ -17,14 +16,18 @@ import (
17
16
kedautil "github.com/kedacore/keda/v2/pkg/util"
18
17
)
19
18
19
+ // mySQLConnectionPoolKey is a custom type that serves as the key for storing
20
+ // and retrieving MySQL connection pools from the global connection pool map
21
+ // It uniquely identifies a MySQL connection pool based on the connection string
22
+ type mySQLConnectionPoolKey string
23
+
20
24
var (
21
25
// A map that holds MySQL connection pools, keyed by connection string,
22
26
// max open connections, max idle connections, and max idle time
23
27
connectionPools * kedautil.RefMap [mySQLConnectionPoolKey , * sql.DB ]
24
28
)
25
29
26
30
func init () {
27
- // Initialize the global connectionPools map
28
31
connectionPools = kedautil .NewRefMap [mySQLConnectionPoolKey , * sql.DB ]()
29
32
}
30
33
@@ -46,31 +49,12 @@ type mySQLMetadata struct {
46
49
QueryValue float64 `keda:"name=queryValue, order=triggerMetadata"`
47
50
ActivationQueryValue float64 `keda:"name=activationQueryValue, order=triggerMetadata, default=0"`
48
51
MetricName string `keda:"name=metricName, order=triggerMetadata, optional"`
49
-
50
- // Connection pool settings
51
- UseGlobalConnPools bool `keda:"name=useGlobalConnPools, order=triggerMetadata, optional"`
52
- MaxOpenConns int `keda:"name=maxOpenConns, order=triggerMetadata, optional"`
53
- MaxIdleConns int `keda:"name=maxIdleConns, order=triggerMetadata, optional"`
54
- ConnMaxIdleTime int `keda:"name=connMaxIdleTime, order=triggerMetadata, optional"` // seconds
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
52
}
65
53
66
- // newMySQLConnectionPoolKey creates a new mySQLConnectionPoolKey
54
+ // newMySQLConnectionPoolKey creates a new mySQLConnectionPoolKey, which is the
55
+ // connection string for the MySQL database
67
56
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
- }
57
+ return mySQLConnectionPoolKey (metadataToConnectionStr (meta ))
74
58
}
75
59
76
60
// NewMySQLScaler creates a new MySQL scaler
@@ -87,15 +71,7 @@ func NewMySQLScaler(config *scalersconfig.ScalerConfig) (Scaler, error) {
87
71
return nil , fmt .Errorf ("error parsing MySQL metadata: %w" , err )
88
72
}
89
73
90
- // Create MySQL connection, if useGlobalConnPools is set to true, it will use
91
- // the global connection pool for the given connection string, otherwise it
92
- // will create a new local connection pool for the given connection string
93
- var conn * sql.DB
94
- if meta .UseGlobalConnPools {
95
- conn , err = getConnectionPool (meta , logger )
96
- } else {
97
- conn , err = newMySQLConnection (meta , logger )
98
- }
74
+ conn , err := getConnectionPool (meta , logger )
99
75
if err != nil {
100
76
return nil , fmt .Errorf ("error creating MySQL connection: %w" , err )
101
77
}
@@ -166,7 +142,7 @@ func getConnectionPool(meta *mySQLMetadata, logger logr.Logger) (*sql.DB, error)
166
142
return nil , err
167
143
}
168
144
err = connectionPools .Store (key , newPool , func (db * sql.DB ) error {
169
- logger .Info ("Closing MySQL connection pool" , "connectionString" , key . connectionString )
145
+ logger .Info ("Closing MySQL connection pool" , "connectionString" , metadataToConnectionStr ( meta ) )
170
146
return db .Close ()
171
147
})
172
148
if err != nil {
@@ -192,28 +168,9 @@ func newMySQLConnection(meta *mySQLMetadata, logger logr.Logger) (*sql.DB, error
192
168
return nil , err
193
169
}
194
170
195
- setConnectionPoolConfiguration (meta , db )
196
-
197
171
return db , nil
198
172
}
199
173
200
- // setConnectionPoolConfiguration configures the MySQL connection pool settings
201
- // based on the parameters provided in mySQLMetadata. If a setting is zero, it
202
- // is left at its default value.
203
- func setConnectionPoolConfiguration (meta * mySQLMetadata , db * sql.DB ) {
204
- if meta .MaxOpenConns > 0 {
205
- db .SetMaxOpenConns (meta .MaxOpenConns )
206
- }
207
-
208
- if meta .MaxIdleConns > 0 {
209
- db .SetMaxIdleConns (meta .MaxIdleConns )
210
- }
211
-
212
- if meta .ConnMaxIdleTime > 0 {
213
- db .SetConnMaxIdleTime (time .Duration (meta .ConnMaxIdleTime ) * time .Second )
214
- }
215
- }
216
-
217
174
// parseMySQLDbNameFromConnectionStr returns dbname from connection string
218
175
// in it is not able to parse it, it returns "dbname" string
219
176
func parseMySQLDbNameFromConnectionStr (connectionString string ) string {
@@ -228,21 +185,6 @@ func parseMySQLDbNameFromConnectionStr(connectionString string) string {
228
185
// Close disposes of MySQL connections, closing either the global pool if used
229
186
// or the local connection pool
230
187
func (s * mySQLScaler ) Close (ctx context.Context ) error {
231
- if s .metadata .UseGlobalConnPools {
232
- if err := s .closeGlobalPool (ctx ); err != nil {
233
- return fmt .Errorf ("error closing MySQL connection: %w" , err )
234
- }
235
- } else {
236
- if err := s .connection .Close (); err != nil {
237
- return fmt .Errorf ("error closing MySQL connection: %w" , err )
238
- }
239
- }
240
-
241
- return nil
242
- }
243
-
244
- // closeGlobalPool closes all MySQL connections in the global pool
245
- func (s * mySQLScaler ) closeGlobalPool (_ context.Context ) error {
246
188
key := newMySQLConnectionPoolKey (s .metadata )
247
189
248
190
if err := connectionPools .RemoveRef (key ); err != nil {
0 commit comments