-
Notifications
You must be signed in to change notification settings - Fork 1
/
request.go
67 lines (56 loc) · 1.72 KB
/
request.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
package remit
import (
"encoding/json"
"time"
"github.com/oklog/ulid"
"github.com/streadway/amqp"
)
// Request represents an RPC request for data.
//
// Most commonly, this is used to contact another service to retrieve
// data, utilising a `Session.Endpoint`.
//
// For examples of Request usage, see `Session.Request` and `Session.LazyRequest`.
type Request struct {
RoutingKey string
session *Session
}
// RequestOptions is a list of options that can be passed when setting up
// a request.
type RequestOptions struct {
RoutingKey string
}
// Send sends some data to a previously-set-up `Request` using `Session.Request`.
// It returns a channel on which a single reply `Event` will be passed upon RPC completion.
func (request *Request) Send(data interface{}) chan Event {
j, err := json.Marshal(data)
failOnError(err, "Failed making JSON from result")
receiveChannel := make(chan Event, 1)
messageId := ulid.MustNew(ulid.Now(), nil).String()
request.session.registerReply(messageId, receiveChannel)
err = request.session.requestChannel.Publish(
"remit", // exchange
request.RoutingKey, // routing key / queue
false, // mandatory
false, // immediate
amqp.Publishing{
Headers: amqp.Table{},
ContentType: "application/json",
Body: j,
Timestamp: time.Now(),
MessageId: messageId,
AppId: request.session.Config.Name,
CorrelationId: messageId,
ReplyTo: "amq.rabbitmq.reply-to",
},
)
failOnError(err, "Failed to send request message")
return receiveChannel
}
func createRequest(session *Session, options RequestOptions) Request {
request := Request{
RoutingKey: options.RoutingKey,
session: session,
}
return request
}