/
client.go
80 lines (63 loc) · 2.35 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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
// Copyright 2020 Douyu
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package grpc
import (
"context"
"fmt"
"time"
"github.com/douyu/jupiter/pkg/client/grpc/resolver"
"github.com/douyu/jupiter/pkg/core/ecode"
"github.com/douyu/jupiter/pkg/xlog"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials/insecure"
)
type ClientConn = grpc.ClientConn
func newGRPCClient(config *Config) (*grpc.ClientConn, error) {
var ctx = context.Background()
dialOptions := getDialOptions(config)
// 默认使用block连接,失败后fallback到异步连接
if config.DialTimeout > time.Duration(0) {
var cancel context.CancelFunc
ctx, cancel = context.WithTimeout(ctx, config.DialTimeout)
defer cancel()
}
conn, err := grpc.DialContext(ctx, config.Addr, append(dialOptions, grpc.WithBlock())...)
if err != nil {
config.logger.Error("dial grpc server failed, connect without block",
xlog.FieldErrKind(ecode.ErrKindRequestErr), xlog.FieldErr(err))
conn, err = grpc.DialContext(context.Background(), config.Addr, dialOptions...)
if err != nil {
config.logger.Error("connect without block failed",
xlog.FieldErrKind(ecode.ErrKindRequestErr), xlog.FieldErr(err))
return nil, err
}
}
config.logger.Info("start grpc client")
return conn, nil
}
func getDialOptions(config *Config) []grpc.DialOption {
dialOptions := config.dialOptions
if config.KeepAlive != nil {
dialOptions = append(dialOptions, grpc.WithKeepaliveParams(*config.KeepAlive))
}
dialOptions = append(dialOptions,
grpc.WithInsecure(),
grpc.WithTransportCredentials(insecure.NewCredentials()),
grpc.WithResolvers(resolver.NewEtcdBuilder("etcd", config.RegistryConfig)),
grpc.WithDisableServiceConfig(),
)
svcCfg := fmt.Sprintf(`{"loadBalancingPolicy":"%s"}`, config.BalancerName)
dialOptions = append(dialOptions, grpc.WithDefaultServiceConfig(svcCfg))
return dialOptions
}