-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.go
56 lines (49 loc) · 1.45 KB
/
client.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
// Package grpcx provides ...
package grpcx
import (
"time"
"github.com/trustasia-com/go-van/pkg/server"
"github.com/trustasia-com/go-van/pkg/server/grpcx/clientinterceptor"
"github.com/trustasia-com/go-van/pkg/server/grpcx/resolver"
"google.golang.org/grpc"
"google.golang.org/grpc/balancer/roundrobin"
)
// DialContext dial to grpc server
func DialContext(opts ...server.DialOption) (*grpc.ClientConn, error) {
options := server.DialOptions{
Endpoint: ":0",
Timeout: time.Second,
Flag: server.ClientStdFlag,
}
for _, o := range opts {
o(&options)
}
// grpc dial options
grpcOpts := []grpc.DialOption{
grpc.WithBalancerName(roundrobin.Name),
}
// discovery
if options.Registry != nil {
builder := resolver.NewBuilder(options.Registry)
grpcOpts = append(grpcOpts, grpc.WithResolvers(builder))
}
// flag apply option
if options.Flag&server.FlagSecure == 0 {
grpcOpts = append(grpcOpts, grpc.WithInsecure())
}
if options.Flag&server.FlagTracing > 0 {
grpcOpts = append(grpcOpts,
grpc.WithUnaryInterceptor(clientinterceptor.UnaryTraceInterceptor()),
grpc.WithStreamInterceptor(clientinterceptor.StreamTraceInterceptor()),
)
}
// context custom options
if options.Context != nil {
opts, ok := options.Context.Value(grpcOptsKey{}).([]grpc.DialOption)
if ok {
grpcOpts = append(grpcOpts, opts...)
}
return grpc.DialContext(options.Context, options.Endpoint, grpcOpts...)
}
return grpc.Dial(options.Endpoint, grpcOpts...)
}