mt-rabbit/handler.go

131 lines
2.6 KiB
Go
Raw Permalink Normal View History

2026-02-20 14:20:04 +03:00
package rabbit
import (
2026-04-03 11:22:13 +03:00
"errors"
2026-04-02 15:36:37 +03:00
"fmt"
2026-02-20 14:20:04 +03:00
amqp "github.com/rabbitmq/amqp091-go"
2026-04-03 11:22:13 +03:00
"io"
2026-02-20 14:20:04 +03:00
"log"
2026-04-08 12:05:39 +03:00
"os"
2026-02-20 14:20:04 +03:00
"sync"
"time"
)
type Client struct {
mutex *sync.Mutex
2026-04-27 00:06:46 +03:00
queueOptions *QueueOpts
2026-02-20 14:20:04 +03:00
logger *log.Logger
connection *amqp.Connection
Channel *amqp.Channel
done chan bool
notifyConnClose chan *amqp.Error
notifyChanClose chan *amqp.Error
notifyConfirm chan amqp.Confirmation
isReady bool
2026-02-20 15:31:13 +03:00
opts options
2026-04-02 15:36:37 +03:00
connected chan struct{}
2026-02-20 14:20:04 +03:00
}
2026-02-20 15:31:13 +03:00
type options struct {
2026-04-02 15:36:37 +03:00
connectTimeout time.Duration
2026-02-20 15:31:13 +03:00
reconnectDelay time.Duration
reInitDelay time.Duration
resendDelay time.Duration
consumerRateLimit time.Duration
consumerBurstSize int
2026-04-03 11:22:13 +03:00
logger *log.Logger
2026-02-20 14:20:04 +03:00
}
2026-04-27 00:06:46 +03:00
type QueueOpts struct {
QueueName string
Durable bool
AutoDelete bool
Exclusive bool
NoWait bool
Args amqp.Table
}
func NewClient(address Address, queueOpts QueueOpts, opts ...Option) (*Client, error) {
2026-04-08 12:05:39 +03:00
l := log.New(os.Stdout, "", log.LstdFlags)
2026-04-03 11:22:13 +03:00
addr, err := address.makeAddr()
if err != nil {
return nil, errors.Join(errBadAddr, err)
2026-02-20 15:31:13 +03:00
}
2026-04-27 00:06:46 +03:00
if queueOpts.QueueName == "" {
2026-04-08 12:05:39 +03:00
l.Fatal(errNoQueue)
2026-02-20 15:31:13 +03:00
}
2026-02-20 14:20:04 +03:00
client := Client{
2026-04-27 00:06:46 +03:00
mutex: &sync.Mutex{},
queueOptions: &queueOpts,
done: make(chan bool),
connected: make(chan struct{}),
logger: l,
2026-02-20 15:31:13 +03:00
}
o := options{
2026-04-02 15:36:37 +03:00
connectTimeout: 15 * time.Second,
2026-04-03 11:22:13 +03:00
reconnectDelay: 5 * time.Second,
reInitDelay: 2 * time.Second,
resendDelay: 5 * time.Second,
2026-02-20 15:31:13 +03:00
consumerRateLimit: time.Millisecond * 500,
consumerBurstSize: 10,
2026-04-03 11:22:13 +03:00
logger: log.New(io.Discard, "", 0),
2026-02-20 15:31:13 +03:00
}
for _, opt := range opts {
opt(&o)
2026-02-20 14:20:04 +03:00
}
2026-04-03 11:22:13 +03:00
client.logger = o.logger
if err = client.connectAndSignal(addr, o.connectTimeout); err != nil {
2026-04-02 15:36:37 +03:00
return nil, fmt.Errorf("failed to connect: %w", err)
}
2026-02-20 15:31:13 +03:00
go client.handleReconnect(addr)
2026-02-20 14:20:04 +03:00
2026-04-02 15:36:37 +03:00
return &client, nil
2026-02-20 14:20:04 +03:00
}
2026-02-20 16:20:09 +03:00
func NewPublisher(client *Client) Publisher {
return &pubHandler{client: client}
2026-02-20 14:20:04 +03:00
}
2026-02-20 16:05:12 +03:00
2026-02-21 15:03:43 +03:00
func NewConsumer(client *Client) Consumer {
return &consumeHandler{client: client}
2026-02-20 16:05:12 +03:00
}
2026-04-02 15:36:37 +03:00
func (c *Client) connectAndSignal(addr string, timeout time.Duration) error {
2026-04-02 15:50:55 +03:00
type result struct {
conn *amqp.Connection
err error
}
resCh := make(chan result, 1)
go func() {
conn, err := amqp.Dial(addr)
resCh <- result{conn, err}
}()
select {
case <-time.After(timeout):
return fmt.Errorf("connection timeout after %v", timeout)
case res := <-resCh:
if res.err != nil {
return res.err
}
2026-04-02 16:09:17 +03:00
c.changeConnection(res.conn)
if err := c.init(res.conn); err != nil {
res.conn.Close()
return fmt.Errorf("init failed: %w", err)
}
2026-04-02 15:50:55 +03:00
close(c.connected)
return nil
2026-04-02 15:36:37 +03:00
}
}