-
-
Notifications
You must be signed in to change notification settings - Fork 156
/
component.go
63 lines (51 loc) · 1.45 KB
/
component.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
package egrpc
import (
"context"
"time"
"google.golang.org/grpc"
"github.com/gotomicro/ego/core/elog"
)
// PackageName 设置包名
const PackageName = "client.egrpc"
// Component 组件
type Component struct {
name string
config *Config
logger *elog.Component
*grpc.ClientConn
}
func newComponent(name string, config *Config, logger *elog.Component) *Component {
var ctx = context.Background()
var dialOptions = config.dialOptions
// 默认配置使用block
if config.EnableBlock {
if config.DialTimeout > time.Duration(0) {
var cancel context.CancelFunc
ctx, cancel = context.WithTimeout(ctx, config.DialTimeout)
defer cancel()
}
dialOptions = append(dialOptions, grpc.WithBlock())
}
if config.EnableWithInsecure {
dialOptions = append(dialOptions, grpc.WithInsecure())
}
if config.keepAlive != nil {
dialOptions = append(dialOptions, grpc.WithKeepaliveParams(*config.keepAlive))
}
dialOptions = append(dialOptions, grpc.WithBalancerName(config.BalancerName))
cc, err := grpc.DialContext(ctx, config.Addr, dialOptions...)
if err != nil {
if config.OnFail == "panic" {
logger.Panic("dial grpc server", elog.FieldErrKind("request err"), elog.FieldErr(err))
} else {
logger.Error("dial grpc server", elog.FieldErrKind("request err"), elog.FieldErr(err))
}
}
logger.Info("start grpc client", elog.FieldName(name))
return &Component{
name: name,
config: config,
logger: logger,
ClientConn: cc,
}
}