2024-11-21 17:34:08 +08:00
|
|
|
package rabbitmq_amqp
|
|
|
|
|
|
2025-01-16 22:26:12 +08:00
|
|
|
import (
|
|
|
|
|
"github.com/Azure/go-amqp"
|
|
|
|
|
"math/rand"
|
|
|
|
|
"time"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
const AtMostOnce = 0
|
|
|
|
|
const AtLeastOnce = 1
|
2024-11-21 17:34:08 +08:00
|
|
|
|
|
|
|
|
// senderLinkOptions returns the options for a sender link
|
|
|
|
|
// with the given address and link name.
|
|
|
|
|
// That should be the same for all the links.
|
2025-01-16 22:26:12 +08:00
|
|
|
func createSenderLinkOptions(address string, linkName string, deliveryMode int) *amqp.SenderOptions {
|
2024-11-21 17:34:08 +08:00
|
|
|
prop := make(map[string]any)
|
|
|
|
|
prop["paired"] = true
|
2025-01-16 22:26:12 +08:00
|
|
|
sndSettleMode := amqp.SenderSettleModeSettled.Ptr()
|
|
|
|
|
/// SndSettleMode = deliveryMode == DeliveryMode.AtMostOnce
|
|
|
|
|
// ? SenderSettleMode.Settled
|
|
|
|
|
// : SenderSettleMode.Unsettled,
|
|
|
|
|
|
|
|
|
|
if deliveryMode == AtLeastOnce {
|
|
|
|
|
sndSettleMode = amqp.SenderSettleModeUnsettled.Ptr()
|
|
|
|
|
}
|
|
|
|
|
|
2024-11-21 17:34:08 +08:00
|
|
|
return &amqp.SenderOptions{
|
|
|
|
|
SourceAddress: address,
|
|
|
|
|
DynamicAddress: false,
|
|
|
|
|
ExpiryPolicy: amqp.ExpiryPolicyLinkDetach,
|
|
|
|
|
ExpiryTimeout: 0,
|
|
|
|
|
Name: linkName,
|
|
|
|
|
Properties: prop,
|
2025-01-16 22:26:12 +08:00
|
|
|
SettlementMode: sndSettleMode,
|
2024-11-21 17:34:08 +08:00
|
|
|
RequestedReceiverSettleMode: amqp.ReceiverSettleModeFirst.Ptr(),
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// receiverLinkOptions returns the options for a receiver link
|
|
|
|
|
// with the given address and link name.
|
|
|
|
|
// That should be the same for all the links.
|
|
|
|
|
func createReceiverLinkOptions(address string, linkName string) *amqp.ReceiverOptions {
|
|
|
|
|
prop := make(map[string]any)
|
|
|
|
|
prop["paired"] = true
|
|
|
|
|
return &amqp.ReceiverOptions{
|
|
|
|
|
TargetAddress: address,
|
|
|
|
|
DynamicAddress: false,
|
|
|
|
|
Name: linkName,
|
|
|
|
|
Properties: prop,
|
|
|
|
|
RequestedSenderSettleMode: amqp.SenderSettleModeSettled.Ptr(),
|
|
|
|
|
SettlementMode: amqp.ReceiverSettleModeFirst.Ptr(),
|
|
|
|
|
ExpiryPolicy: amqp.ExpiryPolicyLinkDetach,
|
|
|
|
|
Credit: 100,
|
|
|
|
|
}
|
|
|
|
|
}
|
2025-01-16 22:26:12 +08:00
|
|
|
|
|
|
|
|
func random(max int) int {
|
|
|
|
|
r := rand.New(rand.NewSource(time.Now().Unix()))
|
|
|
|
|
return r.Intn(max)
|
|
|
|
|
}
|