Skip to content

Commit

Permalink
Fix error handling
Browse files Browse the repository at this point in the history
 - Add try catch around bztt client to prevent server from crashing
 - Add check destroyed for socket.send and socket.end
  • Loading branch information
arwassa committed Aug 12, 2020
1 parent 1936674 commit 4270022
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 12 deletions.
26 changes: 19 additions & 7 deletions src/bztt-client.js
Original file line number Diff line number Diff line change
Expand Up @@ -105,14 +105,26 @@ export class BZTTClientConnection {
this.onDestroy()
}

write(...a){
if(!this.socket.destroyed){
this.socket.write(...a)
}
}

end(...a){
if(!this.socket.destroyed){
this.socket.end(...a)
}
}

handleCompleteMessage(msg) {
log.debug(`Got message ${msg.type} ${msg.id}`)
if (this.connected) {
switch (msg.type) {
case T_DISCONNECT: {
this.connectedUser = undefined
this.connected = false
this.socket.end(
this.end(
this._createMessage(T_DISCONNECT_ACK, {id: msg.id}, 0),
)
break
Expand All @@ -124,13 +136,13 @@ export class BZTTClientConnection {
const subscribtion = topic
.observe(this.connectedUser)
.subscribe((payload) => {
this.socket.write(
this.write(
this._createMessage(T_PUSH, {topic: topicName}, payload),
)
})
this.subscribedTopics.set(topicName, subscribtion)
}
this.socket.write(
this.write(
this._createMessage(T_SUBSCRIBE_ACK, {id: msg.id}, 0),
)
break
Expand All @@ -141,7 +153,7 @@ export class BZTTClientConnection {
this.subscribedTopics.get(topicName).unsubscribe()
this.subscribedTopics.delete(topicName)
}
this.socket.write(
this.write(
this._createMessage(T_UNSUBSCRIBE_ACK, {id: msg.id}, 0),
)
break
Expand All @@ -151,11 +163,11 @@ export class BZTTClientConnection {
const topic = this.topicManager.getTopic(msg.payload.topic)
if(topic){
topic.publish(this.connectedUser, msg.payload.data)
this.socket.write(this._createMessage(T_PUBLISH_ACK, {id: msg.id}, 0))
this.write(this._createMessage(T_PUBLISH_ACK, {id: msg.id}, 0))
}
else{
log.debug({error: `Topic does not exist ${msg.payload.topic}`})
this.socket.write(this._createMessage(T_PUBLISH_ACK, {id: msg.id}, 1))
this.write(this._createMessage(T_PUBLISH_ACK, {id: msg.id}, 1))
}
break
}
Expand All @@ -172,7 +184,7 @@ export class BZTTClientConnection {
this.connectedUser = msg.payload
this.connected = true
log.debug(`User connected: ${this.connectedUser.userId} ${this.connectedUser.username}`)
this.socket.write(this._createMessage(T_CONNECT_ACK, {id: msg.id}, 0))
this.write(this._createMessage(T_CONNECT_ACK, {id: msg.id}, 0))
break
}

Expand Down
14 changes: 9 additions & 5 deletions src/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,13 @@ server.listen(BIND_PORT, BIND_ADDRESS, () => {
const bzttClients = new Set()

server.on("connection", (socket) => {
socket.setKeepAlive(true, 60 * 1000)
const connection = new BZTTClientConnection(socket, topicManager, () =>
bzttClients.delete(connection),
)
bzttClients.add(connection)
try{
socket.setKeepAlive(true, 60 * 1000)
const connection = new BZTTClientConnection(socket, topicManager, () =>
bzttClients.delete(connection),
)
bzttClients.add(connection)
} catch(err) {
log.error({err, message: 'BZTTClient crashed!'})
}
})

0 comments on commit 4270022

Please sign in to comment.