Compare commits
2 commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
72f039e4f7 | ||
|
|
fd495892fa |
3 changed files with 53 additions and 29 deletions
42
consumer.go
42
consumer.go
|
|
@ -28,6 +28,15 @@ func runConsumer(ctx context.Context, client *Client, msgCh chan []byte) {
|
|||
|
||||
limiter := rate.NewLimiter(rate.Every(client.opts.consumerRateLimit), client.opts.consumerBurstSize)
|
||||
|
||||
reconnectSignal := make(chan struct{}, 1)
|
||||
reconnectTrigger := func() {
|
||||
select {
|
||||
case reconnectSignal <- struct{}{}:
|
||||
default:
|
||||
}
|
||||
}
|
||||
|
||||
// initial consume
|
||||
deliveries, err := client.consume()
|
||||
if err != nil {
|
||||
client.logger.Printf("Could not start consuming: %s\n", err)
|
||||
|
|
@ -37,18 +46,7 @@ func runConsumer(ctx context.Context, client *Client, msgCh chan []byte) {
|
|||
chClosedCh := make(chan *amqp.Error, 1)
|
||||
client.Channel.NotifyClose(chClosedCh)
|
||||
|
||||
reconnectTimer := time.NewTimer(0)
|
||||
defer reconnectTimer.Stop()
|
||||
<-reconnectTimer.C
|
||||
|
||||
for {
|
||||
if !reconnectTimer.Stop() {
|
||||
select {
|
||||
case <-reconnectTimer.C:
|
||||
default:
|
||||
}
|
||||
}
|
||||
|
||||
select {
|
||||
|
||||
case <-runCtx.Done():
|
||||
|
|
@ -58,25 +56,33 @@ func runConsumer(ctx context.Context, client *Client, msgCh chan []byte) {
|
|||
}
|
||||
return
|
||||
|
||||
case amqErr := <-chClosedCh:
|
||||
client.logger.Printf("AMQP Channel closed due to: %s Reconnecting...\n", amqErr)
|
||||
reconnectTimer.Reset(time.Second)
|
||||
case <-reconnectSignal:
|
||||
select {
|
||||
case <-runCtx.Done():
|
||||
return
|
||||
case <-time.After(client.opts.reconnectDelay):
|
||||
}
|
||||
|
||||
case <-reconnectTimer.C:
|
||||
deliveries, err = client.consume()
|
||||
if err != nil {
|
||||
client.logger.Println("Error trying to consume, will try again. Retry in 5 seconds.")
|
||||
reconnectTimer.Reset(time.Second * 5)
|
||||
client.logger.Printf("Consume reconnect failed, retry: %s\n", err)
|
||||
reconnectTrigger()
|
||||
continue
|
||||
}
|
||||
|
||||
// re-create closing chan
|
||||
chClosedCh = make(chan *amqp.Error, 1)
|
||||
client.Channel.NotifyClose(chClosedCh)
|
||||
client.logger.Println("Consume reconnect success")
|
||||
|
||||
case amqErr := <-chClosedCh:
|
||||
client.logger.Printf("AMQP Channel closed due to: %s Reconnecting...\n", amqErr)
|
||||
reconnectTrigger()
|
||||
|
||||
case delivery, ok := <-deliveries:
|
||||
if !ok {
|
||||
client.logger.Println("Deliveries channel closed unexpectedly")
|
||||
reconnectTimer.Reset(time.Second)
|
||||
reconnectTrigger()
|
||||
continue
|
||||
}
|
||||
|
||||
|
|
|
|||
17
handler.go
17
handler.go
|
|
@ -13,7 +13,7 @@ import (
|
|||
|
||||
type Client struct {
|
||||
mutex *sync.Mutex
|
||||
queueName string
|
||||
queueOptions *QueueOpts
|
||||
logger *log.Logger
|
||||
connection *amqp.Connection
|
||||
Channel *amqp.Channel
|
||||
|
|
@ -36,7 +36,16 @@ type options struct {
|
|||
logger *log.Logger
|
||||
}
|
||||
|
||||
func NewClient(address Address, queueName string, opts ...Option) (*Client, error) {
|
||||
type QueueOpts struct {
|
||||
QueueName string
|
||||
Durable bool
|
||||
AutoDelete bool
|
||||
Exclusive bool
|
||||
NoWait bool
|
||||
Args amqp.Table
|
||||
}
|
||||
|
||||
func NewClient(address Address, queueOpts QueueOpts, opts ...Option) (*Client, error) {
|
||||
l := log.New(os.Stdout, "", log.LstdFlags)
|
||||
|
||||
addr, err := address.makeAddr()
|
||||
|
|
@ -44,13 +53,13 @@ func NewClient(address Address, queueName string, opts ...Option) (*Client, erro
|
|||
return nil, errors.Join(errBadAddr, err)
|
||||
}
|
||||
|
||||
if queueName == "" {
|
||||
if queueOpts.QueueName == "" {
|
||||
l.Fatal(errNoQueue)
|
||||
}
|
||||
|
||||
client := Client{
|
||||
mutex: &sync.Mutex{},
|
||||
queueName: queueName,
|
||||
queueOptions: &queueOpts,
|
||||
done: make(chan bool),
|
||||
connected: make(chan struct{}),
|
||||
logger: l,
|
||||
|
|
|
|||
15
service.go
15
service.go
|
|
@ -78,7 +78,16 @@ func (c *Client) init(conn *amqp.Connection) error {
|
|||
return err
|
||||
}
|
||||
|
||||
_, err = ch.QueueDeclare(c.queueName, false, false, false, false, nil)
|
||||
//_, err = ch.QueueDeclare(c.queueName, false, false, false, false, nil)
|
||||
_, err = ch.QueueDeclare(
|
||||
c.queueOptions.QueueName,
|
||||
c.queueOptions.Durable,
|
||||
c.queueOptions.AutoDelete,
|
||||
c.queueOptions.Exclusive,
|
||||
c.queueOptions.NoWait,
|
||||
c.queueOptions.Args,
|
||||
)
|
||||
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
|
@ -120,7 +129,7 @@ func (c *Client) unsafePush(data []byte) error {
|
|||
return c.Channel.PublishWithContext(
|
||||
ctx,
|
||||
"",
|
||||
c.queueName,
|
||||
c.queueOptions.QueueName,
|
||||
false,
|
||||
false,
|
||||
amqp.Publishing{
|
||||
|
|
@ -147,7 +156,7 @@ func (c *Client) consume() (<-chan amqp.Delivery, error) {
|
|||
}
|
||||
|
||||
return c.Channel.Consume(
|
||||
c.queueName,
|
||||
c.queueOptions.QueueName,
|
||||
"",
|
||||
false,
|
||||
false,
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue