Skip to content

Commit

Permalink
fix: prevent store message on store when it's restored (#1255)
Browse files Browse the repository at this point in the history
  • Loading branch information
ogis-yamazaki committed Jun 28, 2023
1 parent a170c54 commit 8d68c8c
Show file tree
Hide file tree
Showing 4 changed files with 2,089 additions and 811 deletions.
15 changes: 13 additions & 2 deletions lib/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -1195,9 +1195,10 @@ MqttClient.prototype._cleanUp = function (forced, done) {
* @param {Object} packet - packet options
* @param {Function} cb - callback when the packet is sent
* @param {Function} cbStorePut - called when message is put into outgoingStore
* @param {Boolean} noStore - send without put to the store
* @api private
*/
MqttClient.prototype._sendPacket = function (packet, cb, cbStorePut) {
MqttClient.prototype._sendPacket = function (packet, cb, cbStorePut, noStore) {
debug('_sendPacket :: (%s) :: start', this.options.clientId)
cbStorePut = cbStorePut || nop
cb = cb || nop
Expand All @@ -1224,6 +1225,16 @@ MqttClient.prototype._sendPacket = function (packet, cb, cbStorePut) {
// When sending a packet, reschedule the ping timer
this._shiftPingInterval()

// If "noStore" is true, the message is sent without being recorded in the store.
// Messages that have not received puback or pubcomp remain in the store after disconnection
// and are resent from the store upon reconnection.
// For resend upon reconnection, "noStore" is set to true. This is because the message is already stored in the store.
// This is to avoid interrupting other processes while recording to the store.
if (noStore) {
sendPacket(this, packet, cb)
return
}

switch (packet.cmd) {
case 'publish':
break
Expand Down Expand Up @@ -1840,7 +1851,7 @@ MqttClient.prototype._onConnect = function (packet) {
}
that._packetIdsDuringStoreProcessing[packet.messageId] = true
if (that.messageIdProvider.register(packet.messageId)) {
that._sendPacket(packet)
that._sendPacket(packet, undefined, undefined, true)
} else {
debug('messageId: %d has already used.', packet.messageId)
}
Expand Down

0 comments on commit 8d68c8c

Please sign in to comment.