fix: protect publish path from mysql exhaustion
This commit is contained in:
@@ -21,6 +21,7 @@ import (
|
||||
"github.com/tinode/chat/server/auth"
|
||||
dbadapter "github.com/tinode/chat/server/db"
|
||||
"github.com/tinode/chat/server/db/common"
|
||||
immetrics "github.com/tinode/chat/server/metrics"
|
||||
"github.com/tinode/chat/server/store"
|
||||
t "github.com/tinode/chat/server/store/types"
|
||||
)
|
||||
@@ -109,6 +110,9 @@ type configType struct {
|
||||
MaxOpenConns int `json:"max_open_conns,omitempty"`
|
||||
// Maximum number of connections in the idle connection pool.
|
||||
MaxIdleConns int `json:"max_idle_conns,omitempty"`
|
||||
// Registry database has a separate, much smaller workload and pool budget.
|
||||
RegistryMaxOpenConns int `json:"registry_max_open_conns,omitempty"`
|
||||
RegistryMaxIdleConns int `json:"registry_max_idle_conns,omitempty"`
|
||||
// Maximum amount of time a connection may be reused (in seconds).
|
||||
ConnMaxLifetime int `json:"conn_max_lifetime,omitempty"`
|
||||
|
||||
@@ -185,6 +189,7 @@ func openMySQLDB(config configType, dsn, dbName string) (*sqlx.DB, string, error
|
||||
if config.ConnMaxLifetime > 0 {
|
||||
db.SetConnMaxLifetime(time.Duration(config.ConnMaxLifetime) * time.Second)
|
||||
}
|
||||
immetrics.RegisterDBPool(dbName, db.DB)
|
||||
return db, dbName, nil
|
||||
}
|
||||
|
||||
@@ -281,7 +286,14 @@ func (a *adapter) Open(jsonconfig json.RawMessage) error {
|
||||
a.txTimeout = time.Duration(float64(config.SqlTimeout)*txTimeoutMultiplier) * time.Second
|
||||
}
|
||||
if a.tenantRegistryDB != "" && a.tenantRegistryDB != a.dbName {
|
||||
a.registryDB, _, err = openMySQLDB(config, a.dsn, a.tenantRegistryDB)
|
||||
registryConfig := config
|
||||
if config.RegistryMaxOpenConns > 0 {
|
||||
registryConfig.MaxOpenConns = config.RegistryMaxOpenConns
|
||||
}
|
||||
if config.RegistryMaxIdleConns > 0 {
|
||||
registryConfig.MaxIdleConns = config.RegistryMaxIdleConns
|
||||
}
|
||||
a.registryDB, _, err = openMySQLDB(registryConfig, a.dsn, a.tenantRegistryDB)
|
||||
if err == nil {
|
||||
err = a.registryDB.Ping()
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user