-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathclient.go
221 lines (192 loc) · 4.6 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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
package chanserv
import (
"errors"
"fmt"
"io"
"net"
"time"
)
type client struct {
mpx Multiplexer
onError func(err error)
srcBufSize int
frameBufSize int
timeouts clientTimeouts
}
type clientTimeouts struct {
dialTimeout time.Duration
masterReadTimeout time.Duration
masterWriteTimeout time.Duration
frameReadTimeout time.Duration
}
// NewClient initializes a new client using the provided multiplexer for
// the network capabilities. Refer to the client options if you want to specify
// timeouts and error callbacks.
func NewClient(mpx Multiplexer, opts ...ClientOption) Client {
cli := client{
mpx: mpx,
onError: func(err error) {}, // no report
srcBufSize: 128,
frameBufSize: 1024,
timeouts: clientTimeouts{
dialTimeout: 10 * time.Second,
},
}
for _, o := range opts {
o(&cli)
}
return cli
}
type source struct {
header []byte
metaData metaData
out chan Frame
}
func (s source) Header() []byte {
return s.header
}
func (s source) Meta() MetaData {
return s.metaData
}
func (s source) Out() <-chan Frame {
return s.out
}
type metaData struct {
localAddr string
localNetwork string
remoteAddr string
remoteNetwork string
}
func (m metaData) RemoteAddr() string {
return m.remoteAddr
}
func (m metaData) RemoteNetwork() string {
return m.remoteNetwork
}
func (m metaData) LocalAddr() string {
return m.localAddr
}
func (m metaData) LocalNetwork() string {
return m.localNetwork
}
type frame []byte
func (f frame) Bytes() []byte {
return []byte(f)
}
func (c client) LookupAndPost(vAddr string,
body []byte, tags map[RequestTag]string) (<-chan Source, error) {
retErr := func(err error) (<-chan Source, error) {
srcChan := make(chan Source)
close(srcChan)
return srcChan, err
}
if c.mpx == nil {
return retErr(errors.New("chanserv: mpx not set"))
}
var svcBucket string // TODO: handle other tags
if bucket, ok := tags[TagBucket]; ok && len(bucket) > 0 {
svcBucket = fmt.Sprintf("registry://%s", bucket)
}
conn, err := c.mpx.DialTimeout(svcBucket, vAddr, c.timeouts.dialTimeout)
if err != nil {
return retErr(err)
}
return c.post(conn, body)
}
func (c client) post(conn net.Conn, body []byte) (<-chan Source, error) {
if d := c.timeouts.masterWriteTimeout; d > 0 {
conn.SetWriteDeadline(time.Now().Add(d))
}
retErr := func(err error) (<-chan Source, error) {
srcChan := make(chan Source)
close(srcChan)
return srcChan, err
}
if err := writeFrame(conn, body); err != nil {
err = fmt.Errorf("post writeFrame: %v", err)
return retErr(err)
}
if d := c.timeouts.masterReadTimeout; d > 0 {
conn.SetReadDeadline(time.Now().Add(d))
}
buf, err := readFrame(conn, false)
if err != nil {
if err != io.EOF {
err = fmt.Errorf("post readFrame: %v", err)
return retErr(err)
}
return retErr(nil)
}
srcChan := make(chan Source, c.srcBufSize)
go func() {
expectHeader := true
outChan := make(chan Frame, c.frameBufSize)
meta := metaData{
remoteAddr: conn.RemoteAddr().String(),
remoteNetwork: conn.RemoteAddr().Network(),
localAddr: conn.LocalAddr().String(),
localNetwork: conn.LocalAddr().Network(),
}
src := source{
out: outChan,
metaData: meta,
}
for err == nil {
if expectHeader {
src.header = buf
expectHeader = false
if d := c.timeouts.masterReadTimeout; d > 0 {
conn.SetReadDeadline(time.Now().Add(d))
}
buf, err = readFrame(conn, false)
continue
}
vAddr := string(buf)
go c.discover(vAddr, outChan)
srcChan <- src
expectHeader = true
outChan = make(chan Frame, c.frameBufSize)
src = source{
out: outChan,
metaData: meta,
}
if d := c.timeouts.masterReadTimeout; d > 0 {
conn.SetReadDeadline(time.Now().Add(d))
}
buf, err = readFrame(conn, false)
}
conn.Close()
close(srcChan)
if err != io.EOF {
err = fmt.Errorf("post readFrame: %v", err)
c.onError(err)
}
}()
return srcChan, nil
}
func (c client) discover(vAddr string, out chan<- Frame) {
defer close(out)
// channel discover currently uses no additional options
conn, err := c.mpx.DialTimeout("", vAddr, c.timeouts.dialTimeout)
if err != nil {
err = fmt.Errorf("discover mpx.DialTimeout: %v", err)
c.onError(err)
return
}
defer conn.Close()
if d := c.timeouts.frameReadTimeout; d > 0 {
conn.SetReadDeadline(time.Now().Add(d))
}
buf, err := readFrame(conn, true)
for err == nil {
out <- frame(buf)
if d := c.timeouts.frameReadTimeout; d > 0 {
conn.SetReadDeadline(time.Now().Add(d))
}
buf, err = readFrame(conn, true)
}
if err != io.EOF {
err = fmt.Errorf("discover readFrame: %v", err)
c.onError(err)
}
}