Ai
1 Star 0 Fork 0

kzangv/gsf-fof-mongo

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
component.go 2.14 KB
一键复制 编辑 原始数据 按行查看 历史
kzangv 提交于 2024-09-05 13:46 +08:00 . fixed
package mongo
import (
"fmt"
"gitee.com/kzangv/gsf-fof"
"gitee.com/kzangv/gsf-fof/logger"
"github.com/urfave/cli/v2"
)
type Config struct {
ReqMaxTime int `json:"rep_max_time" yaml:"rep_max_time"`
MaxIdleTime int `json:"max_idle_time" yaml:"max_idle_time"`
SelectionTime int `json:"selection_time" yaml:"selection_time"`
SlowThreshold int `json:"slow_threshold" yaml:"slow_threshold"`
}
type Component struct {
DSNPrint bool
Cfg Config
Clts map[string]*Client
}
func (c *Component) CliFlags() []cli.Flag {
fs := make([][]cli.Flag, 0, len(c.Clts)+1)
fs = append(fs, []cli.Flag{
&cli.IntFlag{Name: "mongo-req-timeout", Usage: "mongo request timeout", Action: func(_ *cli.Context, i int) error { c.Cfg.ReqMaxTime = i; return nil }},
&cli.IntFlag{Name: "mongo-idle-timeout", Usage: "mongo idle connect timeout", Action: func(_ *cli.Context, i int) error { c.Cfg.MaxIdleTime = i; return nil }},
&cli.IntFlag{Name: "mongo-select-timeout", Usage: "mongo selection timeout", Action: func(_ *cli.Context, i int) error { c.Cfg.SelectionTime = i; return nil }},
&cli.IntFlag{Name: "mongo-slow-threshold", Usage: "mongo slow sql threshold", Action: func(_ *cli.Context, i int) error { c.Cfg.SlowThreshold = i; return nil }},
})
for name, clt := range c.Clts {
clt.ref = c
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 *Component) Init(log logger.Interface, cfg gsf.Config) (err error) {
for name, clt := range c.Clts {
clt.Log = log
if err = clt.Init(); err != nil {
return err
}
// 打印链接信息
msg := fmt.Sprintf("Init Mongo (%s)", name)
if cfg.LogMore() && c.DSNPrint {
msg = fmt.Sprintf("%s [mongodb://%s/%s]", msg, clt.Cfg.Host, clt.Cfg.DB)
}
log.Warn(msg)
if err != nil {
break
}
}
return nil
}
func (c *Component) Run(_ logger.Interface, _ gsf.Config) (err error) {
for k, v := range c.Clts {
if err = v.Load(k, &c.Cfg); err != nil {
break
}
}
return err
}
func (c *Component) Close(_ logger.Interface, _ gsf.Config) error {
return nil
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/kzangv/gsf-fof-mongo.git
git@gitee.com:kzangv/gsf-fof-mongo.git
kzangv
gsf-fof-mongo
gsf-fof-mongo
v0.1.3

搜索帮助