mirror of
https://github.com/alibaba/higress.git
synced 2026-06-02 00:57:28 +08:00
add redis init status log (#1867)
Co-authored-by: Kent Dong <ch3cho@qq.com>
This commit is contained in:
@@ -15,7 +15,7 @@ const (
|
|||||||
|
|
||||||
type providerInitializer interface {
|
type providerInitializer interface {
|
||||||
ValidateConfig(ProviderConfig) error
|
ValidateConfig(ProviderConfig) error
|
||||||
CreateProvider(ProviderConfig) (Provider, error)
|
CreateProvider(ProviderConfig, wrapper.Log) (Provider, error)
|
||||||
}
|
}
|
||||||
|
|
||||||
var (
|
var (
|
||||||
@@ -128,12 +128,12 @@ func (c *ProviderConfig) Validate() error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func CreateProvider(pc ProviderConfig) (Provider, error) {
|
func CreateProvider(pc ProviderConfig, log wrapper.Log) (Provider, error) {
|
||||||
initializer, has := providerInitializers[pc.typ]
|
initializer, has := providerInitializers[pc.typ]
|
||||||
if !has {
|
if !has {
|
||||||
return nil, errors.New("unknown provider type: " + pc.typ)
|
return nil, errors.New("unknown provider type: " + pc.typ)
|
||||||
}
|
}
|
||||||
return initializer.CreateProvider(pc)
|
return initializer.CreateProvider(pc, log)
|
||||||
}
|
}
|
||||||
|
|
||||||
type Provider interface {
|
type Provider interface {
|
||||||
|
|||||||
@@ -16,13 +16,14 @@ func (r *redisProviderInitializer) ValidateConfig(cf ProviderConfig) error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r *redisProviderInitializer) CreateProvider(cf ProviderConfig) (Provider, error) {
|
func (r *redisProviderInitializer) CreateProvider(cf ProviderConfig, log wrapper.Log) (Provider, error) {
|
||||||
rp := redisProvider{
|
rp := redisProvider{
|
||||||
config: cf,
|
config: cf,
|
||||||
client: wrapper.NewRedisClusterClient(wrapper.FQDNCluster{
|
client: wrapper.NewRedisClusterClient(wrapper.FQDNCluster{
|
||||||
FQDN: cf.serviceName,
|
FQDN: cf.serviceName,
|
||||||
Host: cf.serviceHost,
|
Host: cf.serviceHost,
|
||||||
Port: int64(cf.servicePort)}),
|
Port: int64(cf.servicePort)}),
|
||||||
|
log: log,
|
||||||
}
|
}
|
||||||
err := rp.Init(cf.username, cf.password, cf.timeout)
|
err := rp.Init(cf.username, cf.password, cf.timeout)
|
||||||
return &rp, err
|
return &rp, err
|
||||||
@@ -31,6 +32,7 @@ func (r *redisProviderInitializer) CreateProvider(cf ProviderConfig) (Provider,
|
|||||||
type redisProvider struct {
|
type redisProvider struct {
|
||||||
config ProviderConfig
|
config ProviderConfig
|
||||||
client wrapper.RedisClient
|
client wrapper.RedisClient
|
||||||
|
log wrapper.Log
|
||||||
}
|
}
|
||||||
|
|
||||||
func (rp *redisProvider) GetProviderType() string {
|
func (rp *redisProvider) GetProviderType() string {
|
||||||
@@ -38,7 +40,13 @@ func (rp *redisProvider) GetProviderType() string {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (rp *redisProvider) Init(username string, password string, timeout uint32) error {
|
func (rp *redisProvider) Init(username string, password string, timeout uint32) error {
|
||||||
return rp.client.Init(rp.config.username, rp.config.password, int64(rp.config.timeout), wrapper.WithDataBase(rp.config.database))
|
err := rp.client.Init(rp.config.username, rp.config.password, int64(rp.config.timeout), wrapper.WithDataBase(rp.config.database))
|
||||||
|
if rp.client.Ready() {
|
||||||
|
rp.log.Info("redis init successfully")
|
||||||
|
} else {
|
||||||
|
rp.log.Error("redis init failed, will try later")
|
||||||
|
}
|
||||||
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
func (rp *redisProvider) Get(key string, cb wrapper.RedisResponseCallback) error {
|
func (rp *redisProvider) Get(key string, cb wrapper.RedisResponseCallback) error {
|
||||||
|
|||||||
@@ -154,7 +154,7 @@ func (c *PluginConfig) Complete(log wrapper.Log) error {
|
|||||||
}
|
}
|
||||||
if c.cacheProviderConfig.GetProviderType() != "" {
|
if c.cacheProviderConfig.GetProviderType() != "" {
|
||||||
log.Debugf("cache provider is set to %s", c.cacheProviderConfig.GetProviderType())
|
log.Debugf("cache provider is set to %s", c.cacheProviderConfig.GetProviderType())
|
||||||
c.cacheProvider, err = cache.CreateProvider(*c.cacheProviderConfig)
|
c.cacheProvider, err = cache.CreateProvider(*c.cacheProviderConfig, log)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -82,7 +82,7 @@ type LimitConfigItem struct {
|
|||||||
timeWindow int64 // 时间窗口大小
|
timeWindow int64 // 时间窗口大小
|
||||||
}
|
}
|
||||||
|
|
||||||
func initRedisClusterClient(json gjson.Result, config *ClusterKeyRateLimitConfig) error {
|
func initRedisClusterClient(json gjson.Result, config *ClusterKeyRateLimitConfig, log wrapper.Log) error {
|
||||||
redisConfig := json.Get("redis")
|
redisConfig := json.Get("redis")
|
||||||
if !redisConfig.Exists() {
|
if !redisConfig.Exists() {
|
||||||
return errors.New("missing redis in config")
|
return errors.New("missing redis in config")
|
||||||
@@ -111,7 +111,13 @@ func initRedisClusterClient(json gjson.Result, config *ClusterKeyRateLimitConfig
|
|||||||
Port: int64(servicePort),
|
Port: int64(servicePort),
|
||||||
})
|
})
|
||||||
database := int(redisConfig.Get("database").Int())
|
database := int(redisConfig.Get("database").Int())
|
||||||
return config.redisClient.Init(username, password, int64(timeout), wrapper.WithDataBase(database))
|
err := config.redisClient.Init(username, password, int64(timeout), wrapper.WithDataBase(database))
|
||||||
|
if config.redisClient.Ready() {
|
||||||
|
log.Info("redis init successfully")
|
||||||
|
} else {
|
||||||
|
log.Error("redis init failed, will try later")
|
||||||
|
}
|
||||||
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
func parseClusterKeyRateLimitConfig(json gjson.Result, config *ClusterKeyRateLimitConfig) error {
|
func parseClusterKeyRateLimitConfig(json gjson.Result, config *ClusterKeyRateLimitConfig) error {
|
||||||
|
|||||||
@@ -80,7 +80,7 @@ type LimitRedisContext struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func parseConfig(json gjson.Result, config *ClusterKeyRateLimitConfig, log wrapper.Log) error {
|
func parseConfig(json gjson.Result, config *ClusterKeyRateLimitConfig, log wrapper.Log) error {
|
||||||
err := initRedisClusterClient(json, config)
|
err := initRedisClusterClient(json, config, log)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -30,6 +30,8 @@ type RedisResponseCallback func(response resp.Value)
|
|||||||
|
|
||||||
type RedisClient interface {
|
type RedisClient interface {
|
||||||
Init(username, password string, timeout int64, opts ...optionFunc) error
|
Init(username, password string, timeout int64, opts ...optionFunc) error
|
||||||
|
// return whether redis client is ready
|
||||||
|
Ready() bool
|
||||||
// with this function, you can call redis as if you are using redis-cli
|
// with this function, you can call redis as if you are using redis-cli
|
||||||
Command(cmds []interface{}, callback RedisResponseCallback) error
|
Command(cmds []interface{}, callback RedisResponseCallback) error
|
||||||
Eval(script string, numkeys int, keys, args []interface{}, callback RedisResponseCallback) error
|
Eval(script string, numkeys int, keys, args []interface{}, callback RedisResponseCallback) error
|
||||||
@@ -183,6 +185,10 @@ func respString(args []interface{}) []byte {
|
|||||||
return buf.Bytes()
|
return buf.Bytes()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (c *RedisClusterClient[C]) Ready() bool {
|
||||||
|
return c.ready
|
||||||
|
}
|
||||||
|
|
||||||
func (c *RedisClusterClient[C]) Init(username, password string, timeout int64, opts ...optionFunc) error {
|
func (c *RedisClusterClient[C]) Init(username, password string, timeout int64, opts ...optionFunc) error {
|
||||||
for _, opt := range opts {
|
for _, opt := range opts {
|
||||||
opt(&c.option)
|
opt(&c.option)
|
||||||
|
|||||||
Reference in New Issue
Block a user