added address, logging + time fixes

This commit is contained in:
nquidox 2026-04-03 11:22:13 +03:00
parent 44d99a4ba5
commit a2862350b3

View file

@ -1,10 +1,11 @@
package rabbit
import (
"errors"
"fmt"
amqp "github.com/rabbitmq/amqp091-go"
"io"
"log"
"os"
"sync"
"time"
)
@ -31,11 +32,13 @@ type options struct {
resendDelay time.Duration
consumerRateLimit time.Duration
consumerBurstSize int
logger *log.Logger
}
func NewClient(addr, queueName string, opts ...Option) (*Client, error) {
if addr == "" {
log.Fatal(errNoAddr)
func NewClient(address Address, queueName string, opts ...Option) (*Client, error) {
addr, err := address.makeAddr()
if err != nil {
return nil, errors.Join(errBadAddr, err)
}
if queueName == "" {
@ -44,7 +47,6 @@ func NewClient(addr, queueName string, opts ...Option) (*Client, error) {
client := Client{
mutex: &sync.Mutex{},
logger: log.New(os.Stdout, "", log.LstdFlags),
queueName: queueName,
done: make(chan bool),
connected: make(chan struct{}),
@ -52,18 +54,21 @@ func NewClient(addr, queueName string, opts ...Option) (*Client, error) {
o := options{
connectTimeout: 15 * time.Second,
reconnectDelay: 5,
reInitDelay: 2,
resendDelay: 5,
reconnectDelay: 5 * time.Second,
reInitDelay: 2 * time.Second,
resendDelay: 5 * time.Second,
consumerRateLimit: time.Millisecond * 500,
consumerBurstSize: 10,
logger: log.New(io.Discard, "", 0),
}
for _, opt := range opts {
opt(&o)
}
if err := client.connectAndSignal(addr, o.connectTimeout); err != nil {
client.logger = o.logger
if err = client.connectAndSignal(addr, o.connectTimeout); err != nil {
return nil, fmt.Errorf("failed to connect: %w", err)
}