forked from porthos-rpc/porthos-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
response_writer.go
51 lines (41 loc) · 1.06 KB
/
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
48
49
50
51
package porthos
import (
"github.com/porthos-rpc/porthos-go/log"
"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 {
log.Debug("Sending response to queue '%s'. Slot: '%d'", rw.delivery.ReplyTo, []byte(rw.delivery.CorrelationId))
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)
log.Debug("Ack from slot '%d'", []byte(rw.delivery.CorrelationId))
}
return nil
}