我从 RabbitMq 服务器收到此错误
服务器关闭的频道:406(PRECONDITION-FAILED),带有消息“PRECONDITION_FAILED - 未知的交付标签 80”
发生这种情况是因为在消费者任务期间连接丢失,最后,当消息被确认/确认时,我收到此错误,因为我无法在与我从中获取消息的通道不同的通道上确认消息。
这是 RabbitMq 连接的代码
async connect({ prefetch = 1, queueName }) {
this.queueName = queueName;
console.log(`[AMQP][${this.queueName}] | connecting`);
return queue
.connect(this.config.rabbitmq.connstring)
.then(conn => {
conn.once('error', err => {
this.channel = null;
if (err.message !== 'Connection closing') {
console.error(
`[AMQP][${this.queueName}] (evt:error) | ${err.message}`,
);
}
});
conn.once('close', () => {
this.channel = null;
console.error(
`[AMQP][${this.queueName}] (evt:close) | reconnecting`,
);
this.connect({ prefetch, queueName: this.queueName });
});
return conn.createChannel();
})
.then(ch => {
console.log(`[AMQP-channel][${this.queueName}] created`);
ch.on('error', err => {
console.error(
`[AMQP-ch][${this.queueName}] (evt:error) | ${err.message}`,
);
});
ch.on('close', () => {
console.error(`[AMQP-ch][${this.queueName}] (evt:close)`);
});
this.channel = ch;
return this.channel;
})
.then(ch => {
return this.channel.prefetch(prefetch);
})
.then(ch => {
return this.channel.assertQueue(this.queueName);
})
.then(async ch => {
while (this.buffer.length > 0) {
const request = this.buffer.pop();
await request();
}
return this.channel;
})
.catch(error => {
console.error(error);
console.log(`[AMQP][${this.queueName}] reconnecting in 1s`);
return this._delay(1000).then(() =>
this.connect({ prefetch, queueName: this.queueName }),
);
});
}
async ack(msg) {
try {
if (this.channel) {
console.log(`[AMQP][${this.queueName}] ack`);
await this.channel.ack(msg);
} else {
console.log(`[AMQP][${this.queueName}] ack (buffer)`);
this.buffer.push(() => {
this.ack(msg);
});
}
} catch (e) {
console.error(`[AMQ][${this.queueName}] ack error: ${e.message}`);
}
}
如您所见,在建立连接后,创建了一个通道,并且在我遇到连接问题后,通道设置为 NULL,1 秒后连接重试,重新创建一个新通道。
为了管理离线期间,我使用了一个缓冲区,该缓冲区收集在通道为 NULL 时发送的所有确认消息,并且在重新建立连接后我卸载缓冲区。
所以基本上我必须找到一种方法在连接丢失或通道因watherver原因关闭后发送ACK。
谢谢你的帮助