options added

This commit is contained in:
nquidox 2026-02-20 15:31:13 +03:00
parent 8b1bc57cd0
commit 3f0401de1d
5 changed files with 91 additions and 44 deletions

View file

@ -5,28 +5,27 @@ import (
amqp "github.com/rabbitmq/amqp091-go"
"golang.org/x/time/rate"
"log"
"time"
)
func runConsumer(ctx context.Context, queue *Client, msgCh chan []byte) {
func runConsumer(ctx context.Context, client *Client, msgCh chan []byte) {
runCtx, cancel := context.WithCancel(ctx)
defer cancel()
limiter := rate.NewLimiter(rate.Every(time.Millisecond*500), 100)
limiter := rate.NewLimiter(rate.Every(client.opts.consumerRateLimit), client.opts.consumerBurstSize)
deliveries, err := queue.Consume()
deliveries, err := client.Consume()
if err != nil {
log.Printf("Could not start consuming: %s\n", err)
return
}
chClosedCh := make(chan *amqp.Error, 1)
queue.Channel.NotifyClose(chClosedCh)
client.Channel.NotifyClose(chClosedCh)
for {
select {
case <-runCtx.Done():
err = queue.Close()
err = client.Close()
if err != nil {
log.Printf("Close failed: %s\n", err)
}
@ -35,14 +34,14 @@ func runConsumer(ctx context.Context, queue *Client, msgCh chan []byte) {
case amqErr := <-chClosedCh:
log.Printf("AMQP Channel closed due to: %s\n", amqErr)
deliveries, err = queue.Consume()
deliveries, err = client.Consume()
if err != nil {
log.Println("Error trying to consume, will try again")
continue
}
chClosedCh = make(chan *amqp.Error, 1)
queue.Channel.NotifyClose(chClosedCh)
client.Channel.NotifyClose(chClosedCh)
case delivery := <-deliveries:
msgCh <- delivery.Body