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

@ -2,7 +2,6 @@ package rabbit
import (
"context"
"errors"
amqp "github.com/rabbitmq/amqp091-go"
"log"
"os"
@ -21,45 +20,48 @@ type Client struct {
notifyChanClose chan *amqp.Error
notifyConfirm chan amqp.Confirmation
isReady bool
reconnectDelay time.Duration
reInitDelay time.Duration
resendDelay time.Duration
opts options
}
//const (
// 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
Addr string
type options struct {
reconnectDelay time.Duration
reInitDelay time.Duration
resendDelay time.Duration
consumerRateLimit time.Duration
consumerBurstSize int
}
var (
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")
)
func NewClient(deps Deps) *Client {
client := Client{
mutex: &sync.Mutex{},
logger: log.New(os.Stdout, "", log.LstdFlags),
queueName: deps.QueueName,
done: make(chan bool),
reconnectDelay: deps.ReconnectDelay,
reInitDelay: deps.ReInitDelay,
resendDelay: deps.ResendDelay,
func NewClient(addr, queueName string, opts ...Option) (*Client, error) {
if addr == "" {
return nil, errNoAddr
}
go client.handleReconnect(deps.Addr)
if queueName == "" {
return nil, errNoQueue
}
return &client
client := Client{
mutex: &sync.Mutex{},
logger: log.New(os.Stdout, "", log.LstdFlags),
queueName: queueName,
done: make(chan bool),
}
o := options{
reconnectDelay: 5,
reInitDelay: 2,
resendDelay: 5,
consumerRateLimit: time.Millisecond * 500,
consumerBurstSize: 10,
}
for _, opt := range opts {
opt(&o)
}
go client.handleReconnect(addr)
return &client, nil
}
func StartConsumer(ctx context.Context, client *Client, chanLen int) chan []byte {