vitess.io/vitess@v0.16.2/go/vt/vtctl/grpcvtctlclient/client.go (about) 1 /* 2 Copyright 2019 The Vitess Authors. 3 4 Licensed under the Apache License, Version 2.0 (the "License"); 5 you may not use this file except in compliance with the License. 6 You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10 Unless required by applicable law or agreed to in writing, software 11 distributed under the License is distributed on an "AS IS" BASIS, 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 See the License for the specific language governing permissions and 14 limitations under the License. 15 */ 16 17 // Package grpcvtctlclient contains the gRPC version of the vtctl client protocol 18 package grpcvtctlclient 19 20 import ( 21 "time" 22 23 "context" 24 25 "google.golang.org/grpc" 26 27 "vitess.io/vitess/go/vt/grpcclient" 28 "vitess.io/vitess/go/vt/logutil" 29 "vitess.io/vitess/go/vt/vtctl/grpcclientcommon" 30 "vitess.io/vitess/go/vt/vtctl/vtctlclient" 31 32 logutilpb "vitess.io/vitess/go/vt/proto/logutil" 33 vtctldatapb "vitess.io/vitess/go/vt/proto/vtctldata" 34 vtctlservicepb "vitess.io/vitess/go/vt/proto/vtctlservice" 35 ) 36 37 type gRPCVtctlClient struct { 38 cc *grpc.ClientConn 39 c vtctlservicepb.VtctlClient 40 } 41 42 func gRPCVtctlClientFactory(addr string) (vtctlclient.VtctlClient, error) { 43 opt, err := grpcclientcommon.SecureDialOption() 44 if err != nil { 45 return nil, err 46 } 47 // create the RPC client 48 cc, err := grpcclient.Dial(addr, grpcclient.FailFast(false), opt) 49 if err != nil { 50 return nil, err 51 } 52 c := vtctlservicepb.NewVtctlClient(cc) 53 54 return &gRPCVtctlClient{ 55 cc: cc, 56 c: c, 57 }, nil 58 } 59 60 type eventStreamAdapter struct { 61 stream vtctlservicepb.Vtctl_ExecuteVtctlCommandClient 62 } 63 64 func (e *eventStreamAdapter) Recv() (*logutilpb.Event, error) { 65 le, err := e.stream.Recv() 66 if err != nil { 67 return nil, err 68 } 69 return le.Event, nil 70 } 71 72 // ExecuteVtctlCommand is part of the VtctlClient interface 73 func (client *gRPCVtctlClient) ExecuteVtctlCommand(ctx context.Context, args []string, actionTimeout time.Duration) (logutil.EventStream, error) { 74 query := &vtctldatapb.ExecuteVtctlCommandRequest{ 75 Args: args, 76 ActionTimeout: int64(actionTimeout.Nanoseconds()), 77 } 78 79 stream, err := client.c.ExecuteVtctlCommand(ctx, query) 80 if err != nil { 81 return nil, err 82 } 83 return &eventStreamAdapter{stream}, nil 84 } 85 86 // Close is part of the VtctlClient interface 87 func (client *gRPCVtctlClient) Close() { 88 client.cc.Close() 89 } 90 91 func init() { 92 vtctlclient.RegisterFactory("grpc", gRPCVtctlClientFactory) 93 }