This repository has been archived by the owner on May 18, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmq_adapter.go
65 lines (57 loc) · 1.71 KB
/
mq_adapter.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
package rabbitmq
import (
"strconv"
"time"
"github.com/smartystreets/messaging/v2"
"github.com/streadway/amqp"
)
func fromAMQPDelivery(delivery amqp.Delivery, channel Acknowledger) messaging.Delivery {
return messaging.Delivery{
SourceID: parseUint64(delivery.AppId),
MessageID: parseUint64(delivery.MessageId),
MessageType: delivery.Type,
ContentType: delivery.ContentType,
ContentEncoding: delivery.ContentEncoding,
Timestamp: delivery.Timestamp,
Payload: delivery.Body,
Upstream: delivery,
Receipt: newReceipt(channel, delivery.DeliveryTag),
}
}
func parseUint64(value string) uint64 {
parsed, _ := strconv.ParseUint(value, 10, 64)
return parsed
}
func toAMQPDispatch(dispatch messaging.Dispatch, now time.Time) amqp.Publishing {
if dispatch.Timestamp == zeroTime {
dispatch.Timestamp = now
}
return amqp.Publishing{
AppId: strconv.FormatUint(dispatch.SourceID, base10),
MessageId: strconv.FormatUint(dispatch.MessageID, base10),
Type: dispatch.MessageType,
ContentType: dispatch.ContentType,
ContentEncoding: dispatch.ContentEncoding,
Timestamp: dispatch.Timestamp,
Expiration: computeExpiration(dispatch.Expiration),
DeliveryMode: computePersistence(dispatch.Durable),
Body: dispatch.Payload,
}
}
func computeExpiration(expiration time.Duration) string {
if expiration == 0 {
return ""
} else if seconds := expiration.Seconds(); seconds <= 0 {
return "0"
} else {
return strconv.FormatUint(uint64(seconds), base10)
}
}
func computePersistence(durable bool) uint8 {
if durable {
return amqp.Persistent
}
return amqp.Transient
}
var zeroTime = time.Time{}
const base10 = 10