forked from porthos-rpc/porthos-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
call.go
173 lines (137 loc) · 3.72 KB
/
call.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
package porthos
import (
"encoding/json"
"strconv"
"time"
"github.com/porthos-rpc/porthos-go/log"
"github.com/streadway/amqp"
)
type call struct {
client *Client
timeout time.Duration
method string
body []byte
contentType string
}
// Map is an abstraction for map[string]interface{} to be used with WithMap.
type Map map[string]interface{}
// NewCall creates a new RPC call object.
func newCall(client *Client, method string) *call {
return &call{client: client, method: method}
}
// WithTimeout defines the timeut for this specific call.
func (c *call) WithTimeout(timeout time.Duration) *call {
c.timeout = timeout
return c
}
// WithBody defines the given bytes array as the request body.
func (c *call) WithBody(body []byte) *call {
c.body = body
c.contentType = "application/octet-stream"
return c
}
// WithBody defines the given bytes array as the request body.
func (c *call) WithBodyContentType(body []byte, contentType string) *call {
c.body = body
c.contentType = contentType
return c
}
// WitArgs defines the given args as the request body.
func (c *call) WithArgs(args ...interface{}) *call {
return c.withJSON(args)
}
// WithMap defines the given map as the request body.
func (c *call) WithMap(m map[string]interface{}) *call {
return c.withJSON(m)
}
// WithStruct defines the given struct as the request body.
func (c *call) WithStruct(i interface{}) *call {
return c.withJSON(i)
}
func (c *call) withJSON(i interface{}) *call {
data, err := json.Marshal(i)
if err != nil {
panic(err)
}
c.body = data
c.contentType = "application/json"
return c
}
// Async calls the remote method with the given arguments.
// It returns a *Slot (which contains the response channel) and any possible error.
func (c *call) Async() (Slot, error) {
res := NewSlot()
correlationID := res.getCorrelationID()
ch, err := c.client.broker.openChannel()
if err != nil {
return nil, err
}
defer ch.Close()
err = ch.Publish(
"", // exchange
c.client.serviceName, // routing key
false, // mandatory
false, // immediate
amqp.Publishing{
Headers: amqp.Table{
"X-Method": c.method,
},
Expiration: strconv.FormatInt(c.getTimeoutMilliseconds(), 10),
ContentType: c.contentType,
CorrelationId: correlationID,
ReplyTo: c.client.responseQueue.Name,
Body: c.body,
})
log.Info("Published method '%s' in '%s'. Expecting response in queue '%s' and slot '%d'", c.method, c.client.serviceName, c.client.responseQueue.Name, []byte(correlationID))
if err != nil {
return nil, err
}
return res, nil
}
// Sync calls the remote method with the given arguments.
// It returns a Response and any possible error.
func (c *call) Sync() (*ClientResponse, error) {
slot, err := c.Async()
if err != nil {
return nil, err
}
defer slot.Dispose()
select {
case response := <-slot.ResponseChannel():
return &response, nil
case <-time.After(c.getTimeout()):
return nil, ErrTimedOut
}
}
// Void calls a remote service procedure/service which will not provide any return value.
func (c *call) Void() error {
err := c.client.channel.Publish(
"", // exchange
c.client.serviceName, // routing key
false, // mandatory
false, // immediate
amqp.Publishing{
Headers: amqp.Table{
"X-Method": c.method,
},
ContentType: c.contentType,
Body: c.body,
})
if err != nil {
return err
}
return nil
}
func (c *call) getTimeout() time.Duration {
if c.timeout > 0 {
return c.timeout
}
return c.client.defaultTTL
}
func (c *call) getTimeoutMilliseconds() int64 {
t := c.client.defaultTTL
if c.timeout > 0 {
t = c.timeout
}
return int64(t / time.Millisecond)
}