-
Notifications
You must be signed in to change notification settings - Fork 2k
/
client.go
50 lines (40 loc) · 1.48 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
// Package vtctldclient contains the generic client side of the remote vtctld
// protocol.
package vtctldclient
import (
"fmt"
"log"
vtctlservicepb "vitess.io/vitess/go/vt/proto/vtctlservice"
)
type VtctldClient interface {
vtctlservicepb.VtctldClient
// Close augments the vtctlservicepb.VtctlClient interface with io.Closer.
Close() error
}
// Factory is a function that creates new VtctldClients.
type Factory func(addr string) (VtctldClient, error)
var registry = map[string]Factory{}
// Register adds a VtctldClient factory for the given name (protocol).
// Attempting to register multiple factories for the same protocol is a fatal
// error.
func Register(name string, factory Factory) {
if _, ok := registry[name]; ok {
log.Fatalf("Register: %s already registered", name)
}
registry[name] = factory
}
// New returns a VtctldClient for the given protocol, connected to a
// VtctldServer on the given addr. This function returns an error if no client
// factory was registered for the given protocol.
//
// This is a departure from vtctlclient's New, which relies on a flag in the
// global namespace to determine the protocol to use. Instead, we require
// users to specify their own flag in their own (hopefully not global) namespace
// to determine the protocol to pass into here.
func New(protocol string, addr string) (VtctldClient, error) {
factory, ok := registry[protocol]
if !ok {
return nil, fmt.Errorf("unknown vtctld client protocol: %s", protocol)
}
return factory(addr)
}