Skip to content

Commit

Permalink
Parallelize unsubscribe handler (#317)
Browse files Browse the repository at this point in the history
  • Loading branch information
gnought authored and mcollina committed Oct 2, 2019
1 parent 24b73f5 commit 2b80c6f
Showing 1 changed file with 10 additions and 8 deletions.
18 changes: 10 additions & 8 deletions lib/handlers/unsubscribe.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,10 @@
var write = require('../write')
var validateTopic = require('./validations').validateTopic

function UnsubscribeState (client, packet, finish, granted) {
function UnsubscribeState (client, packet, finish) {
this.client = client
this.packet = packet
this.finish = finish
this.granted = granted
}

function handleUnsubscribe (client, packet, done) {
Expand Down Expand Up @@ -37,8 +36,8 @@ function handleUnsubscribe (client, packet, done) {

function actualUnsubscribe (client, packet, done) {
var broker = client.broker
broker._series(
new UnsubscribeState(client, packet, done, null),
broker._parallel(
new UnsubscribeState(client, packet, done),
doUnsubscribe,
packet.unsubscriptions,
completeUnsubscribe)
Expand All @@ -47,8 +46,10 @@ function actualUnsubscribe (client, packet, done) {
function doUnsubscribe (sub, done) {
var client = this.client
var broker = client.broker
if (client.subscriptions[sub]) {
var func = client.subscriptions[sub].func
var s = client.subscriptions[sub]

if (s) {
var func = s.func
delete client.subscriptions[sub]
broker.unsubscribe(
sub,
Expand All @@ -60,15 +61,16 @@ function doUnsubscribe (sub, done) {
}

function completeUnsubscribe (err) {
var packet = this.packet
var client = this.client
var done = this.finish

if (err) {
client.emit('error', err)
return
}

var packet = this.packet
var done = this.finish

if ((!packet.close || client.clean === true) && packet.unsubscriptions.length > 0) {
client.broker.emit('unsubscribe', packet.unsubscriptions, client)
}
Expand Down

0 comments on commit 2b80c6f

Please sign in to comment.