different type of check connection
This commit is contained in:
parent
d62d41b41a
commit
ecda63761c
1 changed files with 21 additions and 10 deletions
31
handler.go
31
handler.go
|
|
@ -4,7 +4,6 @@ import (
|
|||
"fmt"
|
||||
amqp "github.com/rabbitmq/amqp091-go"
|
||||
"log"
|
||||
"net"
|
||||
"os"
|
||||
"sync"
|
||||
"time"
|
||||
|
|
@ -82,14 +81,26 @@ func NewConsumer(client *Client) Consumer {
|
|||
}
|
||||
|
||||
func (c *Client) connectAndSignal(addr string, timeout time.Duration) error {
|
||||
dialer := &net.Dialer{Timeout: timeout}
|
||||
conn, err := amqp.DialConfig(addr, amqp.Config{
|
||||
Dial: dialer.Dial,
|
||||
})
|
||||
if err != nil {
|
||||
return err
|
||||
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.connection = res.conn
|
||||
close(c.connected)
|
||||
return nil
|
||||
}
|
||||
c.connection = conn
|
||||
close(c.connected)
|
||||
return nil
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue