/
client.go
233 lines (197 loc) · 5.02 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
package rpcd
import (
"errors"
"fmt"
"io"
"net"
"sync"
"time"
"github.com/imkouga/gocore/loger"
)
const (
doneChannelUnbufferedMsg = "rpcd.Client.Go: done channel is unbuffered"
)
// ServerError represents an error that has been returned from
// the remote side of the RPC connection.
type ServerError string
func (e ServerError) Error() string {
return string(e)
}
var ErrShutdown = errors.New("connection is shut down")
type Call struct {
ServiceMethod string // The name of the service and method to call.
Args interface{} // The argument to the function (*struct).
Reply interface{} // The reply from the function (*struct).
Error error // After completion, the error status.
Done chan *Call // Receives *Call when Go is complete.
}
type Client struct {
codec ClientCodec
reqMutex sync.Mutex // protects following
request Request
mutex sync.Mutex // protects following
seq uint64
pending map[uint64]*Call
closed bool // user has called Close
shutdown bool // server has told us to stop
}
type ClientCodec interface {
WriteRequest(*Request, interface{}) error
ReadResponseHeader(*Response) error
ReadResponseBody(interface{}) error
Close() error
}
func Dial(network, address string, opts ...DialOption) (*Client, error) {
dialOpts := &dialOptions{
timeout: time.Second * 30,
}
for _, opt := range opts {
opt.apply(dialOpts)
}
conn, err := net.DialTimeout(network, address, dialOpts.timeout)
if err != nil {
return nil, err
}
codec := NewPbClientCodec(conn)
return NewClientWithCodec(codec), nil
}
func NewClientWithCodec(codec ClientCodec) *Client {
client := &Client{
codec: codec,
pending: make(map[uint64]*Call),
}
go client.input()
return client
}
func (client *Client) send(call *Call) {
client.reqMutex.Lock()
defer client.reqMutex.Unlock()
if client.ClosedOrShutdowned() {
call.Error = ErrShutdown
call.done()
return
}
// Register this call.
client.mutex.Lock()
seq := client.seq
client.seq++
client.pending[seq] = call
client.mutex.Unlock()
// Encode and send the request.
client.request.Seq = seq
client.request.ServiceMethod = call.ServiceMethod
err := client.codec.WriteRequest(&client.request, call.Args)
if err != nil {
client.mutex.Lock()
call = client.pending[seq]
delete(client.pending, seq)
client.mutex.Unlock()
if call != nil {
call.Error = err
call.done()
}
}
}
func (client *Client) input() {
var err error
var response Response
for err == nil {
response = Response{}
err = client.codec.ReadResponseHeader(&response)
if err != nil {
break
}
seq := response.Seq
client.mutex.Lock()
call := client.pending[seq]
delete(client.pending, seq)
client.mutex.Unlock()
switch {
case call == nil:
err = client.codec.ReadResponseBody(nil)
if err != nil {
err = fmt.Errorf("rpcd.Client.input: reading error body[%s]", err.Error())
}
case response.Error != "":
call.Error = ServerError(response.Error)
err = client.codec.ReadResponseBody(nil)
if err != nil {
err = fmt.Errorf("rpcd.Client.input: reading error body[%s]", err.Error())
}
call.done()
default:
err = client.codec.ReadResponseBody(call.Reply)
if err != nil {
call.Error = fmt.Errorf("rpcd.Client.input: reading body[%s]", err.Error())
}
call.done()
}
}
// Terminate pending calls.
client.reqMutex.Lock()
client.mutex.Lock()
client.shutdown = true
closed := client.closed
if err == io.EOF {
if err = ErrShutdown; !closed {
err = io.ErrUnexpectedEOF
}
}
for _, call := range client.pending {
call.Error = err
call.done()
}
client.mutex.Unlock()
client.reqMutex.Unlock()
if err != io.EOF && !closed {
loger.Error("rpcd.Client.input: client protocol error:", err)
}
}
func (call *Call) done() {
select {
case call.Done <- call:
// ok
default:
loger.Warn("rpcd.Call.done: discarding Call reply due to insufficient Done chan capacity")
}
}
func (client *Client) Close() error {
client.mutex.Lock()
if client.closed {
client.mutex.Unlock()
return ErrShutdown
}
client.closed = true
client.mutex.Unlock()
return client.codec.Close()
}
func (client *Client) Go(serviceMethod string, args interface{}, reply interface{}, done chan *Call) *Call {
call := new(Call)
call.ServiceMethod = serviceMethod
call.Args = args
call.Reply = reply
if done == nil {
done = make(chan *Call, 10) // buffered.
} else if cap(done) <= 0 {
call.Done = done
call.Error = errors.New(doneChannelUnbufferedMsg)
loger.Error(doneChannelUnbufferedMsg)
return call
}
call.Done = done
client.send(call)
return call
}
// Call invokes the named function, waits for it to complete, and returns its error status.
func (client *Client) Call(serviceMethod string, args interface{}, reply interface{}) error {
call := <-client.Go(serviceMethod, args, reply, make(chan *Call, 1)).Done
return call.Error
}
func (client *Client) ClosedOrShutdowned() bool {
if nil == client {
return true
}
client.mutex.Lock()
defer client.mutex.Unlock()
return client.closed || client.shutdown
}