-
Notifications
You must be signed in to change notification settings - Fork 2k
/
grpcthrottlerclient.go
126 lines (106 loc) · 4.09 KB
/
grpcthrottlerclient.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
/*
Copyright 2017 Google Inc.
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 grpcthrottlerclient contains the gRPC version of the throttler client protocol.
package grpcthrottlerclient
import (
"flag"
"golang.org/x/net/context"
"google.golang.org/grpc"
"vitess.io/vitess/go/vt/grpcclient"
"vitess.io/vitess/go/vt/throttler/throttlerclient"
"vitess.io/vitess/go/vt/vterrors"
throttlerdatapb "vitess.io/vitess/go/vt/proto/throttlerdata"
throttlerservicepb "vitess.io/vitess/go/vt/proto/throttlerservice"
)
var (
cert = flag.String("throttler_client_grpc_cert", "", "the cert to use to connect")
key = flag.String("throttler_client_grpc_key", "", "the key to use to connect")
ca = flag.String("throttler_client_grpc_ca", "", "the server ca to use to validate servers when connecting")
name = flag.String("throttler_client_grpc_server_name", "", "the server name to use to validate server certificate")
)
type client struct {
conn *grpc.ClientConn
gRPCClient throttlerservicepb.ThrottlerClient
}
func factory(addr string) (throttlerclient.Client, error) {
opt, err := grpcclient.SecureDialOption(*cert, *key, *ca, *name)
if err != nil {
return nil, err
}
conn, err := grpcclient.Dial(addr, grpcclient.FailFast(false), opt)
if err != nil {
return nil, err
}
gRPCClient := throttlerservicepb.NewThrottlerClient(conn)
return &client{conn, gRPCClient}, nil
}
// MaxRates is part of the throttlerclient.Client interface and returns the
// current max rate for each throttler of the process.
func (c *client) MaxRates(ctx context.Context) (map[string]int64, error) {
response, err := c.gRPCClient.MaxRates(ctx, &throttlerdatapb.MaxRatesRequest{})
if err != nil {
return nil, vterrors.FromGRPC(err)
}
return response.Rates, nil
}
// SetMaxRate is part of the throttlerclient.Client interface and sets the rate
// on all throttlers of the server.
func (c *client) SetMaxRate(ctx context.Context, rate int64) ([]string, error) {
request := &throttlerdatapb.SetMaxRateRequest{
Rate: rate,
}
response, err := c.gRPCClient.SetMaxRate(ctx, request)
if err != nil {
return nil, vterrors.FromGRPC(err)
}
return response.Names, nil
}
// GetConfiguration is part of the throttlerclient.Client interface.
func (c *client) GetConfiguration(ctx context.Context, throttlerName string) (map[string]*throttlerdatapb.Configuration, error) {
response, err := c.gRPCClient.GetConfiguration(ctx, &throttlerdatapb.GetConfigurationRequest{
ThrottlerName: throttlerName,
})
if err != nil {
return nil, vterrors.FromGRPC(err)
}
return response.Configurations, nil
}
// UpdateConfiguration is part of the throttlerclient.Client interface.
func (c *client) UpdateConfiguration(ctx context.Context, throttlerName string, configuration *throttlerdatapb.Configuration, copyZeroValues bool) ([]string, error) {
response, err := c.gRPCClient.UpdateConfiguration(ctx, &throttlerdatapb.UpdateConfigurationRequest{
ThrottlerName: throttlerName,
Configuration: configuration,
CopyZeroValues: copyZeroValues,
})
if err != nil {
return nil, vterrors.FromGRPC(err)
}
return response.Names, nil
}
// ResetConfiguration is part of the throttlerclient.Client interface.
func (c *client) ResetConfiguration(ctx context.Context, throttlerName string) ([]string, error) {
response, err := c.gRPCClient.ResetConfiguration(ctx, &throttlerdatapb.ResetConfigurationRequest{
ThrottlerName: throttlerName,
})
if err != nil {
return nil, vterrors.FromGRPC(err)
}
return response.Names, nil
}
// Close is part of the throttlerclient.Client interface.
func (c *client) Close() {
c.conn.Close()
}
func init() {
throttlerclient.RegisterFactory("grpc", factory)
}