optimize code of redis init
This commit is contained in:
parent
3ff29cc072
commit
3374eec047
|
|
@ -19,6 +19,7 @@ type ServiceConfig struct {
|
||||||
ServiceAddr string `mapstructure:"service_addr"`
|
ServiceAddr string `mapstructure:"service_addr"`
|
||||||
ServiceName string `mapstructure:"service_name"`
|
ServiceName string `mapstructure:"service_name"`
|
||||||
SecretKey string `mapstructure:"secret_key"`
|
SecretKey string `mapstructure:"secret_key"`
|
||||||
|
DeployEnv string `mapstructure:"deploy_env"`
|
||||||
}
|
}
|
||||||
|
|
||||||
// KafkaConfig define config struct of kafka config
|
// KafkaConfig define config struct of kafka config
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,11 @@
|
||||||
|
// Package constants define constant variable
|
||||||
|
package constants
|
||||||
|
|
||||||
|
const (
|
||||||
|
// DevelopmentDeployMode define development operator environment for modelRT project
|
||||||
|
DevelopmentDeployMode = "development"
|
||||||
|
// DebugDeployMode define debug operator environment for modelRT project
|
||||||
|
DebugDeployMode = "debug"
|
||||||
|
// ProductionDeployMode define production operator environment for modelRT project
|
||||||
|
ProductionDeployMode = "production"
|
||||||
|
)
|
||||||
|
|
@ -16,10 +16,10 @@ var (
|
||||||
)
|
)
|
||||||
|
|
||||||
// initClient define func of return successfully initialized redis client
|
// initClient define func of return successfully initialized redis client
|
||||||
func initClient(rCfg config.RedisConfig) *redis.Client {
|
func initClient(rCfg config.RedisConfig, deployEnv string) *redis.Client {
|
||||||
client, err := util.NewRedisClient(
|
client, err := util.NewRedisClient(
|
||||||
rCfg.Addr,
|
rCfg.Addr,
|
||||||
util.WithPassword(rCfg.Password),
|
util.WithPassword(rCfg.Password, deployEnv),
|
||||||
util.WithDB(rCfg.DB),
|
util.WithDB(rCfg.DB),
|
||||||
util.WithPoolSize(rCfg.PoolSize),
|
util.WithPoolSize(rCfg.PoolSize),
|
||||||
util.WithConnectTimeout(time.Duration(rCfg.DialTimeout)*time.Second),
|
util.WithConnectTimeout(time.Duration(rCfg.DialTimeout)*time.Second),
|
||||||
|
|
@ -33,9 +33,9 @@ func initClient(rCfg config.RedisConfig) *redis.Client {
|
||||||
}
|
}
|
||||||
|
|
||||||
// InitRedisClientInstance define func of return instance of redis client
|
// InitRedisClientInstance define func of return instance of redis client
|
||||||
func InitRedisClientInstance(rCfg config.RedisConfig) *redis.Client {
|
func InitRedisClientInstance(rCfg config.RedisConfig, deployEnv string) *redis.Client {
|
||||||
once.Do(func() {
|
once.Do(func() {
|
||||||
_globalStorageClient = initClient(rCfg)
|
_globalStorageClient = initClient(rCfg, deployEnv)
|
||||||
})
|
})
|
||||||
return _globalStorageClient
|
return _globalStorageClient
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -16,10 +16,10 @@ var (
|
||||||
)
|
)
|
||||||
|
|
||||||
// initClient define func of return successfully initialized redis client
|
// initClient define func of return successfully initialized redis client
|
||||||
func initClient(rCfg config.RedisConfig) *redis.Client {
|
func initClient(rCfg config.RedisConfig, deployEnv string) *redis.Client {
|
||||||
client, err := util.NewRedisClient(
|
client, err := util.NewRedisClient(
|
||||||
rCfg.Addr,
|
rCfg.Addr,
|
||||||
util.WithPassword(rCfg.Password),
|
util.WithPassword(rCfg.Password, deployEnv),
|
||||||
util.WithDB(rCfg.DB),
|
util.WithDB(rCfg.DB),
|
||||||
util.WithPoolSize(rCfg.PoolSize),
|
util.WithPoolSize(rCfg.PoolSize),
|
||||||
util.WithConnectTimeout(time.Duration(rCfg.DialTimeout)*time.Second),
|
util.WithConnectTimeout(time.Duration(rCfg.DialTimeout)*time.Second),
|
||||||
|
|
@ -33,9 +33,9 @@ func initClient(rCfg config.RedisConfig) *redis.Client {
|
||||||
}
|
}
|
||||||
|
|
||||||
// InitClientInstance define func of return instance of redis client
|
// InitClientInstance define func of return instance of redis client
|
||||||
func InitClientInstance(rCfg config.RedisConfig) *redis.Client {
|
func InitClientInstance(rCfg config.RedisConfig, deployEnv string) *redis.Client {
|
||||||
once.Do(func() {
|
once.Do(func() {
|
||||||
_globalLockerClient = initClient(rCfg)
|
_globalLockerClient = initClient(rCfg, deployEnv)
|
||||||
})
|
})
|
||||||
return _globalLockerClient
|
return _globalLockerClient
|
||||||
}
|
}
|
||||||
|
|
|
||||||
4
main.go
4
main.go
|
|
@ -130,10 +130,10 @@ func main() {
|
||||||
defer searchPool.Close()
|
defer searchPool.Close()
|
||||||
model.InitAutocompleterWithPool(searchPool)
|
model.InitAutocompleterWithPool(searchPool)
|
||||||
|
|
||||||
storageClient := diagram.InitRedisClientInstance(modelRTConfig.StorageRedisConfig)
|
storageClient := diagram.InitRedisClientInstance(modelRTConfig.StorageRedisConfig, *&modelRTConfig.ServiceConfig.DeployEnv)
|
||||||
defer storageClient.Close()
|
defer storageClient.Close()
|
||||||
|
|
||||||
lockerClient := locker.InitClientInstance(modelRTConfig.LockerRedisConfig)
|
lockerClient := locker.InitClientInstance(modelRTConfig.LockerRedisConfig, *&modelRTConfig.ServiceConfig.DeployEnv)
|
||||||
defer lockerClient.Close()
|
defer lockerClient.Close()
|
||||||
|
|
||||||
// init anchor param ants pool
|
// init anchor param ants pool
|
||||||
|
|
|
||||||
|
|
@ -5,6 +5,8 @@ import (
|
||||||
"errors"
|
"errors"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
"modelRT/constants"
|
||||||
|
|
||||||
"github.com/redis/go-redis/v9"
|
"github.com/redis/go-redis/v9"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
@ -15,9 +17,9 @@ type clientConfig struct {
|
||||||
type Option func(*clientConfig) error
|
type Option func(*clientConfig) error
|
||||||
|
|
||||||
// WithPassword define func of configure redis password options
|
// WithPassword define func of configure redis password options
|
||||||
func WithPassword(password string) Option {
|
func WithPassword(password string, env string) Option {
|
||||||
return func(c *clientConfig) error {
|
return func(c *clientConfig) error {
|
||||||
if password == "" {
|
if env == constants.ProductionDeployMode && password == "" {
|
||||||
return errors.New("password is empty")
|
return errors.New("password is empty")
|
||||||
}
|
}
|
||||||
c.Password = password
|
c.Password = password
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue