different type of check connection

This commit is contained in:
nquidox 2026-04-02 15:50:55 +03:00
parent d62d41b41a
commit ecda63761c

View file

@ -4,7 +4,6 @@ import (
"fmt" "fmt"
amqp "github.com/rabbitmq/amqp091-go" amqp "github.com/rabbitmq/amqp091-go"
"log" "log"
"net"
"os" "os"
"sync" "sync"
"time" "time"
@ -82,14 +81,26 @@ func NewConsumer(client *Client) Consumer {
} }
func (c *Client) connectAndSignal(addr string, timeout time.Duration) error { func (c *Client) connectAndSignal(addr string, timeout time.Duration) error {
dialer := &net.Dialer{Timeout: timeout} type result struct {
conn, err := amqp.DialConfig(addr, amqp.Config{ conn *amqp.Connection
Dial: dialer.Dial, err error
}) }
if err != nil { resCh := make(chan result, 1)
return err
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.connection = res.conn
close(c.connected)
return nil
} }
c.connection = conn
close(c.connected)
return nil
} }