Fix timing issue with subscribe in chat2 (#226)

- Support static node
- Ensure nodes are connected before subscribing
- Sleep to allow settling down before subscribing
- Default chat2 log to DEBUG
This commit is contained in:
Oskar Thorén
2020-10-14 18:34:29 +08:00
committed by GitHub
parent f31ab81546
commit d6fdecafd8
3 changed files with 17 additions and 3 deletions
+13 -2
View File
@@ -66,6 +66,11 @@ proc dialPeer(c: Chat, address: string) {.async.} =
discard await c.node.switch.dial(remotePeer, WakuRelayCodec)
c.connected = true
proc connectToNodes(c: Chat, nodes: openArray[string]) =
echo "Connecting to nodes"
for nodeId in nodes:
discard dialPeer(c, nodeId)
proc publish(c: Chat, line: string) =
let payload = cast[seq[byte]](line)
let message = WakuMessage(payload: payload, contentTopic: DefaultContentTopic)
@@ -158,6 +163,11 @@ proc processInput(rfd: AsyncFD, rng: ref BrHmacDrbgContext) {.async.} =
# waitFor vs await
await node.start()
var chat = Chat(node: node, transp: transp, subscribed: true, connected: false, started: true)
if conf.staticnodes.len > 0:
connectToNodes(chat, conf.staticnodes)
let peerInfo = node.peerInfo
let listenStr = $peerInfo.addrs[0] & "/p2p/" & $peerInfo.peerId
echo &"Listening on\n {listenStr}"
@@ -171,9 +181,10 @@ proc processInput(rfd: AsyncFD, rng: ref BrHmacDrbgContext) {.async.} =
let payload = cast[string](message.payload)
echo &"{payload}"
info "Hit subscribe handler", topic=topic, payload=payload, contentTopic=message.contentTopic
await node.subscribe(topic, handler)
var chat = Chat(node: node, transp: transp, subscribed: true, connected: false, started: true)
# XXX Timing issue with subscribe, need to wait a bit to ensure GRAFT message is sent
await sleepAsync(5.seconds)
await node.subscribe(topic, handler)
await chat.readWriteLoop()
runForever()