forked from vitessio/vitess
-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.go
73 lines (60 loc) · 1.94 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
// Copyright 2014, Google Inc. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// Package grpcvtctlclient contains the gRPC version of the vtctl client protocol
package grpcvtctlclient
import (
"time"
"github.com/youtube/vitess/go/vt/logutil"
"github.com/youtube/vitess/go/vt/vtctl/vtctlclient"
"golang.org/x/net/context"
"google.golang.org/grpc"
logutilpb "github.com/youtube/vitess/go/vt/proto/logutil"
vtctldatapb "github.com/youtube/vitess/go/vt/proto/vtctldata"
vtctlservicepb "github.com/youtube/vitess/go/vt/proto/vtctlservice"
)
type gRPCVtctlClient struct {
cc *grpc.ClientConn
c vtctlservicepb.VtctlClient
}
func gRPCVtctlClientFactory(addr string, dialTimeout time.Duration) (vtctlclient.VtctlClient, error) {
// create the RPC client
cc, err := grpc.Dial(addr, grpc.WithInsecure(), grpc.WithBlock(), grpc.WithTimeout(dialTimeout))
if err != nil {
return nil, err
}
c := vtctlservicepb.NewVtctlClient(cc)
return &gRPCVtctlClient{
cc: cc,
c: c,
}, nil
}
type eventStreamAdapter struct {
stream vtctlservicepb.Vtctl_ExecuteVtctlCommandClient
}
func (e *eventStreamAdapter) Recv() (*logutilpb.Event, error) {
le, err := e.stream.Recv()
if err != nil {
return nil, err
}
return le.Event, nil
}
// ExecuteVtctlCommand is part of the VtctlClient interface
func (client *gRPCVtctlClient) ExecuteVtctlCommand(ctx context.Context, args []string, actionTimeout time.Duration) (logutil.EventStream, error) {
query := &vtctldatapb.ExecuteVtctlCommandRequest{
Args: args,
ActionTimeout: int64(actionTimeout.Nanoseconds()),
}
stream, err := client.c.ExecuteVtctlCommand(ctx, query)
if err != nil {
return nil, err
}
return &eventStreamAdapter{stream}, nil
}
// Close is part of the VtctlClient interface
func (client *gRPCVtctlClient) Close() {
client.cc.Close()
}
func init() {
vtctlclient.RegisterFactory("grpc", gRPCVtctlClientFactory)
}