forked from vitessio/vitess
-
Notifications
You must be signed in to change notification settings - Fork 1
/
interface.go
55 lines (44 loc) · 1.92 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
// Copyright 2015, 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 vtworkerclient contains the generic client side of the remote vtworker protocol.
package vtworkerclient
import (
"flag"
"fmt"
"time"
log "github.com/golang/glog"
"github.com/youtube/vitess/go/vt/logutil"
"golang.org/x/net/context"
)
// VtworkerClientProtocol specifices which RPC client implementation should be used.
var VtworkerClientProtocol = flag.String("vtworker_client_protocol", "grpc", "the protocol to use to talk to the vtworker server")
// ErrFunc is returned by streaming queries to get the error
type ErrFunc func() error
// VtworkerClient defines the interface used to send remote vtworker commands
type VtworkerClient interface {
// ExecuteVtworkerCommand will execute the command remotely.
// NOTE: ErrFunc should only be checked after the returned channel was closed to avoid races.
ExecuteVtworkerCommand(ctx context.Context, args []string) (<-chan *logutil.LoggerEvent, ErrFunc, 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, connectTimeout time.Duration) (VtworkerClient, 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
}
// New allows a user of the client library to get its implementation.
func New(addr string, connectTimeout time.Duration) (VtworkerClient, error) {
factory, ok := factories[*VtworkerClientProtocol]
if !ok {
return nil, fmt.Errorf("unknown vtworker client protocol: %v", *VtworkerClientProtocol)
}
return factory(addr, connectTimeout)
}