75 lines
2.0 KiB
Go
75 lines
2.0 KiB
Go
package component
|
|
|
|
import (
|
|
"fmt"
|
|
"gitea.party/public-messag-service/config"
|
|
"gitea.party/public-messag-service/config/app_ser"
|
|
"gitea.party/public-messag-service/config/logger"
|
|
"github.com/urfave/cli/v2"
|
|
)
|
|
|
|
type Redis struct {
|
|
Cfg config.ComponentRedis
|
|
Clts map[string]*RedisClient
|
|
Log logger.Interface
|
|
}
|
|
|
|
func (c *Redis) CliFlags() []cli.Flag {
|
|
fs := make([][]cli.Flag, 0, len(c.Clts)+1)
|
|
fs = append(fs, []cli.Flag{
|
|
&cli.IntFlag{Name: "redis-sr-timeout", Usage: "redis connect socket read timeout", Value: 15, Destination: &c.Cfg.MaxReadTime},
|
|
&cli.IntFlag{Name: "redis-sw-timeout", Usage: "redis connect socket write timeout", Value: 15, Destination: &c.Cfg.MaxWriteTime},
|
|
&cli.IntFlag{Name: "redis-cr-timeout", Usage: "redis client request timeout", Value: 15, Destination: &c.Cfg.MaxRequestTime},
|
|
&cli.IntFlag{Name: "redis-check-idle-time", Usage: "redis idle timeout", Value: 30, Destination: &c.Cfg.IdleCheckTime},
|
|
&cli.IntFlag{Name: "redis-threshold", Usage: "redis slow request threshold", Value: 100, Destination: &c.Cfg.SlowThreshold},
|
|
})
|
|
for name, clt := range c.Clts {
|
|
fs = append(fs, clt.CliFlags(name))
|
|
}
|
|
l := 0
|
|
for k := range fs {
|
|
l += len(fs[k])
|
|
}
|
|
ret := make([]cli.Flag, 0, l)
|
|
for k := range fs {
|
|
ret = append(ret, fs[k]...)
|
|
}
|
|
return ret
|
|
}
|
|
|
|
func (c *Redis) Init(l logger.Interface, cfg *app_ser.AppConfig, _ *cli.Context) error {
|
|
c.Log = l
|
|
// 打印链接信息
|
|
for k, v := range c.Clts {
|
|
v.Init(cfg.Env(), c)
|
|
msg := fmt.Sprintf("Init Redis (%s) config", k)
|
|
if config.PrintfDSN {
|
|
msg = fmt.Sprintf("%s (%s/%d)", msg, v.Config().Addr, v.Config().DB)
|
|
}
|
|
l.WarnForce(msg)
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func (c *Redis) Run() (err error) {
|
|
for name := range c.Clts {
|
|
if err = c.LoadOne(name); err != nil {
|
|
break
|
|
}
|
|
}
|
|
return err
|
|
}
|
|
|
|
func (c *Redis) LoadOne(name string) (err error) {
|
|
if clt, ok := c.Clts[name]; ok {
|
|
err = clt.Load(name)
|
|
} else {
|
|
err = fmt.Errorf("Redis (%s) config no find", name)
|
|
}
|
|
return err
|
|
}
|
|
|
|
func (c *Redis) Close() error {
|
|
return nil
|
|
}
|