forked from juju/juju
-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.go
150 lines (133 loc) · 3.64 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
// Copyright 2012, 2013 Canonical Ltd.
// Licensed under the AGPLv3, see LICENCE file for details.
package rpc
import (
"strings"
"github.com/juju/errors"
)
var ErrShutdown = errors.New("connection is shut down")
func IsShutdownErr(err error) bool {
return errors.Cause(err) == ErrShutdown
}
// Call represents an active RPC.
type Call struct {
Request
Params interface{}
Response interface{}
Error error
Done chan *Call
}
// RequestError represents an error returned from an RPC request.
type RequestError struct {
Message string
Code string
}
func (e *RequestError) Error() string {
if e.Code != "" {
return e.Message + " (" + e.Code + ")"
}
return e.Message
}
func (e *RequestError) ErrorCode() string {
return e.Code
}
func (conn *Conn) send(call *Call) {
conn.sending.Lock()
defer conn.sending.Unlock()
// Register this call.
conn.mutex.Lock()
if conn.dead == nil {
panic("rpc: call made when connection not started")
}
if conn.closing || conn.shutdown {
call.Error = ErrShutdown
conn.mutex.Unlock()
call.done()
return
}
conn.reqId++
reqId := conn.reqId
conn.clientPending[reqId] = call
conn.mutex.Unlock()
// Encode and send the request.
hdr := &Header{
RequestId: reqId,
Request: call.Request,
Version: 1,
}
params := call.Params
if params == nil {
params = struct{}{}
}
if err := conn.codec.WriteMessage(hdr, params); err != nil {
conn.mutex.Lock()
call = conn.clientPending[reqId]
delete(conn.clientPending, reqId)
conn.mutex.Unlock()
if call != nil {
call.Error = err
call.done()
}
}
}
func (conn *Conn) handleResponse(hdr *Header) error {
reqId := hdr.RequestId
conn.mutex.Lock()
call := conn.clientPending[reqId]
delete(conn.clientPending, reqId)
conn.mutex.Unlock()
var err error
switch {
case call == nil:
// We've got no pending call. That usually means that
// WriteHeader partially failed, and call was already
// removed; response is a server telling us about an
// error reading request body. We should still attempt
// to read error body, but there's no one to give it to.
err = conn.readBody(nil, false)
case hdr.Error != "":
// Report rpcreflect.NoSuchMethodError with CodeNotImplemented.
if strings.HasPrefix(hdr.Error, "no such request ") && hdr.ErrorCode == "" {
hdr.ErrorCode = codeNotImplemented
}
// We've got an error response. Give this to the request;
// any subsequent requests will get the ReadResponseBody
// error if there is one.
call.Error = &RequestError{
Message: hdr.Error,
Code: hdr.ErrorCode,
}
err = conn.readBody(nil, false)
call.done()
default:
err = conn.readBody(call.Response, false)
call.done()
}
return errors.Annotate(err, "error handling response")
}
func (call *Call) done() {
select {
case call.Done <- call:
// ok
default:
// We don't want to block here. It is the caller's responsibility to make
// sure the channel has enough buffer space. See comment in Go().
logger.Errorf("discarding Call reply due to insufficient Done chan capacity")
}
}
// Call invokes the named action on the object of the given type with the given
// id. The returned values will be stored in response, which should be a pointer.
// If the action fails remotely, the error will have a cause of type RequestError.
// The params value may be nil if no parameters are provided; the response value
// may be nil to indicate that any result should be discarded.
func (conn *Conn) Call(req Request, params, response interface{}) error {
call := &Call{
Request: req,
Params: params,
Response: response,
Done: make(chan *Call, 1),
}
conn.send(call)
result := <-call.Done
return errors.Trace(result.Error)
}