-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathdoc.go
129 lines (116 loc) · 3.34 KB
/
doc.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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
/*
*
* Copyright 2019 gRPC authors.
*
* 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 grpcgcp provides grpc supports for Google Cloud APIs.
For now it provides connection management with affinity support.
Note: "channel" is analagous to "connection" in our context.
Usage:
1. First, initialize the api configuration. There are two ways:
1a. Create a json file defining the configuration and read it.
// Create some_api_config.json
{
"channelPool": {
"maxSize": 4,
"maxConcurrentStreamsLowWatermark": 50
},
"method": [
{
"name": [ "/some.api.v1/Method1" ],
"affinity": {
"command": "BIND",
"affinityKey": "key1"
}
},
{
"name": [ "/some.api.v1/Method2" ],
"affinity": {
"command": "BOUND",
"affinityKey": "key2"
}
},
{
"name": [ "/some.api.v1/Method3" ],
"affinity": {
"command": "UNBIND",
"affinityKey": "key3"
}
}
]
}
jsonFile, err := ioutil.ReadFile("some_api_config.json")
if err != nil {
t.Fatalf("Failed to read config file: %v", err)
}
jsonCfg := string(jsonFile)
1b. Create apiConfig directly and convert it to json.
// import (
// configpb "github.com/GoogleCloudPlatform/grpc-gcp-go/grpcgcp/grpc_gcp"
// )
apiConfig := &configpb.ApiConfig{
ChannelPool: &configpb.ChannelPoolConfig{
MaxSize: 4,
MaxConcurrentStreamsLowWatermark: 50,
},
Method: []*configpb.MethodConfig{
&configpb.MethodConfig{
Name: []string{"/some.api.v1/Method1"},
Affinity: &configpb.AffinityConfig{
Command: configpb.AffinityConfig_BIND,
AffinityKey: "key1",
},
},
&configpb.MethodConfig{
Name: []string{"/some.api.v1/Method2"},
Affinity: &configpb.AffinityConfig{
Command: configpb.AffinityConfig_BOUND,
AffinityKey: "key2",
},
},
&configpb.MethodConfig{
Name: []string{"/some.api.v1/Method3"},
Affinity: &configpb.AffinityConfig{
Command: configpb.AffinityConfig_UNBIND,
AffinityKey: "key3",
},
},
},
}
c, err := protojson.Marshal(apiConfig)
if err != nil {
t.Fatalf("cannot json encode config: %v", err)
}
jsonCfg := string(c)
2. Make ClientConn with specific DialOptions to enable grpc_gcp load balancer
with provided configuration. And specify gRPC-GCP interceptors.
conn, err := grpc.Dial(
target,
// Register and specify the grpc-gcp load balancer.
grpc.WithDisableServiceConfig(),
grpc.WithDefaultServiceConfig(
fmt.Sprintf(
`{"loadBalancingConfig": [{"%s":%s}]}`,
grpcgcp.Name,
jsonCfg,
),
),
// Set grpcgcp interceptors.
grpc.WithUnaryInterceptor(grpcgcp.GCPUnaryClientInterceptor),
grpc.WithStreamInterceptor(grpcgcp.GCPStreamClientInterceptor),
)
*/
package grpcgcp // import "github.com/GoogleCloudPlatform/grpc-gcp-go/grpcgcp"