-
Notifications
You must be signed in to change notification settings - Fork 3
/
client.go
97 lines (84 loc) · 1.94 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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
package client
import (
"context"
"github.com/quanterall/kitchensink/pkg/proto"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials/insecure"
"time"
)
func New(serverAddr string, timeout time.Duration) (
client *b32c, err error,
) {
client = &b32c{
addr: serverAddr,
encChan: make(chan encReq, 1),
encRes: make(chan *proto.EncodeResponse),
decChan: make(chan decReq, 1),
decRes: make(chan *proto.DecodeResponse),
timeout: timeout,
waitingEnc: make(map[time.Time]encReq),
waitingDec: make(map[time.Time]decReq),
}
return
}
// Start up the client. Call b.cancel() to stop.
//
// The returned send and recv functions are async by default
// but can be used synchronously by receiving from them directly:
//
// encRes := <-enc(req)
// decRes := <-dec(req)
func (b *b32c) Start() (
enc func(*proto.EncodeRequest) chan *proto.EncodeResponse,
dec func(*proto.DecodeRequest) chan *proto.DecodeResponse,
stop func(),
err error,
) {
// Dial the configured server address
clientConn, err := grpc.Dial(
b.addr,
grpc.WithTransportCredentials(insecure.NewCredentials()),
)
if err != nil {
return
}
cli := proto.NewTranscriberClient(clientConn)
ctx, cancelFunc := context.WithCancel(context.Background())
b.stop = ctx.Done()
var encode proto.Transcriber_EncodeClient
encode, err = cli.Encode(ctx)
if err != nil {
cancelFunc()
return
}
var decode proto.Transcriber_DecodeClient
decode, err = cli.Decode(ctx)
if err != nil {
cancelFunc()
return
}
go func() {
err := b.Decode(decode)
if err != nil {
log.Print(err)
}
}()
go func() {
err := b.Encode(encode)
if err != nil {
log.Print(err)
}
}()
enc = func(req *proto.EncodeRequest) chan *proto.EncodeResponse {
r := newEncReq(req)
b.encChan <- r
return r.Res
}
dec = func(req *proto.DecodeRequest) chan *proto.DecodeResponse {
r := newDecReq(req)
b.decChan <- r
return r.Res
}
stop = cancelFunc
return
}