forked from fperf/fperf
/
client.go
71 lines (59 loc) · 1.5 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
package fperf
import (
"flag"
"golang.org/x/net/context"
)
//NewClientFunc defines the function type a client should implement
type NewClientFunc func(*FlagSet) Client
//FlagSet combines the standard flag.FlagSet, this can be used to parse args by the client
type FlagSet struct {
*flag.FlagSet
}
var clients = make(map[string]NewClientFunc)
var descriptions = make(map[string]string)
//Parse the command line args
func (f *FlagSet) Parse() {
f.FlagSet.Parse(flag.Args()[1:])
}
//NewClient create a client by the name it registered
func NewClient(name string) Client {
if c := clients[name]; c != nil {
subcmd := &FlagSet{flag.NewFlagSet(flag.Arg(0), flag.ExitOnError)}
return c(subcmd)
}
return nil
}
//Register attatch a client to fperf
func Register(name string, f NewClientFunc, desc ...string) {
clients[name] = f
if len(desc) > 0 {
descriptions[name] = desc[0]
}
}
//AllClients return the client name and its description
func AllClients() map[string]string {
m := make(map[string]string)
for k := range clients {
m[k] = descriptions[k]
}
return m
}
//Client use Dial to connect to the server
type Client interface {
Dial(addr string) error
}
//UnaryClient defines the request-reply access model
type UnaryClient interface {
Client
Request() error
}
//StreamClient used to create a stream
type StreamClient interface {
Client
CreateStream(ctx context.Context) (Stream, error)
}
//Stream use DoSend/DoRecv to send/recv data or message
type Stream interface {
DoSend() error
DoRecv() error
}