-
Notifications
You must be signed in to change notification settings - Fork 52
/
Copy pathgcp_interceptor.go
215 lines (191 loc) · 5.37 KB
/
gcp_interceptor.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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
package balancer
import (
"context"
"os"
"sync"
pb "github.com/bazelbuild/remote-apis-sdks/go/pkg/balancer/proto"
"google.golang.org/grpc"
"google.golang.org/protobuf/encoding/protojson"
)
const (
// Default max number of connections is 0, meaning "no limit"
defaultMaxConn = 0
// Default max stream watermark is 100, which is the current stream limit for GFE.
// Any value >100 will be rounded down to 100.
defaultMaxStream = 100
)
type key int
var gcpKey key
type poolConfig struct {
maxConn uint32
maxStream uint32
}
type gcpContext struct {
affinityCfg *pb.AffinityConfig
poolCfg *poolConfig
// request message used for pre-process of an affinity call
reqMsg interface{}
// response message used for post-process of an affinity call
replyMsg interface{}
}
// GCPInterceptor provides functions for intercepting client requests
// in order to support GCP specific features
type GCPInterceptor struct {
poolCfg *poolConfig
// Maps method path to AffinityConfig
methodToAffinity map[string]*pb.AffinityConfig
}
// NewGCPInterceptor creates a new GCPInterceptor with a given ApiConfig
func NewGCPInterceptor(config *pb.ApiConfig) *GCPInterceptor {
mp := make(map[string]*pb.AffinityConfig)
methodCfgs := config.GetMethod()
for _, methodCfg := range methodCfgs {
methodNames := methodCfg.GetName()
affinityCfg := methodCfg.GetAffinity()
if methodNames != nil && affinityCfg != nil {
for _, method := range methodNames {
mp[method] = affinityCfg
}
}
}
poolCfg := &poolConfig{
maxConn: defaultMaxConn,
maxStream: defaultMaxStream,
}
userPoolCfg := config.GetChannelPool()
// Set user defined MaxSize.
poolCfg.maxConn = userPoolCfg.GetMaxSize()
// Set user defined MaxConcurrentStreamsLowWatermark if ranged in [1, defaultMaxStream],
// otherwise use the defaultMaxStream.
watermarkValue := userPoolCfg.GetMaxConcurrentStreamsLowWatermark()
if watermarkValue >= 1 && watermarkValue <= defaultMaxStream {
poolCfg.maxStream = watermarkValue
}
return &GCPInterceptor{
poolCfg: poolCfg,
methodToAffinity: mp,
}
}
// GCPUnaryClientInterceptor intercepts the execution of a unary RPC
// and injects necessary information to be used by the picker.
func (gcpInt *GCPInterceptor) GCPUnaryClientInterceptor(
ctx context.Context,
method string,
req interface{},
reply interface{},
cc *grpc.ClientConn,
invoker grpc.UnaryInvoker,
opts ...grpc.CallOption,
) error {
affinityCfg, _ := gcpInt.methodToAffinity[method]
gcpCtx := &gcpContext{
affinityCfg: affinityCfg,
reqMsg: req,
replyMsg: reply,
poolCfg: gcpInt.poolCfg,
}
ctx = context.WithValue(ctx, gcpKey, gcpCtx)
return invoker(ctx, method, req, reply, cc, opts...)
}
// GCPStreamClientInterceptor intercepts the execution of a client streaming RPC
// and injects necessary information to be used by the picker.
func (gcpInt *GCPInterceptor) GCPStreamClientInterceptor(
ctx context.Context,
desc *grpc.StreamDesc,
cc *grpc.ClientConn,
method string,
streamer grpc.Streamer,
opts ...grpc.CallOption,
) (grpc.ClientStream, error) {
// This constructor does not create a real ClientStream,
// it only stores all parameters and let SendMsg() to create ClientStream.
affinityCfg, _ := gcpInt.methodToAffinity[method]
gcpCtx := &gcpContext{
affinityCfg: affinityCfg,
poolCfg: gcpInt.poolCfg,
}
ctx = context.WithValue(ctx, gcpKey, gcpCtx)
cs := &gcpClientStream{
gcpInt: gcpInt,
ctx: ctx,
desc: desc,
cc: cc,
method: method,
streamer: streamer,
opts: opts,
}
cs.cond = sync.NewCond(cs)
return cs, nil
}
type gcpClientStream struct {
sync.Mutex
grpc.ClientStream
cond *sync.Cond
initStreamErr error
gcpInt *GCPInterceptor
ctx context.Context
desc *grpc.StreamDesc
cc *grpc.ClientConn
method string
streamer grpc.Streamer
opts []grpc.CallOption
}
func (cs *gcpClientStream) SendMsg(m interface{}) error {
cs.Lock()
// Initialize underlying ClientStream when getting the first request.
if cs.ClientStream == nil {
affinityCfg, ok := cs.gcpInt.methodToAffinity[cs.method]
ctx := cs.ctx
if ok {
gcpCtx := &gcpContext{
affinityCfg: affinityCfg,
reqMsg: m,
poolCfg: cs.gcpInt.poolCfg,
}
ctx = context.WithValue(cs.ctx, gcpKey, gcpCtx)
}
realCS, err := cs.streamer(ctx, cs.desc, cs.cc, cs.method, cs.opts...)
if err != nil {
cs.initStreamErr = err
cs.Unlock()
cs.cond.Broadcast()
return err
}
cs.ClientStream = realCS
}
cs.Unlock()
cs.cond.Broadcast()
return cs.ClientStream.SendMsg(m)
}
func (cs *gcpClientStream) RecvMsg(m interface{}) error {
// If RecvMsg is called before SendMsg, it should wait until cs.ClientStream
// is initialized or the initialization failed.
cs.Lock()
for cs.initStreamErr == nil && cs.ClientStream == nil {
cs.cond.Wait()
}
if cs.initStreamErr != nil {
cs.Unlock()
return cs.initStreamErr
}
cs.Unlock()
return cs.ClientStream.RecvMsg(m)
}
func (cs *gcpClientStream) CloseSend() error {
cs.Lock()
defer cs.Unlock()
if cs.ClientStream != nil {
return cs.ClientStream.CloseSend()
}
return nil
}
// ParseAPIConfig parses a json config file into ApiConfig proto message.
func ParseAPIConfig(path string) (*pb.ApiConfig, error) {
jsonFile, err := os.ReadFile(path)
if err != nil {
return nil, err
}
result := &pb.ApiConfig{}
protojson.Unmarshal(jsonFile, result)
return result, nil
}