Skip to content

Commit 8d68c8c

Browse files
fix: prevent store message on store when it's restored (#1255)
1 parent a170c54 commit 8d68c8c

File tree

4 files changed

+2089
-811
lines changed

4 files changed

+2089
-811
lines changed

lib/client.js

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1195,9 +1195,10 @@ MqttClient.prototype._cleanUp = function (forced, done) {
11951195
* @param {Object} packet - packet options
11961196
* @param {Function} cb - callback when the packet is sent
11971197
* @param {Function} cbStorePut - called when message is put into outgoingStore
1198+
* @param {Boolean} noStore - send without put to the store
11981199
* @api private
11991200
*/
1200-
MqttClient.prototype._sendPacket = function (packet, cb, cbStorePut) {
1201+
MqttClient.prototype._sendPacket = function (packet, cb, cbStorePut, noStore) {
12011202
debug('_sendPacket :: (%s) :: start', this.options.clientId)
12021203
cbStorePut = cbStorePut || nop
12031204
cb = cb || nop
@@ -1224,6 +1225,16 @@ MqttClient.prototype._sendPacket = function (packet, cb, cbStorePut) {
12241225
// When sending a packet, reschedule the ping timer
12251226
this._shiftPingInterval()
12261227

1228+
// If "noStore" is true, the message is sent without being recorded in the store.
1229+
// Messages that have not received puback or pubcomp remain in the store after disconnection
1230+
// and are resent from the store upon reconnection.
1231+
// For resend upon reconnection, "noStore" is set to true. This is because the message is already stored in the store.
1232+
// This is to avoid interrupting other processes while recording to the store.
1233+
if (noStore) {
1234+
sendPacket(this, packet, cb)
1235+
return
1236+
}
1237+
12271238
switch (packet.cmd) {
12281239
case 'publish':
12291240
break
@@ -1840,7 +1851,7 @@ MqttClient.prototype._onConnect = function (packet) {
18401851
}
18411852
that._packetIdsDuringStoreProcessing[packet.messageId] = true
18421853
if (that.messageIdProvider.register(packet.messageId)) {
1843-
that._sendPacket(packet)
1854+
that._sendPacket(packet, undefined, undefined, true)
18441855
} else {
18451856
debug('messageId: %d has already used.', packet.messageId)
18461857
}

0 commit comments

Comments
 (0)