Compare commits

...

28 commits
v0.1.0 ... main

Author SHA1 Message Date
nquidox
72f039e4f7 reconnect bugfix 2026-05-04 11:50:59 +03:00
nquidox
fd495892fa queue opts 2026-04-27 00:06:46 +03:00
nquidox
9b9d8a3064 removed unused import 2026-04-08 12:21:19 +03:00
nquidox
154320d288 logger fix 2026-04-08 12:05:39 +03:00
nquidox
a2862350b3 added address, logging + time fixes 2026-04-03 11:22:13 +03:00
nquidox
44d99a4ba5 address maker 2026-04-03 11:21:24 +03:00
nquidox
e58b052251 logging options added 2026-04-03 11:21:13 +03:00
nquidox
4810a8666e msg change 2026-04-03 11:20:53 +03:00
nquidox
f23b388689 mutex removed 2026-04-03 10:27:01 +03:00
nquidox
5e8661cfcd isReady disabled 2026-04-02 16:15:36 +03:00
nquidox
8f967361e8 connection check 2026-04-02 16:09:17 +03:00
nquidox
ecda63761c different type of check connection 2026-04-02 15:50:55 +03:00
nquidox
d62d41b41a chan fix 2026-04-02 15:46:21 +03:00
nquidox
9d837385b1 connection timeout 2026-04-02 15:36:37 +03:00
nquidox
46e07b7577 bugfix 2026-02-21 16:07:19 +03:00
nquidox
c8a6a7f8fa read only chan 2026-02-21 15:55:29 +03:00
nquidox
40d1d3dd29 ctx + write only chan 2026-02-21 15:54:50 +03:00
nquidox
42646a6a08 reconnect timer + fixes 2026-02-21 15:03:51 +03:00
nquidox
13ee3230e3 fix 2026-02-20 18:29:00 +03:00
nquidox
2846c1eda0 panic on empty values 2026-02-20 16:33:15 +03:00
nquidox
23b439f0aa privated 2026-02-20 16:20:19 +03:00
nquidox
4c5b1c7030 consumer constructor 2026-02-20 16:20:09 +03:00
nquidox
9366cbbf6d publisher added 2026-02-20 16:05:12 +03:00
nquidox
6addcca08d private methods + push move 2026-02-20 16:04:48 +03:00
nquidox
3f0401de1d options added 2026-02-20 15:31:13 +03:00
nquidox
8b1bc57cd0 simple rate limiter 2026-02-20 14:39:24 +03:00
nquidox
d2af3b1183 added chan 2026-02-20 14:25:34 +03:00
nquidox
41f464a823 added 2026-02-20 14:25:24 +03:00
10 changed files with 367 additions and 99 deletions

1
.gitignore vendored Normal file
View file

@ -0,0 +1 @@
.idea

34
address.go Normal file
View file

@ -0,0 +1,34 @@
package rabbit
import (
"errors"
"fmt"
"net/url"
)
type Address struct {
Username string
Password string
Host string
Port uint16
Vhost string
}
func (a *Address) makeAddr() (string, error) {
if a.Host == "" {
return "", errors.New("no host provided")
}
if a.Vhost == "" {
return "", errors.New("no vhost provided")
}
//"amqp://username:password@host:port/vhost"
return fmt.Sprintf("amqp://%v:%v@%v:%v/%v",
url.QueryEscape(a.Username),
url.QueryEscape(a.Password),
a.Host,
a.Port,
url.PathEscape(a.Vhost),
), nil
}

View file

@ -3,52 +3,111 @@ package rabbit
import ( import (
"context" "context"
amqp "github.com/rabbitmq/amqp091-go" amqp "github.com/rabbitmq/amqp091-go"
"log" "golang.org/x/time/rate"
"time" "time"
) )
func runConsumer(ctx context.Context, queue *Client, msgCh chan []byte) { type Consumer interface {
Start(ctx context.Context, chanLen uint) <-chan []byte
}
type consumeHandler struct {
client *Client
}
func (c *consumeHandler) Start(ctx context.Context, chanLen uint) <-chan []byte {
msgCh := make(chan []byte, chanLen)
go runConsumer(ctx, c.client, msgCh)
return msgCh
}
func runConsumer(ctx context.Context, client *Client, msgCh chan []byte) {
runCtx, cancel := context.WithCancel(ctx) runCtx, cancel := context.WithCancel(ctx)
defer cancel() defer cancel()
deliveries, err := queue.Consume() limiter := rate.NewLimiter(rate.Every(client.opts.consumerRateLimit), client.opts.consumerBurstSize)
reconnectSignal := make(chan struct{}, 1)
reconnectTrigger := func() {
select {
case reconnectSignal <- struct{}{}:
default:
}
}
// initial consume
deliveries, err := client.consume()
if err != nil { if err != nil {
log.Printf("Could not start consuming: %s\n", err) client.logger.Printf("Could not start consuming: %s\n", err)
return return
} }
chClosedCh := make(chan *amqp.Error, 1) chClosedCh := make(chan *amqp.Error, 1)
queue.Channel.NotifyClose(chClosedCh) client.Channel.NotifyClose(chClosedCh)
for { for {
select { select {
case <-runCtx.Done(): case <-runCtx.Done():
err = queue.Close() err = client.Close()
if err != nil { if err != nil {
log.Printf("Close failed: %s\n", err) client.logger.Printf("Close failed: %s\n", err)
} }
return return
case amqErr := <-chClosedCh: case <-reconnectSignal:
log.Printf("AMQP Channel closed due to: %s\n", amqErr) select {
case <-runCtx.Done():
return
case <-time.After(client.opts.reconnectDelay):
}
deliveries, err = queue.Consume() deliveries, err = client.consume()
if err != nil { if err != nil {
log.Println("Error trying to consume, will try again") client.logger.Printf("Consume reconnect failed, retry: %s\n", err)
reconnectTrigger()
continue continue
} }
// re-create closing chan
chClosedCh = make(chan *amqp.Error, 1) chClosedCh = make(chan *amqp.Error, 1)
queue.Channel.NotifyClose(chClosedCh) client.Channel.NotifyClose(chClosedCh)
client.logger.Println("Consume reconnect success")
case delivery := <-deliveries: case amqErr := <-chClosedCh:
msgCh <- delivery.Body client.logger.Printf("AMQP Channel closed due to: %s Reconnecting...\n", amqErr)
log.Printf("Received message: %s\n", delivery.Body) reconnectTrigger()
case delivery, ok := <-deliveries:
if !ok {
client.logger.Println("Deliveries channel closed unexpectedly")
reconnectTrigger()
continue
}
if err = limiter.Wait(runCtx); err != nil {
client.logger.Printf("Wait limiter failed: %s\n", err)
}
select {
case <-runCtx.Done():
if err = delivery.Nack(false, true); err != nil {
client.logger.Printf("Error nacking message: %s\n", err)
}
err = client.Close()
if err != nil {
client.logger.Printf("Close failed: %s\n", err)
}
return
case msgCh <- delivery.Body:
client.logger.Printf("Received message: %s\n", delivery.Body)
if err = delivery.Ack(false); err != nil { if err = delivery.Ack(false); err != nil {
log.Printf("Error acknowledging message: %s\n", err) client.logger.Printf("Error acknowledging message: %s\n", err)
} }
<-time.After(time.Second * 2) }
} }
} }
} }

2
go.mod
View file

@ -3,3 +3,5 @@ module repo.nqws.ru/merch-tracker-v2/mt-rabbit
go 1.25.0 go 1.25.0
require github.com/rabbitmq/amqp091-go v1.10.0 require github.com/rabbitmq/amqp091-go v1.10.0
require golang.org/x/time v0.14.0 // indirect

2
go.sum
View file

@ -2,3 +2,5 @@ github.com/rabbitmq/amqp091-go v1.10.0 h1:STpn5XsHlHGcecLmMFCtg7mqq0RnD+zFr4uzuk
github.com/rabbitmq/amqp091-go v1.10.0/go.mod h1:Hy4jKW5kQART1u+JkDTF9YYOQUHXqMuhrgxOEeS7G4o= github.com/rabbitmq/amqp091-go v1.10.0/go.mod h1:Hy4jKW5kQART1u+JkDTF9YYOQUHXqMuhrgxOEeS7G4o=
go.uber.org/goleak v1.3.0 h1:2K3zAYmnTNqV73imy9J1T3WC+gmCePx2hEGkimedGto= go.uber.org/goleak v1.3.0 h1:2K3zAYmnTNqV73imy9J1T3WC+gmCePx2hEGkimedGto=
go.uber.org/goleak v1.3.0/go.mod h1:CoHD4mav9JJNrW/WLlf7HGZPjdw8EucARQHekz1X6bE= go.uber.org/goleak v1.3.0/go.mod h1:CoHD4mav9JJNrW/WLlf7HGZPjdw8EucARQHekz1X6bE=
golang.org/x/time v0.14.0 h1:MRx4UaLrDotUKUdCIqzPC48t1Y9hANFKIRpNx+Te8PI=
golang.org/x/time v0.14.0/go.mod h1:eL/Oa2bBBK0TkX57Fyni+NgnyQQN4LitPmob2Hjnqw4=

View file

@ -1,9 +1,10 @@
package rabbit package rabbit
import ( import (
"context"
"errors" "errors"
"fmt"
amqp "github.com/rabbitmq/amqp091-go" amqp "github.com/rabbitmq/amqp091-go"
"io"
"log" "log"
"os" "os"
"sync" "sync"
@ -12,7 +13,7 @@ import (
type Client struct { type Client struct {
mutex *sync.Mutex mutex *sync.Mutex
queueName string queueOptions *QueueOpts
logger *log.Logger logger *log.Logger
connection *amqp.Connection connection *amqp.Connection
Channel *amqp.Channel Channel *amqp.Channel
@ -21,50 +22,109 @@ type Client struct {
notifyChanClose chan *amqp.Error notifyChanClose chan *amqp.Error
notifyConfirm chan amqp.Confirmation notifyConfirm chan amqp.Confirmation
isReady bool isReady bool
opts options
connected chan struct{}
}
type options struct {
connectTimeout time.Duration
reconnectDelay time.Duration reconnectDelay time.Duration
reInitDelay time.Duration reInitDelay time.Duration
resendDelay time.Duration resendDelay time.Duration
consumerRateLimit time.Duration
consumerBurstSize int
logger *log.Logger
} }
//const ( type QueueOpts struct {
// reconnectDelay = 5 * time.Second
// reInitDelay = 2 * time.Second
// resendDelay = 5 * time.Second
//)
type Deps struct {
ReconnectDelay time.Duration
ReInitDelay time.Duration
ResendDelay time.Duration
QueueName string QueueName string
Addr string Durable bool
AutoDelete bool
Exclusive bool
NoWait bool
Args amqp.Table
} }
var ( func NewClient(address Address, queueOpts QueueOpts, opts ...Option) (*Client, error) {
errNotConnected = errors.New("not connected to a server") l := log.New(os.Stdout, "", log.LstdFlags)
errAlreadyClosed = errors.New("already closed: not connected to the server")
errShutdown = errors.New("client is shutting down") addr, err := address.makeAddr()
) if err != nil {
return nil, errors.Join(errBadAddr, err)
}
if queueOpts.QueueName == "" {
l.Fatal(errNoQueue)
}
func NewClient(deps Deps) *Client {
client := Client{ client := Client{
mutex: &sync.Mutex{}, mutex: &sync.Mutex{},
logger: log.New(os.Stdout, "", log.LstdFlags), queueOptions: &queueOpts,
queueName: deps.QueueName,
done: make(chan bool), done: make(chan bool),
reconnectDelay: deps.ReconnectDelay, connected: make(chan struct{}),
reInitDelay: deps.ReInitDelay, logger: l,
resendDelay: deps.ResendDelay,
} }
go client.handleReconnect(deps.Addr) o := options{
connectTimeout: 15 * time.Second,
return &client reconnectDelay: 5 * time.Second,
reInitDelay: 2 * time.Second,
resendDelay: 5 * time.Second,
consumerRateLimit: time.Millisecond * 500,
consumerBurstSize: 10,
logger: log.New(io.Discard, "", 0),
} }
func StartConsumer(ctx context.Context, client *Client, chanLen int) chan []byte { for _, opt := range opts {
msgCh := make(chan []byte, chanLen) opt(&o)
go runConsumer(ctx, client) }
return msgCh client.logger = o.logger
if err = client.connectAndSignal(addr, o.connectTimeout); err != nil {
return nil, fmt.Errorf("failed to connect: %w", err)
}
go client.handleReconnect(addr)
return &client, nil
}
func NewPublisher(client *Client) Publisher {
return &pubHandler{client: client}
}
func NewConsumer(client *Client) Consumer {
return &consumeHandler{client: client}
}
func (c *Client) connectAndSignal(addr string, timeout time.Duration) error {
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
}
c.changeConnection(res.conn)
if err := c.init(res.conn); err != nil {
res.conn.Close()
return fmt.Errorf("init failed: %w", err)
}
close(c.connected)
return nil
}
} }

11
messages.go Normal file
View file

@ -0,0 +1,11 @@
package rabbit
import "errors"
var (
errBadAddr = errors.New("bad address")
errNoQueue = errors.New("no queue")
errNotConnected = errors.New("not connected to a server")
errAlreadyClosed = errors.New("already closed: not connected to the server")
errShutdown = errors.New("client is shutting down")
)

60
options.go Normal file
View file

@ -0,0 +1,60 @@
package rabbit
import (
"io"
"log"
"os"
"time"
)
type Option func(*options)
func WithConnectTimeout(t time.Duration) Option {
return func(op *options) {
op.connectTimeout = t
}
}
func WithReconnectDelay(t time.Duration) Option {
return func(op *options) {
op.reconnectDelay = t
}
}
func WithReInitDelay(t time.Duration) Option {
return func(op *options) {
op.reInitDelay = t
}
}
func WithResendDelay(t time.Duration) Option {
return func(op *options) {
op.resendDelay = t
}
}
func WithConsumerRateLimit(t time.Duration) Option {
return func(op *options) {
op.consumerRateLimit = t
}
}
func WithConsumerBurstSize(t int) Option {
return func(op *options) {
op.consumerBurstSize = t
}
}
func WithLogger(l *log.Logger) Option {
return func(op *options) { op.logger = l }
}
func WithLogging(enabled bool) Option {
return func(op *options) {
if enabled {
op.logger = log.New(os.Stdout, "", log.LstdFlags)
} else {
op.logger = log.New(io.Discard, "", 0)
}
}
}

65
publisher.go Normal file
View file

@ -0,0 +1,65 @@
package rabbit
import (
"context"
"errors"
"time"
)
type Publisher interface {
Start(ctx context.Context, chanLen uint) chan<- []byte
}
type pubHandler struct {
client *Client
}
func (p *pubHandler) Start(ctx context.Context, chanLen uint) chan<- []byte {
ch := make(chan []byte, chanLen)
go func() {
for {
select {
case <-ctx.Done():
for msg := range ch {
if err := p.push(msg); err != nil {
p.client.logger.Printf("Error publishing message (shutdown): %s", err)
}
}
p.client.logger.Println("Publisher stopped")
return
case msg := <-ch:
if err := p.push(msg); err != nil {
p.client.logger.Printf("Error publishing message: %s", err)
}
}
}
}()
return ch
}
func (p *pubHandler) push(data []byte) error {
p.client.mutex.Lock()
if !p.client.isReady {
p.client.mutex.Unlock()
return errors.New("failed to push: not connected")
}
p.client.mutex.Unlock()
for {
err := p.client.unsafePush(data)
if err != nil {
p.client.logger.Println("Push failed. Retrying...")
select {
case <-p.client.done:
return errShutdown
case <-time.After(p.client.opts.resendDelay):
}
continue
}
confirm := <-p.client.notifyConfirm
if confirm.Ack {
p.client.logger.Printf("Push confirmed [%d]!", confirm.DeliveryTag)
return nil
}
}
}

View file

@ -2,17 +2,12 @@ package rabbit
import ( import (
"context" "context"
"errors"
amqp "github.com/rabbitmq/amqp091-go" amqp "github.com/rabbitmq/amqp091-go"
"time" "time"
) )
func (c *Client) handleReconnect(addr string) { func (c *Client) handleReconnect(addr string) {
for { for {
c.mutex.Lock()
c.isReady = false
c.mutex.Unlock()
c.logger.Println("Connecting to server...") c.logger.Println("Connecting to server...")
conn, err := c.connect(addr) conn, err := c.connect(addr)
@ -22,7 +17,7 @@ func (c *Client) handleReconnect(addr string) {
select { select {
case <-c.done: case <-c.done:
return return
case <-time.After(c.reconnectDelay): case <-time.After(c.opts.reconnectDelay):
} }
continue continue
} }
@ -46,10 +41,6 @@ func (c *Client) connect(addr string) (*amqp.Connection, error) {
func (c *Client) handleReInit(conn *amqp.Connection) bool { func (c *Client) handleReInit(conn *amqp.Connection) bool {
for { for {
c.mutex.Lock()
c.isReady = false
c.mutex.Unlock()
if err := c.init(conn); err != nil { if err := c.init(conn); err != nil {
c.logger.Printf("Failed to initialize connection: %s", err) c.logger.Printf("Failed to initialize connection: %s", err)
@ -59,7 +50,7 @@ func (c *Client) handleReInit(conn *amqp.Connection) bool {
case <-c.notifyConnClose: case <-c.notifyConnClose:
c.logger.Println("Connection closed. Reconnecting...") c.logger.Println("Connection closed. Reconnecting...")
return false return false
case <-time.After(c.reInitDelay): case <-time.After(c.opts.reInitDelay):
} }
continue continue
} }
@ -87,7 +78,16 @@ func (c *Client) init(conn *amqp.Connection) error {
return err return err
} }
_, err = ch.QueueDeclare(c.queueName, false, false, false, false, nil) //_, err = ch.QueueDeclare(c.queueName, false, false, false, false, nil)
_, err = ch.QueueDeclare(
c.queueOptions.QueueName,
c.queueOptions.Durable,
c.queueOptions.AutoDelete,
c.queueOptions.Exclusive,
c.queueOptions.NoWait,
c.queueOptions.Args,
)
if err != nil { if err != nil {
return err return err
} }
@ -115,33 +115,7 @@ func (c *Client) changeChannel(channel *amqp.Channel) {
c.Channel.NotifyPublish(c.notifyConfirm) c.Channel.NotifyPublish(c.notifyConfirm)
} }
func (c *Client) Push(data []byte) error { func (c *Client) unsafePush(data []byte) error {
c.mutex.Lock()
if !c.isReady {
c.mutex.Unlock()
return errors.New("failed to push: not connected")
}
c.mutex.Unlock()
for {
err := c.UnsafePush(data)
if err != nil {
c.logger.Println("Push failed. Retrying...")
select {
case <-c.done:
return errShutdown
case <-time.After(c.resendDelay):
}
continue
}
confirm := <-c.notifyConfirm
if confirm.Ack {
c.logger.Printf("Push confirmed [%d]!", confirm.DeliveryTag)
return nil
}
}
}
func (c *Client) UnsafePush(data []byte) error {
c.mutex.Lock() c.mutex.Lock()
if !c.isReady { if !c.isReady {
c.mutex.Unlock() c.mutex.Unlock()
@ -155,7 +129,7 @@ func (c *Client) UnsafePush(data []byte) error {
return c.Channel.PublishWithContext( return c.Channel.PublishWithContext(
ctx, ctx,
"", "",
c.queueName, c.queueOptions.QueueName,
false, false,
false, false,
amqp.Publishing{ amqp.Publishing{
@ -165,7 +139,7 @@ func (c *Client) UnsafePush(data []byte) error {
) )
} }
func (c *Client) Consume() (<-chan amqp.Delivery, error) { func (c *Client) consume() (<-chan amqp.Delivery, error) {
c.mutex.Lock() c.mutex.Lock()
if !c.isReady { if !c.isReady {
c.mutex.Unlock() c.mutex.Unlock()
@ -182,7 +156,7 @@ func (c *Client) Consume() (<-chan amqp.Delivery, error) {
} }
return c.Channel.Consume( return c.Channel.Consume(
c.queueName, c.queueOptions.QueueName,
"", "",
false, false,
false, false,