From c0b59007ce833aecc1f54a91157672d1f2d81cc6 Mon Sep 17 00:00:00 2001 From: Jack Date: Fri, 27 Nov 2020 13:24:05 +0800 Subject: [PATCH] improve configuration for package gdb --- database/gdb/gdb.go | 62 +++++++++++--------------- database/gdb/gdb_core_config.go | 78 ++++++++++++++++----------------- 2 files changed, 64 insertions(+), 76 deletions(-) diff --git a/database/gdb/gdb.go b/database/gdb/gdb.go index 79c897000..084e6c27a 100644 --- a/database/gdb/gdb.go +++ b/database/gdb/gdb.go @@ -164,18 +164,13 @@ type DB interface { // Core is the base struct for database management. type Core struct { - DB DB // DB interface object. - group string // Configuration group name. - debug *gtype.Bool // Enable debug mode for the database. - cache *gcache.Cache // Cache manager, SQL result cache only. - schema *gtype.String // Custom schema for this object. - dryrun *gtype.Bool // Dry run. - prefix string // Table prefix. - logger *glog.Logger // Logger. - config *ConfigNode // Current config node. - maxIdleConnCount int // Max idle connection count. - maxOpenConnCount int // Max open connection count. - maxConnLifetime time.Duration // Max TTL for a connection. + DB DB // DB interface object. + group string // Configuration group name. + debug *gtype.Bool // Enable debug mode for the database, which can be changed in runtime. + cache *gcache.Cache // Cache manager, SQL result cache only. + schema *gtype.String // Custom schema for this object. + logger *glog.Logger // Logger. + config *ConfigNode // Current config node. } // Driver is the interface for integrating sql drivers into package gdb. @@ -303,17 +298,12 @@ func New(group ...string) (db DB, err error) { if _, ok := configs.config[groupName]; ok { if node, err := getConfigNodeByGroup(groupName, true); err == nil { c := &Core{ - group: groupName, - debug: gtype.NewBool(), - cache: gcache.New(), - schema: gtype.NewString(), - dryrun: gtype.NewBool(), - logger: glog.New(), - prefix: node.Prefix, - config: node, - maxIdleConnCount: defaultMaxIdleConnCount, - maxOpenConnCount: defaultMaxOpenConnCount, - maxConnLifetime: defaultMaxConnLifeTime, // Default max connection life time if user does not configure. + group: groupName, + debug: gtype.NewBool(), + cache: gcache.New(), + schema: gtype.NewString(), + logger: glog.New(), + config: node, } if v, ok := driverMap[node.Type]; ok { c.DB, err = v.New(c, node) @@ -452,22 +442,20 @@ func (c *Core) getSqlDb(master bool, schema ...string) (sqlDb *sql.DB, err error intlog.Printf("DB open failed: %v, %+v", err, node) return nil, err } - if c.maxIdleConnCount > 0 { - sqlDb.SetMaxIdleConns(c.maxIdleConnCount) - } else if node.MaxIdleConnCount > 0 { - sqlDb.SetMaxIdleConns(node.MaxIdleConnCount) + if c.config.MaxIdleConnCount > 0 { + sqlDb.SetMaxIdleConns(c.config.MaxIdleConnCount) } - - if c.maxOpenConnCount > 0 { - sqlDb.SetMaxOpenConns(c.maxOpenConnCount) - } else if node.MaxOpenConnCount > 0 { - sqlDb.SetMaxOpenConns(node.MaxOpenConnCount) + if c.config.MaxOpenConnCount > 0 { + sqlDb.SetMaxOpenConns(c.config.MaxOpenConnCount) } - - if c.maxConnLifetime > 0 { - sqlDb.SetConnMaxLifetime(c.maxConnLifetime * time.Second) - } else if node.MaxConnLifetime > 0 { - sqlDb.SetConnMaxLifetime(node.MaxConnLifetime * time.Second) + if c.config.MaxConnLifetime > 0 { + // Automatically checks whether MaxConnLifetime is configured using string like: "30s", "60s", etc. + // Or else it is configured just using number, which means value in seconds. + if c.config.MaxConnLifetime > time.Second { + sqlDb.SetConnMaxLifetime(c.config.MaxConnLifetime) + } else { + sqlDb.SetConnMaxLifetime(c.config.MaxConnLifetime * time.Second) + } } return sqlDb, nil }, 0) diff --git a/database/gdb/gdb_core_config.go b/database/gdb/gdb_core_config.go index 00e941449..b553919eb 100644 --- a/database/gdb/gdb_core_config.go +++ b/database/gdb/gdb_core_config.go @@ -27,25 +27,26 @@ type ConfigGroup []ConfigNode // ConfigNode is configuration for one node. type ConfigNode struct { - Host string // Host of server, ip or domain like: 127.0.0.1, localhost - Port string // Port, it's commonly 3306. - User string // Authentication username. - Pass string // Authentication password. - Name string // Default used database name. - Type string // Database type: mysql, sqlite, mssql, pgsql, oracle. - Role string // (Optional, "master" in default) Node role, used for master-slave mode: master, slave. - Debug bool // (Optional) Debug mode enables debug information logging and output. - Prefix string // (Optional) Table prefix. - DryRun bool // (Optional) Dry run, which does SELECT but no INSERT/UPDATE/DELETE statements. - Weight int // (Optional) Weight for load balance calculating, it's useless if there's just one node. - Charset string // (Optional, "utf8mb4" in default) Custom charset when operating on database. - CreatedAt string // (Optional) The filed name of table for automatic-filled created datetime. - UpdatedAt string // (Optional) The filed name of table for automatic-filled updated datetime. - DeletedAt string // (Optional) The filed name of table for automatic-filled updated datetime. - LinkInfo string `json:"link"` // (Optional) Custom link information, when it is used, configuration Host/Port/User/Pass/Name are ignored. - MaxIdleConnCount int `json:"maxidle"` // (Optional) Max idle connection configuration for underlying connection pool. - MaxOpenConnCount int `json:"maxopen"` // (Optional) Max open connection configuration for underlying connection pool. - MaxConnLifetime time.Duration `json:"maxlifetime"` // (Optional) Max connection TTL configuration for underlying connection pool. + Host string // Host of server, ip or domain like: 127.0.0.1, localhost + Port string // Port, it's commonly 3306. + User string // Authentication username. + Pass string // Authentication password. + Name string // Default used database name. + Type string // Database type: mysql, sqlite, mssql, pgsql, oracle. + Role string // (Optional, "master" in default) Node role, used for master-slave mode: master, slave. + Debug bool // (Optional) Debug mode enables debug information logging and output. + Prefix string // (Optional) Table prefix. + DryRun bool // (Optional) Dry run, which does SELECT but no INSERT/UPDATE/DELETE statements. + Weight int // (Optional) Weight for load balance calculating, it's useless if there's just one node. + Charset string // (Optional, "utf8mb4" in default) Custom charset when operating on database. + LinkInfo string `json:"link"` // (Optional) Custom link information, when it is used, configuration Host/Port/User/Pass/Name are ignored. + MaxIdleConnCount int `json:"maxidle"` // (Optional) Max idle connection configuration for underlying connection pool. + MaxOpenConnCount int `json:"maxopen"` // (Optional) Max open connection configuration for underlying connection pool. + MaxConnLifetime time.Duration `json:"maxlifetime"` // (Optional) Max connection TTL configuration for underlying connection pool. + CreatedAt string // (Optional) The filed name of table for automatic-filled created datetime. + UpdatedAt string // (Optional) The filed name of table for automatic-filled updated datetime. + DeletedAt string // (Optional) The filed name of table for automatic-filled updated datetime. + TimeMaintainDisabled bool // (Optional) Disable the automatic time maintaining feature. } // configs is internal used configuration object. @@ -138,18 +139,18 @@ func (c *Core) GetLogger() *glog.Logger { // SetMaxIdleConnCount sets the max idle connection count for underlying connection pool. func (c *Core) SetMaxIdleConnCount(n int) { - c.maxIdleConnCount = n + c.config.MaxIdleConnCount = n } // SetMaxOpenConnCount sets the max open connection count for underlying connection pool. func (c *Core) SetMaxOpenConnCount(n int) { - c.maxOpenConnCount = n + c.config.MaxOpenConnCount = n } // SetMaxConnLifetime sets the connection TTL for underlying connection pool. // If parameter <= 0, it means the connection never expires. func (c *Core) SetMaxConnLifetime(d time.Duration) { - c.maxConnLifetime = d + c.config.MaxConnLifetime = d } // String returns the node as string. @@ -165,6 +166,11 @@ func (node *ConfigNode) String() string { ) } +// GetConfig returns the current used node configuration. +func (c *Core) GetConfig() *ConfigNode { + return c.config +} + // SetDebug enables/disables the debug mode. func (c *Core) SetDebug(debug bool) { c.debug.Set(debug) @@ -180,28 +186,27 @@ func (c *Core) GetCache() *gcache.Cache { return c.cache } -// GetPrefix returns the table prefix string configured. -func (c *Core) GetPrefix() string { - return c.prefix -} - // GetGroup returns the group string configured. func (c *Core) GetGroup() string { return c.group } // SetDryRun enables/disables the DryRun feature. -func (c *Core) SetDryRun(dryrun bool) { - c.dryrun.Set(dryrun) +// Deprecated, use GetConfig instead. +func (c *Core) SetDryRun(enabled bool) { + c.config.DryRun = enabled } // GetDryRun returns the DryRun value. +// Deprecated, use GetConfig instead. func (c *Core) GetDryRun() bool { - if allDryRun { - // Globally set. - return true - } - return c.dryrun.Val() + return c.config.DryRun +} + +// GetPrefix returns the table prefix string configured. +// Deprecated, use GetConfig instead. +func (c *Core) GetPrefix() string { + return c.config.Prefix } // SetSchema changes the schema for this database connection object. @@ -215,8 +220,3 @@ func (c *Core) SetSchema(schema string) { func (c *Core) GetSchema() string { return c.schema.Val() } - -// GetConfig returns the current used node configuration. -func (c *Core) GetConfig() *ConfigNode { - return c.config -}