-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.go
292 lines (264 loc) · 6.61 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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
package gorpc
import (
"bufio"
"context"
"encoding/json"
"errors"
"fmt"
"go-rpc/codec"
"io"
"net"
"net/http"
"strings"
"sync"
"time"
"github.com/sirupsen/logrus"
)
type Call struct {
Seq uint64
ServiceMethod string
Args interface{}
Reply interface{}
Error error
Done chan *Call
}
type Client struct {
cc codec.Codec
option *Option
sending sync.Mutex
header codec.Header
mu sync.Mutex
seq uint64
pending map[uint64]*Call
closing bool
shutdown bool
}
type clientResult struct {
client *Client
err error
}
type NewClientFunc func(conn net.Conn, option *Option) (client *Client, err error)
var _ io.Closer = (*Client)(nil)
var ShutDownErr = errors.New("connection is shut down")
func (call *Call) done() {
call.Done <- call
}
func (client *Client) Close() error {
client.mu.Lock()
defer client.mu.Unlock()
if client.closing || client.shutdown {
return ShutDownErr
}
client.closing = true
return client.cc.Close()
}
func (client *Client) IsOnWork() bool {
client.mu.Lock()
defer client.mu.Unlock()
return !client.closing && !client.shutdown
}
func (client *Client) registerCall(call *Call) (uint64, error) {
client.mu.Lock()
defer client.mu.Unlock()
if client.closing || client.shutdown {
return 0, ShutDownErr
}
call.Seq = client.seq
client.pending[call.Seq] = call
client.seq++
return call.Seq, nil
}
func (client *Client) removeCall(seq uint64) *Call {
client.mu.Lock()
defer client.mu.Unlock()
call := client.pending[seq]
delete(client.pending, seq)
return call
}
func (client *Client) terminateCalls(err error) {
client.sending.Lock()
defer client.sending.Unlock()
client.mu.Lock()
defer client.mu.Unlock()
client.shutdown = true
for _, call := range client.pending {
call.Error = err
call.done()
}
}
func (client *Client) recieve() {
var err error
for err == nil {
h := new(codec.Header)
if err = client.cc.ReadHeader(h); err != nil {
break
}
call := client.removeCall(h.Seq)
if call == nil {
err = client.cc.ReadBody(nil)
} else if h.Error != "" {
call.Error = errors.New(h.Error)
err = client.cc.ReadBody(nil)
call.done()
} else {
err = client.cc.ReadBody(call.Reply)
if err != nil {
call.Error = errors.New("fail to read body " + err.Error())
}
call.done()
}
}
client.terminateCalls(err)
}
func NewClient(conn net.Conn, option *Option) (*Client, error) {
codecFunc := codec.NewCodecFuncMap[option.CodecType]
if codecFunc == nil {
err := errors.New("invalid codectype" + string(option.CodecType))
logrus.Errorf("client error: %v", err)
return nil, err
}
if err := json.NewEncoder(conn).Encode(option); err != nil {
return nil, err
}
if err := json.NewDecoder(conn).Decode(option); err != nil {
return nil, err
}
enc := json.NewEncoder(conn)
err := enc.Encode(option)
if err != nil {
err := errors.New("fail to decode opion" + err.Error())
logrus.Error(err)
conn.Close()
return nil, err
}
return newClientCodec(codecFunc(conn), option), nil
}
func newClientCodec(cc codec.Codec, option *Option) *Client {
client := &Client{
seq: 1,
cc: cc,
option: option,
pending: make(map[uint64]*Call),
}
go client.recieve()
return client
}
func parseOption(options ...*Option) (*Option, error) {
if len(options) == 0 || options[0] == nil {
return DefaultOption, nil
}
if len(options) > 1 {
return nil, errors.New("option is more than one")
}
option := options[0]
option.MagicNumber = DefaultOption.MagicNumber
if option.CodecType == "" {
option.CodecType = DefaultOption.CodecType
}
return option, nil
}
func Dial(network, addr string, opts ...*Option) (client *Client, err error) {
return dialTimeout(NewClient, network, addr, opts...)
}
func dialTimeout(f NewClientFunc, network, address string, options ...*Option) (client *Client, error error) {
option, err := parseOption(options...)
if err != nil {
return nil, err
}
conn, err := net.DialTimeout(network, address, option.ConnectTimeout)
if err != nil {
return nil, err
}
defer func() {
if err != nil {
conn.Close()
}
}()
ch := make(chan clientResult)
go func() {
client, err := f(conn, option)
ch <- clientResult{client: client, err: err}
}()
if option.ConnectTimeout == 0 {
result := <-ch
return result.client, result.err
}
select {
case <-time.After(option.ConnectTimeout):
logrus.Error("timeout")
return nil, errors.New("client: connection time out, expected within" + option.ConnectTimeout.String())
case result := <-ch:
return result.client, result.err
}
}
func (client *Client) send(call *Call) {
client.sending.Lock()
defer client.sending.Unlock()
id, err := client.registerCall(call)
if err != nil {
call.Error = err
call.done()
return
}
client.header.ServiceMethod = call.ServiceMethod
client.header.Seq = id
client.header.Error = ""
if err := client.cc.Write(&client.header, call.Args); err != nil {
call := client.removeCall(id)
if call != nil {
call.Error = err
call.done()
}
}
}
func (client *Client) Go(serviceMethod string, args, reply interface{}, done chan *Call) *Call {
if done == nil {
done = make(chan *Call, 10)
} else if cap(done) == 0 {
logrus.Panic("client error: done channel has no capacity")
}
call := &Call{
ServiceMethod: serviceMethod,
Args: args,
Reply: reply,
Done: done,
}
client.send(call)
return call
}
func (client *Client) Call(contex context.Context, serviceMethod string, args, reply interface{}) error {
call := client.Go(serviceMethod, args, reply, make(chan *Call, 1))
select {
case <-contex.Done():
return errors.New("client: call timeout: " + contex.Err().Error())
case call := <-call.Done:
return call.Error
}
}
func NewHTTPClient(conn net.Conn, option *Option) (*Client, error) {
io.WriteString(conn, fmt.Sprintf("CONNECT %s HTTP/1.0\n\n", defaultRPCPath))
respons, err := http.ReadResponse(bufio.NewReader(conn), &http.Request{Method: "CONNECT"})
if err == nil && respons.Status == connected {
return NewClient(conn, option)
}
if err == nil {
err = errors.New("unexpected http status: " + respons.Status)
}
return nil, err
}
func DialHTTP(network, addr string, option ...*Option) (*Client, error) {
return dialTimeout(NewHTTPClient, network, addr, option...)
}
func XDial(rpcAddr string, options ...*Option) (*Client, error) {
parts := strings.Split(rpcAddr, "@")
if len(parts) != 2 {
return nil, errors.New("invalid address formate: " + rpcAddr + ". expected protocal@address")
}
protocal := parts[0]
addr := parts[1]
if protocal == "http" {
return DialHTTP("tcp", addr, options...)
} else {
return Dial(protocal, addr, options...)
}
}