-
-
Notifications
You must be signed in to change notification settings - Fork 158
/
container.go
76 lines (62 loc) · 1.96 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
package egrpc
import (
"google.golang.org/grpc"
"github.com/gotomicro/ego/core/econf"
"github.com/gotomicro/ego/core/elog"
)
// Option 可选项
type Option func(c *Container)
// Container 容器
type Container struct {
config *Config
name string
logger *elog.Component
}
// DefaultContainer 默认容器
func DefaultContainer() *Container {
return &Container{
config: DefaultConfig(),
logger: elog.EgoLogger.With(elog.FieldComponent(PackageName)),
}
}
// Load 加载配置key
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.logger = c.logger.With(elog.FieldAddr(c.config.Addr))
c.name = key
return c
}
// Build 构建组件
func (c *Container) Build(options ...Option) *Component {
if c.config.Debug {
options = append(options, WithDialOption(grpc.WithChainUnaryInterceptor(debugUnaryClientInterceptor(c.logger, c.name, c.config.Addr))))
}
if c.config.EnableAppNameInterceptor {
options = append(options, WithDialOption(grpc.WithChainUnaryInterceptor(appNameUnaryClientInterceptor())))
}
if c.config.EnableTimeoutInterceptor {
options = append(options, WithDialOption(grpc.WithChainUnaryInterceptor(timeoutUnaryClientInterceptor(c.logger, c.config.ReadTimeout, c.config.SlowLogThreshold))))
}
if c.config.EnableTraceInterceptor {
options = append(options,
WithDialOption(grpc.WithChainUnaryInterceptor(traceUnaryClientInterceptor())),
)
}
options = append(options,
WithDialOption(grpc.WithChainUnaryInterceptor(loggerUnaryClientInterceptor(c.logger, c.config))),
)
if c.config.EnableMetricInterceptor {
options = append(options,
WithDialOption(grpc.WithChainUnaryInterceptor(metricUnaryClientInterceptor(c.name))),
)
}
for _, option := range options {
option(c)
}
return newComponent(c.name, c.config, c.logger)
}