more checkout
This commit is contained in:
+36
-8
@@ -13,31 +13,59 @@ import (
|
||||
amqp "github.com/rabbitmq/amqp091-go"
|
||||
)
|
||||
|
||||
// rabbitPublisher implements order.Publisher for the amqp_emit flow hook.
|
||||
// rabbitPublisher is the broker-delivery side used by the outbox relay. It dials
|
||||
// lazily and reconnects on failure, so the relay can be started even when the
|
||||
// broker is down at boot: messages accumulate durably in the outbox and drain
|
||||
// once the broker is reachable.
|
||||
type rabbitPublisher struct {
|
||||
url string
|
||||
mu sync.Mutex
|
||||
conn *amqp.Connection
|
||||
ch *amqp.Channel
|
||||
mu sync.Mutex
|
||||
}
|
||||
|
||||
func newRabbitPublisher(url string) (*rabbitPublisher, error) {
|
||||
conn, err := amqp.Dial(url)
|
||||
func newRabbitPublisher(url string) *rabbitPublisher {
|
||||
return &rabbitPublisher{url: url}
|
||||
}
|
||||
|
||||
func (p *rabbitPublisher) connect() error {
|
||||
conn, err := amqp.Dial(p.url)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
return err
|
||||
}
|
||||
ch, err := conn.Channel()
|
||||
if err != nil {
|
||||
_ = conn.Close()
|
||||
return nil, err
|
||||
return err
|
||||
}
|
||||
return &rabbitPublisher{conn: conn, ch: ch}, nil
|
||||
p.conn, p.ch = conn, ch
|
||||
return nil
|
||||
}
|
||||
|
||||
func (p *rabbitPublisher) reset() {
|
||||
if p.ch != nil {
|
||||
_ = p.ch.Close()
|
||||
}
|
||||
if p.conn != nil {
|
||||
_ = p.conn.Close()
|
||||
}
|
||||
p.ch, p.conn = nil, nil
|
||||
}
|
||||
|
||||
func (p *rabbitPublisher) Publish(exchange, routingKey string, body []byte) error {
|
||||
p.mu.Lock()
|
||||
defer p.mu.Unlock()
|
||||
return p.ch.PublishWithContext(context.Background(), exchange, routingKey, false, false,
|
||||
if p.ch == nil {
|
||||
if err := p.connect(); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
err := p.ch.PublishWithContext(context.Background(), exchange, routingKey, false, false,
|
||||
amqp.Publishing{ContentType: "application/json", Body: body})
|
||||
if err != nil {
|
||||
p.reset() // force a reconnect on the next attempt
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
||||
// This ingester lets the order service supersede the legacy go-order-manager:
|
||||
|
||||
Reference in New Issue
Block a user