forked from mtusi2026/mtusi2026lab5
120 lines
3.2 KiB
Go
120 lines
3.2 KiB
Go
package broker
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"net"
|
|
"time"
|
|
|
|
amqp "github.com/rabbitmq/amqp091-go"
|
|
)
|
|
|
|
const (
|
|
Exchange = "images"
|
|
Queue = "image.jobs"
|
|
Route = "process"
|
|
dead = "image.invalid"
|
|
)
|
|
|
|
type boundedConn struct{ net.Conn }
|
|
|
|
func (c boundedConn) Write(p []byte) (int, error) {
|
|
if err := c.SetWriteDeadline(time.Now().Add(5 * time.Second)); err != nil {
|
|
return 0, err
|
|
}
|
|
return c.Conn.Write(p)
|
|
}
|
|
|
|
// Connect supplies a freshly declared channel after a broker connection loss.
|
|
func Connect(url string) (*amqp.Connection, *amqp.Channel, error) {
|
|
dial := amqp.DefaultDial(5 * time.Second)
|
|
conn, err := amqp.DialConfig(url, amqp.Config{
|
|
Heartbeat: 10 * time.Second, Locale: "en_US",
|
|
Dial: func(network, addr string) (net.Conn, error) {
|
|
c, err := dial(network, addr)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return boundedConn{c}, nil
|
|
},
|
|
})
|
|
if err != nil {
|
|
// Connection errors may include credentials in a URL.
|
|
return nil, nil, errors.New("RabbitMQ connection unavailable")
|
|
}
|
|
ch, err := conn.Channel()
|
|
if err == nil {
|
|
err = declare(ch)
|
|
}
|
|
if err != nil {
|
|
Close(conn)
|
|
return nil, nil, err
|
|
}
|
|
return conn, ch, nil
|
|
}
|
|
|
|
func Close(conn *amqp.Connection) {
|
|
_ = conn.CloseDeadline(time.Now().Add(time.Second))
|
|
}
|
|
|
|
func declare(ch *amqp.Channel) error {
|
|
if err := ch.ExchangeDeclare(Exchange, "direct", true, false, false, false, nil); err != nil {
|
|
return err
|
|
}
|
|
if _, err := ch.QueueDeclare(dead, true, false, false, false, nil); err != nil {
|
|
return err
|
|
}
|
|
if _, err := ch.QueueDeclare(Queue, true, false, false, false, amqp.Table{
|
|
"x-queue-type": "classic", "x-dead-letter-exchange": "", "x-dead-letter-routing-key": dead,
|
|
}); err != nil {
|
|
return err
|
|
}
|
|
return ch.QueueBind(Queue, Route, Exchange, false, nil)
|
|
}
|
|
|
|
// Publisher has exactly one in-flight publish. A failed/ambiguous publish
|
|
// invalidates its channel before the next event can be attempted.
|
|
type Publisher struct {
|
|
ch *amqp.Channel
|
|
returns <-chan amqp.Return
|
|
confirms <-chan amqp.Confirmation
|
|
}
|
|
|
|
func NewPublisher(ch *amqp.Channel) (*Publisher, error) {
|
|
if err := ch.Confirm(false); err != nil {
|
|
return nil, err
|
|
}
|
|
return &Publisher{
|
|
ch: ch, returns: ch.NotifyReturn(make(chan amqp.Return, 1)),
|
|
confirms: ch.NotifyPublish(make(chan amqp.Confirmation, 1)),
|
|
}, nil
|
|
}
|
|
|
|
func (p *Publisher) Publish(ctx context.Context, id string, payload []byte) error {
|
|
seq := p.ch.GetNextPublishSeqNo()
|
|
if err := p.ch.PublishWithContext(ctx, Exchange, Route, true, false, amqp.Publishing{
|
|
ContentType: "application/json", DeliveryMode: amqp.Persistent, MessageId: id, Body: payload,
|
|
}); err != nil {
|
|
return err
|
|
}
|
|
select {
|
|
case <-p.returns:
|
|
return errors.New("outbox message was not routed")
|
|
case confirm, ok := <-p.confirms:
|
|
if !ok || confirm.DeliveryTag != seq || !confirm.Ack {
|
|
return errors.New("outbox publish was not confirmed")
|
|
}
|
|
// RabbitMQ sends basic.return before its confirm for an unrouted
|
|
// mandatory publish. The client's frame reader enqueues both in order.
|
|
// With a single in-flight message any queued return is for this ID.
|
|
select {
|
|
case <-p.returns:
|
|
return errors.New("outbox message was not routed")
|
|
default:
|
|
return nil
|
|
}
|
|
case <-ctx.Done():
|
|
return ctx.Err()
|
|
}
|
|
}
|