mt-rabbit/handler.go

113 lines
2.2 KiB
Go
Raw Permalink Normal View History

2026-02-20 14:20:04 +03:00
package rabbit
import (
2026-04-02 15:36:37 +03:00
"fmt"
2026-02-20 14:20:04 +03:00
amqp "github.com/rabbitmq/amqp091-go"
"log"
"os"
"sync"
"time"
)
type Client struct {
mutex *sync.Mutex
queueName string
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-02-20 14:20:04 +03:00
}
2026-04-02 15:36:37 +03:00
func NewClient(addr, queueName string, opts ...Option) (*Client, error) {
2026-02-20 15:31:13 +03:00
if addr == "" {
2026-02-20 16:33:15 +03:00
log.Fatal(errNoAddr)
2026-02-20 15:31:13 +03:00
}
if queueName == "" {
2026-02-20 16:33:15 +03:00
log.Fatal(errNoQueue)
2026-02-20 15:31:13 +03:00
}
2026-02-20 14:20:04 +03:00
client := Client{
2026-02-20 15:31:13 +03:00
mutex: &sync.Mutex{},
logger: log.New(os.Stdout, "", log.LstdFlags),
queueName: queueName,
done: make(chan bool),
2026-04-02 15:46:21 +03:00
connected: make(chan struct{}),
2026-02-20 15:31:13 +03:00
}
o := options{
2026-04-02 15:36:37 +03:00
connectTimeout: 15 * time.Second,
2026-02-20 15:31:13 +03:00
reconnectDelay: 5,
reInitDelay: 2,
resendDelay: 5,
consumerRateLimit: time.Millisecond * 500,
consumerBurstSize: 10,
}
for _, opt := range opts {
opt(&o)
2026-02-20 14:20:04 +03:00
}
2026-04-02 15:36:37 +03:00
if err := client.connectAndSignal(addr, o.connectTimeout); err != nil {
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
}
}