Compare commits

..

No commits in common. "main" and "v0.1.19" have entirely different histories.

View file

@ -28,15 +28,6 @@ func runConsumer(ctx context.Context, client *Client, msgCh chan []byte) {
limiter := rate.NewLimiter(rate.Every(client.opts.consumerRateLimit), client.opts.consumerBurstSize) 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() deliveries, err := client.consume()
if err != nil { if err != nil {
client.logger.Printf("Could not start consuming: %s\n", err) client.logger.Printf("Could not start consuming: %s\n", err)
@ -46,7 +37,18 @@ func runConsumer(ctx context.Context, client *Client, msgCh chan []byte) {
chClosedCh := make(chan *amqp.Error, 1) chClosedCh := make(chan *amqp.Error, 1)
client.Channel.NotifyClose(chClosedCh) client.Channel.NotifyClose(chClosedCh)
reconnectTimer := time.NewTimer(0)
defer reconnectTimer.Stop()
<-reconnectTimer.C
for { for {
if !reconnectTimer.Stop() {
select {
case <-reconnectTimer.C:
default:
}
}
select { select {
case <-runCtx.Done(): case <-runCtx.Done():
@ -56,33 +58,25 @@ func runConsumer(ctx context.Context, client *Client, msgCh chan []byte) {
} }
return return
case <-reconnectSignal: case amqErr := <-chClosedCh:
select { client.logger.Printf("AMQP Channel closed due to: %s Reconnecting...\n", amqErr)
case <-runCtx.Done(): reconnectTimer.Reset(time.Second)
return
case <-time.After(client.opts.reconnectDelay):
}
case <-reconnectTimer.C:
deliveries, err = client.consume() deliveries, err = client.consume()
if err != nil { if err != nil {
client.logger.Printf("Consume reconnect failed, retry: %s\n", err) client.logger.Println("Error trying to consume, will try again. Retry in 5 seconds.")
reconnectTrigger() reconnectTimer.Reset(time.Second * 5)
continue continue
} }
// re-create closing chan
chClosedCh = make(chan *amqp.Error, 1) chClosedCh = make(chan *amqp.Error, 1)
client.Channel.NotifyClose(chClosedCh) 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: case delivery, ok := <-deliveries:
if !ok { if !ok {
client.logger.Println("Deliveries channel closed unexpectedly") client.logger.Println("Deliveries channel closed unexpectedly")
reconnectTrigger() reconnectTimer.Reset(time.Second)
continue continue
} }