better exception handling

This commit is contained in:
Dmitriy Ryajov 2020-05-23 10:50:29 -06:00
parent d83ce4c932
commit 93e5805c01
3 changed files with 32 additions and 43 deletions

View File

@ -118,7 +118,7 @@ proc internalCleanup(p: PubSub, conn: Connection) {.async.} =
var peer = p.getPeer(conn.peerInfo, p.codec) var peer = p.getPeer(conn.peerInfo, p.codec)
await conn.closeEvent.wait() await conn.closeEvent.wait()
trace "connection closed, cleaning up peer", peer = conn.peerInfo.id trace "pubsub conn closed, cleaning up peer", peer = conn.peerInfo.id
await p.cleanUpHelper(peer) await p.cleanUpHelper(peer)
method handleConn*(p: PubSub, method handleConn*(p: PubSub,

View File

@ -78,7 +78,6 @@ proc handle*(p: PubSubPeer, conn: Connection) {.async.} =
await conn.close() await conn.close()
proc send*(p: PubSubPeer, msgs: seq[RPCMsg]) {.async.} = proc send*(p: PubSubPeer, msgs: seq[RPCMsg]) {.async.} =
try:
for m in msgs.items: for m in msgs.items:
trace "sending msgs to peer", toPeer = p.id, msgs = msgs trace "sending msgs to peer", toPeer = p.id, msgs = msgs
let encoded = encodeRpcMsg(m) let encoded = encodeRpcMsg(m)
@ -98,18 +97,16 @@ proc send*(p: PubSubPeer, msgs: seq[RPCMsg]) {.async.} =
continue continue
proc sendToRemote() {.async.} = proc sendToRemote() {.async.} =
trace "about send message", peer = p.id, try:
trace "about to send message", peer = p.id,
encoded = digest encoded = digest
await p.onConnect.wait() await p.onConnect.wait()
try:
trace "sending encoded msgs to peer", peer = p.id, trace "sending encoded msgs to peer", peer = p.id,
encoded = encoded.buffer.shortLog encoded = encoded.buffer.shortLog
await p.sendConn.writeLp(encoded.buffer) await p.sendConn.writeLp(encoded.buffer)
p.sentRpcCache.put(digest) p.sentRpcCache.put(digest)
except CatchableError as exc: except CatchableError as exc:
trace "unable to send to remote", exc = exc.msg trace "unable to send to remote", exc = exc.msg
if not(isNil(p.sendConn)):
await p.sendConn.close()
p.sendConn = nil p.sendConn = nil
p.onConnect.clear() p.onConnect.clear()
@ -118,13 +115,6 @@ proc send*(p: PubSubPeer, msgs: seq[RPCMsg]) {.async.} =
# becomes available # becomes available
asyncCheck sendToRemote() asyncCheck sendToRemote()
except CatchableError as exc:
trace "Exception occurred in PubSubPeer.send", exc = exc.msg
if not(isNil(p.sendConn)):
await p.sendConn.close()
p.sendConn = nil
p.onConnect.clear()
proc sendMsg*(p: PubSubPeer, proc sendMsg*(p: PubSubPeer,
peerId: PeerID, peerId: PeerID,
topic: string, topic: string,

View File

@ -244,7 +244,6 @@ proc encodeRpcMsg*(msg: RPCMsg): ProtoBuffer {.gcsafe.} =
proc decodeRpcMsg*(msg: seq[byte]): RPCMsg {.gcsafe.} = proc decodeRpcMsg*(msg: seq[byte]): RPCMsg {.gcsafe.} =
var pb = initProtoBuffer(msg) var pb = initProtoBuffer(msg)
result.subscriptions = newSeq[SubOpts]()
while true: while true:
# decode SubOpts array # decode SubOpts array
var field = pb.enterSubMessage() var field = pb.enterSubMessage()