| 1 |
package amqp |
| 2 |
|
| 3 |
import ( |
| 4 |
"crypto/tls" |
| 5 |
"time" |
| 6 |
|
| 7 |
"github.com/NeowayLabs/wabbit" |
| 8 |
"github.com/NeowayLabs/wabbit/utils" |
| 9 |
amqp "github.com/rabbitmq/amqp091-go" |
| 10 |
) |
| 11 |
|
| 12 |
// Conn is the amqp connection |
| 13 |
type Conn struct { |
| 14 |
*amqp.Connection |
| 15 |
|
| 16 |
// closure info of connection |
| 17 |
dialFn func() error |
| 18 |
attempts uint8 |
| 19 |
} |
| 20 |
|
| 21 |
func doDial(conn *Conn, dialFn func() error) (*Conn, error) { |
| 22 |
conn.dialFn = dialFn |
| 23 |
|
| 24 |
err := conn.dialFn() |
| 25 |
if err != nil { |
| 26 |
return nil, err |
| 27 |
} |
| 28 |
|
| 29 |
return conn, nil |
| 30 |
} |
| 31 |
|
| 32 |
// Dial connects to an AMQP broker, with defaults |
| 33 |
func Dial(uri string) (*Conn, error) { |
| 34 |
conn := &Conn{} |
| 35 |
|
| 36 |
return doDial(conn, func() error { |
| 37 |
var err error |
| 38 |
conn.Connection, err = amqp.Dial(uri) |
| 39 |
if err != nil { |
| 40 |
return err |
| 41 |
} |
| 42 |
return nil |
| 43 |
}) |
| 44 |
} |
| 45 |
|
| 46 |
// DialTLS connects to an AMQP broker, with TLS config |
| 47 |
func DialTLS(uri string, tlsconfig *tls.Config) (*Conn, error) { |
| 48 |
conn := &Conn{} |
| 49 |
|
| 50 |
return doDial(conn, func() error { |
| 51 |
var err error |
| 52 |
conn.Connection, err = amqp.DialTLS(uri, tlsconfig) |
| 53 |
if err != nil { |
| 54 |
return err |
| 55 |
} |
| 56 |
return nil |
| 57 |
}) |
| 58 |
} |
| 59 |
|
| 60 |
// DialConfig connects to an AMQP broker, with custom config |
| 61 |
func DialConfig(uri string, config amqp.Config) (*Conn, error) { |
| 62 |
conn := &Conn{} |
| 63 |
|
| 64 |
return doDial(conn, func() error { |
| 65 |
var err error |
| 66 |
conn.Connection, err = amqp.DialConfig(uri, config) |
| 67 |
if err != nil { |
| 68 |
return err |
| 69 |
} |
| 70 |
return nil |
| 71 |
}) |
| 72 |
} |
| 73 |
|
| 74 |
// NotifyClose registers a listener for close events. |
| 75 |
// For more information see: https://godoc.org/github.com/rabbitmq/amqp091-go#Connection.NotifyClose |
| 76 |
func (conn *Conn) NotifyClose(c chan wabbit.Error) chan wabbit.Error { |
| 77 |
amqpErr := conn.Connection.NotifyClose(make(chan *amqp.Error, cap(c))) |
| 78 |
|
| 79 |
go func() { |
| 80 |
for err := range amqpErr { |
| 81 |
var ne wabbit.Error |
| 82 |
if err != nil { |
| 83 |
ne = utils.NewError( |
| 84 |
err.Code, |
| 85 |
err.Reason, |
| 86 |
err.Server, |
| 87 |
err.Recover, |
| 88 |
) |
| 89 |
} else { |
| 90 |
ne = nil |
| 91 |
} |
| 92 |
c <- ne |
| 93 |
} |
| 94 |
close(c) |
| 95 |
}() |
| 96 |
|
| 97 |
return c |
| 98 |
} |
| 99 |
|
| 100 |
// AutoRedial manages the automatic redial of connection when unexpected closed. |
| 101 |
// outChan is an unbuffered channel required to receive the errors that results from |
| 102 |
// attempts of reconnect. On successfully reconnected, the true value is sent to done channel |
| 103 |
// |
| 104 |
// The outChan parameter can receive *amqp.Error for AMQP connection errors |
| 105 |
// or errors.Error for any other net/tcp internal error. |
| 106 |
// |
| 107 |
// Redial strategy: |
| 108 |
// If the connection is closed in an unexpected way (opposite of conn.Close()), then |
| 109 |
// AutoRedial will try to automatically reconnect waiting for N seconds before each |
| 110 |
// attempt, where N is the number of attempts of reconnecting. If the number of |
| 111 |
// attempts reach 60, it will be zero'ed. |
| 112 |
func (conn *Conn) AutoRedial(outChan chan wabbit.Error, done chan bool) { |
| 113 |
errChan2 := make(chan wabbit.Error) |
| 114 |
errChan := conn.NotifyClose(errChan2) |
| 115 |
|
| 116 |
go func() { |
| 117 |
var err wabbit.Error |
| 118 |
|
| 119 |
select { |
| 120 |
case amqpErr := <-errChan: |
| 121 |
err = amqpErr |
| 122 |
|
| 123 |
if amqpErr == nil { |
| 124 |
// Gracefull connection close |
| 125 |
return |
| 126 |
} |
| 127 |
attempts: |
| 128 |
outChan <- err |
| 129 |
|
| 130 |
if conn.attempts > 60 { |
| 131 |
conn.attempts = 0 |
| 132 |
} |
| 133 |
|
| 134 |
// Wait n Seconds where n == conn.attempts... |
| 135 |
time.Sleep(time.Duration(conn.attempts) * time.Second) |
| 136 |
|
| 137 |
connErr := conn.dialFn() |
| 138 |
|
| 139 |
if connErr != nil { |
| 140 |
conn.attempts++ |
| 141 |
goto attempts |
| 142 |
} |
| 143 |
|
| 144 |
// enabled AutoRedial on the new connection |
| 145 |
conn.AutoRedial(outChan, done) |
| 146 |
done <- true |
| 147 |
return |
| 148 |
} |
| 149 |
}() |
| 150 |
} |
| 151 |
|
| 152 |
// Channel returns a new channel ready to be used |
| 153 |
func (conn *Conn) Channel() (wabbit.Channel, error) { |
| 154 |
ch, err := conn.Connection.Channel() |
| 155 |
|
| 156 |
if err != nil { |
| 157 |
return nil, err |
| 158 |
} |
| 159 |
|
| 160 |
return &Channel{ch}, nil |
| 161 |
} |