-
Notifications
You must be signed in to change notification settings - Fork 2.1k
/
client.go
80 lines (68 loc) · 1.99 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
74
75
76
77
78
79
80
// 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 (
"io"
"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"
pb "github.com/youtube/vitess/go/vt/proto/vtctldata"
pbs "github.com/youtube/vitess/go/vt/proto/vtctlservice"
)
type gRPCVtctlClient struct {
cc *grpc.ClientConn
c pbs.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 := pbs.NewVtctlClient(cc)
return &gRPCVtctlClient{
cc: cc,
c: c,
}, nil
}
// ExecuteVtctlCommand is part of the VtctlClient interface
func (client *gRPCVtctlClient) ExecuteVtctlCommand(ctx context.Context, args []string, actionTimeout, lockTimeout time.Duration) (<-chan *logutil.LoggerEvent, vtctlclient.ErrFunc, error) {
query := &pb.ExecuteVtctlCommandRequest{
Args: args,
ActionTimeout: int64(actionTimeout.Nanoseconds()),
LockTimeout: int64(lockTimeout.Nanoseconds()),
}
stream, err := client.c.ExecuteVtctlCommand(ctx, query)
if err != nil {
return nil, nil, err
}
results := make(chan *logutil.LoggerEvent, 1)
var finalError error
go func() {
for {
le, err := stream.Recv()
if err != nil {
if err != io.EOF {
finalError = err
}
close(results)
return
}
results <- logutil.ProtoToLoggerEvent(le.Event)
}
}()
return results, func() error {
return finalError
}, nil
}
// Close is part of the VtctlClient interface
func (client *gRPCVtctlClient) Close() {
client.cc.Close()
}
func init() {
vtctlclient.RegisterFactory("grpc", gRPCVtctlClientFactory)
}