forked from porthos-rpc/porthos-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathresponse_writer.go
47 lines (38 loc) · 864 Bytes
/
response_writer.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
package porthos
import (
"github.com/streadway/amqp"
)
// ResponseWriter is responsible for sending back the response to the replyTo queue.
type ResponseWriter interface {
Write(res Response) error
}
type responseWriter struct {
channel *amqp.Channel
autoAck bool
delivery amqp.Delivery
}
func (rw *responseWriter) Write(res Response) error {
if rw.channel == nil {
return ErrNilPublishChannel
}
// status code is a header as well.
res.GetHeaders().Set("statusCode", res.GetStatusCode())
err := rw.channel.Publish(
"",
rw.delivery.ReplyTo,
false,
false,
amqp.Publishing{
Headers: res.GetHeaders().asMap(),
ContentType: res.GetContentType(),
CorrelationId: rw.delivery.CorrelationId,
Body: res.GetBody(),
})
if err != nil {
return err
}
if !rw.autoAck {
rw.delivery.Ack(false)
}
return nil
}