Skip to content

Commit 4861916

Browse files
committed
remove parameters for global connection pool
1 parent 500f263 commit 4861916

File tree

1 file changed

+10
-68
lines changed

1 file changed

+10
-68
lines changed

pkg/scalers/mysql_scaler.go

+10-68
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import (
66
"fmt"
77
"net"
88
"strings"
9-
"time"
109

1110
"github.com/go-logr/logr"
1211
"github.com/go-sql-driver/mysql"
@@ -17,14 +16,18 @@ import (
1716
kedautil "github.com/kedacore/keda/v2/pkg/util"
1817
)
1918

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+
2024
var (
2125
// A map that holds MySQL connection pools, keyed by connection string,
2226
// max open connections, max idle connections, and max idle time
2327
connectionPools *kedautil.RefMap[mySQLConnectionPoolKey, *sql.DB]
2428
)
2529

2630
func init() {
27-
// Initialize the global connectionPools map
2831
connectionPools = kedautil.NewRefMap[mySQLConnectionPoolKey, *sql.DB]()
2932
}
3033

@@ -46,31 +49,12 @@ type mySQLMetadata struct {
4649
QueryValue float64 `keda:"name=queryValue, order=triggerMetadata"`
4750
ActivationQueryValue float64 `keda:"name=activationQueryValue, order=triggerMetadata, default=0"`
4851
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
6452
}
6553

66-
// newMySQLConnectionPoolKey creates a new mySQLConnectionPoolKey
54+
// newMySQLConnectionPoolKey creates a new mySQLConnectionPoolKey, which is the
55+
// connection string for the MySQL database
6756
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))
7458
}
7559

7660
// NewMySQLScaler creates a new MySQL scaler
@@ -87,15 +71,7 @@ func NewMySQLScaler(config *scalersconfig.ScalerConfig) (Scaler, error) {
8771
return nil, fmt.Errorf("error parsing MySQL metadata: %w", err)
8872
}
8973

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)
9975
if err != nil {
10076
return nil, fmt.Errorf("error creating MySQL connection: %w", err)
10177
}
@@ -166,7 +142,7 @@ func getConnectionPool(meta *mySQLMetadata, logger logr.Logger) (*sql.DB, error)
166142
return nil, err
167143
}
168144
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))
170146
return db.Close()
171147
})
172148
if err != nil {
@@ -192,28 +168,9 @@ func newMySQLConnection(meta *mySQLMetadata, logger logr.Logger) (*sql.DB, error
192168
return nil, err
193169
}
194170

195-
setConnectionPoolConfiguration(meta, db)
196-
197171
return db, nil
198172
}
199173

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-
217174
// parseMySQLDbNameFromConnectionStr returns dbname from connection string
218175
// in it is not able to parse it, it returns "dbname" string
219176
func parseMySQLDbNameFromConnectionStr(connectionString string) string {
@@ -228,21 +185,6 @@ func parseMySQLDbNameFromConnectionStr(connectionString string) string {
228185
// Close disposes of MySQL connections, closing either the global pool if used
229186
// or the local connection pool
230187
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 {
246188
key := newMySQLConnectionPoolKey(s.metadata)
247189

248190
if err := connectionPools.RemoveRef(key); err != nil {

0 commit comments

Comments
 (0)