-
Notifications
You must be signed in to change notification settings - Fork 20
/
client.go
104 lines (89 loc) · 3.17 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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
/**
* Tencent is pleased to support the open source community by making Polaris available.
*
* Copyright (C) 2019 THL A29 Limited, a Tencent company. All rights reserved.
*
* Licensed under the BSD 3-Clause License (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://opensource.org/licenses/BSD-3-Clause
*
* 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 grpcpolaris
import (
"context"
"encoding/base64"
"encoding/json"
"fmt"
"strings"
"google.golang.org/grpc"
"google.golang.org/grpc/metadata"
)
const (
scheme = "polaris"
prefix = scheme + "://"
optionsKey = "options"
)
// DialContext dial target and get connection
func DialContext(ctx context.Context, target string, opts ...DialOption) (conn *grpc.ClientConn, err error) {
options := newDialOptions()
for _, opt := range opts {
opt.apply(options)
}
if options.Config != nil {
setPolarisConfig(options.Config)
}
if !strings.HasPrefix(target, prefix) {
// not polaris target, go through gRPC resolver
return grpc.DialContext(ctx, target, options.gRPCDialOptions...)
}
lbStr := fmt.Sprintf(lbConfig, scheme)
options.gRPCDialOptions = append(options.gRPCDialOptions, grpc.WithDefaultServiceConfig(lbStr))
options.gRPCDialOptions = append(options.gRPCDialOptions, grpc.WithChainUnaryInterceptor(injectCallerInfo(options)))
jsonStr, err := json.Marshal(options)
if nil != err {
return nil, fmt.Errorf("fail to marshal options: %w", err)
}
endpoint := base64.URLEncoding.EncodeToString(jsonStr)
target = fmt.Sprintf("%s?%s=%s", target, optionsKey, endpoint)
return grpc.DialContext(ctx, target, options.gRPCDialOptions...)
}
// BuildTarget build the invoker grpc target
// Deprecated: will remove in 1.4
func BuildTarget(target string, opts ...DialOption) (string, error) {
options := newDialOptions()
for _, opt := range opts {
opt.apply(options)
}
jsonStr, err := json.Marshal(options)
if nil != err {
return "", fmt.Errorf("fail to marshal options: %w", err)
}
if options.Config != nil {
setPolarisConfig(options.Config)
}
endpoint := base64.URLEncoding.EncodeToString(jsonStr)
target = fmt.Sprintf(prefix+"%s?%s=%s", target, optionsKey, endpoint)
return target, nil
}
// injectCallerInfo 将主调方信息写入 grpc 请求的 header 中
func injectCallerInfo(options *dialOptions) grpc.UnaryClientInterceptor {
return func(ctx context.Context, method string, req, reply interface{}, cc *grpc.ClientConn,
invoker grpc.UnaryInvoker, opts ...grpc.CallOption) error {
if len(options.SrcService) > 0 {
if _, ok := metadata.FromOutgoingContext(ctx); !ok {
ctx = metadata.NewOutgoingContext(ctx, metadata.MD{})
}
ctx = metadata.AppendToOutgoingContext(ctx,
polarisCallerServiceKey, options.SrcService,
polarisCallerNamespaceKey, options.Namespace,
)
}
return invoker(ctx, method, req, reply, cc, opts...)
}
}