forked from vitessio/vitess
-
Notifications
You must be signed in to change notification settings - Fork 13
/
interface.go
75 lines (60 loc) · 2.52 KB
/
interface.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
/*
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 vtctlclient contains the generic client side of the remote vtctl protocol.
package vtctlclient
import (
"flag"
"fmt"
"time"
"golang.org/x/net/context"
"vitess.io/vitess/go/vt/log"
"vitess.io/vitess/go/vt/logutil"
)
// vtctlClientProtocol specifices which RPC client implementation should be used.
var vtctlClientProtocol = flag.String("vtctl_client_protocol", "grpc", "the protocol to use to talk to the vtctl server")
// VtctlClient defines the interface used to send remote vtctl commands
type VtctlClient interface {
// ExecuteVtctlCommand will execute the command remotely
ExecuteVtctlCommand(ctx context.Context, args []string, actionTimeout time.Duration) (logutil.EventStream, error)
// Close will terminate the connection. This object won't be
// used after this.
Close()
}
// Factory functions are registered by client implementations
type Factory func(addr string) (VtctlClient, error)
var factories = make(map[string]Factory)
// RegisterFactory allows a client implementation to register itself.
func RegisterFactory(name string, factory Factory) {
if _, ok := factories[name]; ok {
log.Fatalf("RegisterFactory: %s already exists", name)
}
factories[name] = factory
}
// UnregisterFactoryForTest allows to unregister a client implementation from the static map.
// This function is used by unit tests to cleanly unregister any fake implementations.
// This way, a test package can use the same name for different fakes and no dangling fakes are
// left behind in the static factories map after the test.
func UnregisterFactoryForTest(name string) {
if _, ok := factories[name]; !ok {
log.Fatalf("UnregisterFactoryForTest: %s is not registered", name)
}
delete(factories, name)
}
// New allows a user of the client library to get its implementation.
func New(addr string) (VtctlClient, error) {
factory, ok := factories[*vtctlClientProtocol]
if !ok {
return nil, fmt.Errorf("unknown vtctl client protocol: %v", *vtctlClientProtocol)
}
return factory(addr)
}