emulate defered

This commit is contained in:
Dmitriy Ryajov 2020-06-02 10:27:39 -06:00
parent 2ecc699b6e
commit a73bc40ae4
No known key found for this signature in database
GPG Key ID: DA8C680CE7C657A4
2 changed files with 89 additions and 92 deletions

View File

@ -67,101 +67,102 @@ proc newStreamInternal*(m: Mplex,
method handle*(m: Mplex) {.async, gcsafe.} = method handle*(m: Mplex) {.async, gcsafe.} =
trace "starting mplex main loop", oid = m.oid trace "starting mplex main loop", oid = m.oid
try: try:
while not m.connection.closed: try:
trace "waiting for data", oid = m.oid while not m.connection.closed:
let (id, msgType, data) = await m.connection.readMsg() trace "waiting for data", oid = m.oid
trace "read message from connection", id = id, let (id, msgType, data) = await m.connection.readMsg()
trace "read message from connection", id = id,
msgType = msgType,
data = data.shortLog,
oid = m.oid
let initiator = bool(ord(msgType) and 1)
var channel: LPChannel
if MessageType(msgType) != MessageType.New:
let channels = m.getChannelList(initiator)
if id notin channels:
trace "Channel not found, skipping", id = id,
initiator = initiator,
msg = msgType,
oid = m.oid
continue
channel = channels[id]
case msgType:
of MessageType.New:
let name = cast[string](data)
channel = await m.newStreamInternal(false, id, name)
trace "created channel", id = id,
name = name,
inititator = channel.initiator,
channoid = channel.oid,
oid = m.oid
if not isNil(m.streamHandler):
let stream = newConnection(channel)
m.conns.add(stream)
stream.peerInfo = m.connection.peerInfo
var fut = newFuture[void]()
proc handler() {.async.} =
try:
await m.streamHandler(stream)
except CatchableError as exc:
trace "exception in stream handler", exc = exc.msg
finally:
m.conns.keepItIf(it != stream)
m.handlerFuts.keepItIf(it != fut)
fut = handler()
of MessageType.MsgIn, MessageType.MsgOut:
trace "pushing data to channel", id = id,
initiator = initiator,
msgType = msgType, msgType = msgType,
data = data.shortLog, size = data.len,
name = channel.name,
channoid = channel.oid,
oid = m.oid oid = m.oid
let initiator = bool(ord(msgType) and 1)
var channel: LPChannel
if MessageType(msgType) != MessageType.New:
let channels = m.getChannelList(initiator)
if id notin channels:
trace "Channel not found, skipping", id = id,
initiator = initiator,
msg = msgType,
oid = m.oid
continue
channel = channels[id]
case msgType: if data.len > MaxMsgSize:
of MessageType.New: raise newLPStreamLimitError()
let name = cast[string](data) await channel.pushTo(data)
channel = await m.newStreamInternal(false, id, name) of MessageType.CloseIn, MessageType.CloseOut:
trace "created channel", id = id, trace "closing channel", id = id,
name = name, initiator = initiator,
inititator = channel.initiator, msgType = msgType,
channoid = channel.oid, name = channel.name,
oid = m.oid channoid = channel.oid,
if not isNil(m.streamHandler): oid = m.oid
let stream = newConnection(channel)
m.conns.add(stream)
stream.peerInfo = m.connection.peerInfo
var fut = newFuture[void]() await channel.closeRemote()
proc handler() {.async.} = m.getChannelList(initiator).del(id)
try: trace "deleted channel", id = id,
await m.streamHandler(stream) initiator = initiator,
except CatchableError as exc: msgType = msgType,
trace "exception in stream handler", exc = exc.msg name = channel.name,
finally: channoid = channel.oid,
m.conns.keepItIf(it != stream) oid = m.oid
m.handlerFuts.keepItIf(it != fut) of MessageType.ResetIn, MessageType.ResetOut:
trace "resetting channel", id = id,
initiator = initiator,
msgType = msgType,
name = channel.name,
channoid = channel.oid,
oid = m.oid
fut = handler() await channel.reset()
m.getChannelList(initiator).del(id)
of MessageType.MsgIn, MessageType.MsgOut: trace "deleted channel", id = id,
trace "pushing data to channel", id = id, initiator = initiator,
initiator = initiator, msgType = msgType,
msgType = msgType, name = channel.name,
size = data.len, channoid = channel.oid,
name = channel.name, oid = m.oid
channoid = channel.oid, break
oid = m.oid finally:
trace "stopping mplex main loop", oid = m.oid
if data.len > MaxMsgSize: await m.close()
raise newLPStreamLimitError()
await channel.pushTo(data)
of MessageType.CloseIn, MessageType.CloseOut:
trace "closing channel", id = id,
initiator = initiator,
msgType = msgType,
name = channel.name,
channoid = channel.oid,
oid = m.oid
await channel.closeRemote()
m.getChannelList(initiator).del(id)
trace "deleted channel", id = id,
initiator = initiator,
msgType = msgType,
name = channel.name,
channoid = channel.oid,
oid = m.oid
of MessageType.ResetIn, MessageType.ResetOut:
trace "resetting channel", id = id,
initiator = initiator,
msgType = msgType,
name = channel.name,
channoid = channel.oid,
oid = m.oid
await channel.reset()
m.getChannelList(initiator).del(id)
trace "deleted channel", id = id,
initiator = initiator,
msgType = msgType,
name = channel.name,
channoid = channel.oid,
oid = m.oid
break
except CatchableError as exc: except CatchableError as exc:
trace "Exception occurred", exception = exc.msg, oid = m.oid trace "Exception occurred", exception = exc.msg, oid = m.oid
finally:
trace "stopping mplex main loop", oid = m.oid
await m.close()
proc newMplex*(conn: Connection, proc newMplex*(conn: Connection,
maxChanns: uint = MaxChannels): Mplex = maxChanns: uint = MaxChannels): Mplex =

View File

@ -83,7 +83,6 @@ proc testPubSubDaemonPublish(gossip: bool = false,
let smsg = cast[string](data) let smsg = cast[string](data)
check smsg == pubsubData check smsg == pubsubData
times.inc() times.inc()
echo "TIMES ", times
if times >= count and not finished: if times >= count and not finished:
finished = true finished = true
@ -108,7 +107,6 @@ proc testPubSubDaemonPublish(gossip: bool = false,
await wait(publisher(), 5.minutes) # should be plenty of time await wait(publisher(), 5.minutes) # should be plenty of time
echo "HEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEE"
result = true result = true
await nativeNode.stop() await nativeNode.stop()
await allFutures(awaiters) await allFutures(awaiters)
@ -144,7 +142,6 @@ proc testPubSubNodePublish(gossip: bool = false,
let smsg = cast[string](message.data) let smsg = cast[string](message.data)
check smsg == pubsubData check smsg == pubsubData
times.inc() times.inc()
echo "TIMES ", times
if times >= count and not finished: if times >= count and not finished:
finished = true finished = true
result = true # don't cancel subscription result = true # don't cancel subscription
@ -356,7 +353,6 @@ suite "Interop":
check line == test check line == test
await conn.writeLp(cast[seq[byte]](test)) await conn.writeLp(cast[seq[byte]](test))
count.inc() count.inc()
echo "COUNT ", count
testFuture.complete(count) testFuture.complete(count)
await conn.close() await conn.close()