-
-
Notifications
You must be signed in to change notification settings - Fork 7
/
container.go
110 lines (93 loc) · 2.98 KB
/
container.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
package egorm
import (
"fmt"
_ "github.com/ego-component/egorm/internal/dsn"
"github.com/ego-component/egorm/manager"
"github.com/gotomicro/ego/core/econf"
"github.com/gotomicro/ego/core/elog"
"github.com/gotomicro/ego/core/emetric"
)
// Container ...
type Container struct {
config *config
name string
logger *elog.Component
dsnParser manager.DSNParser
}
// DefaultContainer ...
func DefaultContainer() *Container {
return &Container{
config: DefaultConfig(),
logger: elog.EgoLogger.With(elog.FieldComponent(PackageName)),
}
}
// Load ...
func Load(key string) *Container {
c := DefaultContainer()
if err := econf.UnmarshalKey(key, &c.config); err != nil {
c.logger.Panic("parse config error", elog.FieldErr(err), elog.FieldKey(key))
return c
}
c.logger = c.logger.With(elog.FieldComponentName(key))
c.name = key
return c
}
func (c *Container) setDSNParser(dialect string) error {
dsnParser := manager.Get(dialect)
if dsnParser == nil {
return fmt.Errorf("invalid support Dialect: %s", dialect)
}
c.dsnParser = dsnParser
return nil
}
// Build 构建组件
func (c *Container) Build(options ...Option) *Component {
if c.config.Debug {
options = append(options, WithInterceptor(debugInterceptor))
}
if c.config.EnableTraceInterceptor {
options = append(options, WithInterceptor(traceInterceptor))
}
if c.config.EnableMetricInterceptor {
options = append(options, WithInterceptor(metricInterceptor))
}
for _, option := range options {
option(c)
}
var err error
// todo 设置补齐超时时间
// timeout 1s
// readTimeout 5s
// writeTimeout 5s
err = c.setDSNParser(c.config.Dialect)
if err != nil {
c.logger.Panic("setDSNParser err", elog.String("dialect", c.config.Dialect), elog.FieldErr(err))
}
c.config.dsnCfg, err = c.dsnParser.ParseDSN(c.config.DSN)
if err == nil {
c.logger.Info("start db", elog.FieldAddr(c.config.dsnCfg.Addr), elog.FieldName(c.config.dsnCfg.DBName))
} else {
c.logger.Panic("start db", elog.FieldErr(err))
}
c.logger = c.logger.With(elog.FieldAddr(c.config.dsnCfg.Addr))
component, err := newComponent(c.name, c.dsnParser, c.config, c.logger)
if err != nil {
if c.config.OnFail == "panic" {
c.logger.Panic("open db", elog.FieldErrKind("register err"), elog.FieldErr(err), elog.FieldAddr(c.config.dsnCfg.Addr), elog.FieldValueAny(c.config))
} else {
emetric.ClientHandleCounter.Inc(emetric.TypeGorm, c.name, c.name+".ping", c.config.dsnCfg.Addr, "open err")
c.logger.Error("open db", elog.FieldErrKind("register err"), elog.FieldErr(err), elog.FieldAddr(c.config.dsnCfg.Addr), elog.FieldValueAny(c.config))
return component
}
}
sqlDB, err := component.DB()
if err != nil {
c.logger.Panic("ping db", elog.FieldErrKind("register err"), elog.FieldErr(err), elog.FieldValueAny(c.config))
}
if err := sqlDB.Ping(); err != nil {
c.logger.Panic("ping db", elog.FieldErrKind("register err"), elog.FieldErr(err), elog.FieldValueAny(c.config))
}
// store db
instances.Store(c.name, component)
return component
}