-
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathcontainer.go
84 lines (73 loc) · 2.57 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
package ekafka
import (
"fmt"
"github.com/gotomicro/ego/core/econf"
"github.com/gotomicro/ego/core/elog"
)
type Option func(c *Container)
type Container struct {
config *config
name string
logger *elog.Component
}
// DefaultContainer 返回默认Container
func DefaultContainer() *Container {
return &Container{
config: DefaultConfig(),
logger: elog.EgoLogger.With(elog.FieldComponent(PackageName)),
}
}
// Load 载入配置,初始化Container
func Load(key string) *Container {
c := DefaultContainer()
if err := econf.UnmarshalKey(key, &c.config, econf.WithWeaklyTypedInput(true)); 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
if c.config.EnableCompress {
if c.config.CompressLimit <= 0 {
c.config.CompressLimit = defaultCompressLimit
}
if c.config.CompressType == "" {
c.config.CompressType = compressTypeGzip
}
}
return c
}
// Build 构建Container
func (c *Container) Build(options ...Option) *Component {
// 放第一个时间才准确
options = append(options, WithClientInterceptor(fixedClientInterceptor(c.name, c.config)))
options = append(options, WithClientInterceptor(traceClientInterceptor(c.name, c.config)))
options = append(options, WithClientInterceptor(accessClientInterceptor(c.name, c.config, c.logger)))
if c.config.EnableMetricInterceptor {
options = append(options, WithClientInterceptor(metricClientInterceptor(c.name, c.config)))
}
if c.config.EnableCompress {
options = append(options, WithClientInterceptor(compressClientInterceptor(c.name, c.config, c.logger)))
}
options = append(options, WithServerInterceptor(fixedServerInterceptor(c.name, c.config)))
options = append(options, WithServerInterceptor(traceServerInterceptor(c.name, c.config)))
options = append(options, WithServerInterceptor(accessServerInterceptor(c.name, c.config, c.logger)))
if c.config.EnableMetricInterceptor {
options = append(options, WithServerInterceptor(metricServerInterceptor(c.name, c.config)))
}
if c.config.EnableCompress {
options = append(options, WithServerInterceptor(compressServerInterceptor(c.name, c.config, c.logger)))
}
for _, option := range options {
option(c)
}
c.logger = c.logger.With(elog.FieldAddr(fmt.Sprintf("%s", c.config.Brokers)))
cmp := &Component{
config: c.config,
logger: c.logger,
producers: make(map[string]*Producer),
consumers: make(map[string]*Consumer),
consumerGroups: make(map[string]*ConsumerGroup),
compName: c.name,
}
return cmp
}