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"
|
"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
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue