forked from vitessio/vitess
/
client.go
78 lines (63 loc) · 2.02 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
// 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 grpcmysqlctlclient contains the gRPC1 version of the mysqlctl
// client protocol.
package grpcmysqlctlclient
import (
"net"
"time"
"google.golang.org/grpc"
"golang.org/x/net/context"
"github.com/youtube/vitess/go/vt/mysqlctl/mysqlctlclient"
mysqlctlpb "github.com/youtube/vitess/go/vt/proto/mysqlctl"
)
type client struct {
cc *grpc.ClientConn
c mysqlctlpb.MysqlCtlClient
}
func factory(network, addr string, dialTimeout time.Duration) (mysqlctlclient.MysqlctlClient, error) {
// create the RPC client
cc, err := grpc.Dial(addr, grpc.WithInsecure(), grpc.WithBlock(), grpc.WithTimeout(dialTimeout), grpc.WithDialer(func(addr string, timeout time.Duration) (net.Conn, error) {
return net.DialTimeout(network, addr, timeout)
}))
if err != nil {
return nil, err
}
c := mysqlctlpb.NewMysqlCtlClient(cc)
return &client{
cc: cc,
c: c,
}, nil
}
// Start is part of the MysqlctlClient interface.
func (c *client) Start(ctx context.Context, mysqldArgs ...string) error {
_, err := c.c.Start(ctx, &mysqlctlpb.StartRequest{
MysqldArgs: mysqldArgs,
})
return err
}
// Shutdown is part of the MysqlctlClient interface.
func (c *client) Shutdown(ctx context.Context, waitForMysqld bool) error {
_, err := c.c.Shutdown(ctx, &mysqlctlpb.ShutdownRequest{
WaitForMysqld: waitForMysqld,
})
return err
}
// RunMysqlUpgrade is part of the MysqlctlClient interface.
func (c *client) RunMysqlUpgrade(ctx context.Context) error {
_, err := c.c.RunMysqlUpgrade(ctx, &mysqlctlpb.RunMysqlUpgradeRequest{})
return err
}
// ReinitConfig is part of the MysqlctlClient interface.
func (c *client) ReinitConfig(ctx context.Context) error {
_, err := c.c.ReinitConfig(ctx, &mysqlctlpb.ReinitConfigRequest{})
return err
}
// Close is part of the MysqlctlClient interface.
func (c *client) Close() {
c.cc.Close()
}
func init() {
mysqlctlclient.RegisterFactory("grpc", factory)
}