Skip to content

Commit

Permalink
feat(wakucanary): add latency measurement using ping protocol (#2074)
Browse files Browse the repository at this point in the history
  • Loading branch information
vpavlin authored Sep 26, 2023
1 parent a27d005 commit 6cb9a8d
Showing 1 changed file with 32 additions and 1 deletion.
33 changes: 32 additions & 1 deletion apps/wakucanary/wakucanary.nim
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ type

logLevel* {.
desc: "Sets the log level",
defaultValue: LogLevel.DEBUG,
defaultValue: LogLevel.INFO,
name: "log-level",
abbr: "l".}: LogLevel

Expand All @@ -68,6 +68,11 @@ type
defaultValue: ""
name: "websocket-secure-cert-path".}: string

ping* {.
desc: "Ping the peer node to measure latency",
defaultValue: true,
name: "ping" .}: bool

proc parseCmdArg*(T: type chronos.Duration, p: string): T =
try:
result = chronos.seconds(parseInt(p))
Expand Down Expand Up @@ -98,6 +103,18 @@ proc areProtocolsSupported(

return false

proc pingNode(node: WakuNode, peerInfo: RemotePeerInfo): Future[void] {.async, gcsafe.} =
try:
let conn = await node.switch.dial(peerInfo.peerId, peerInfo.addrs, PingCodec)
let pingDelay = await node.libp2pPing.ping(conn)
info "Peer response time (ms)", peerId = peerInfo.peerId, ping=pingDelay.millis

except CatchableError:
var msg = getCurrentExceptionMsg()
if msg == "Future operation cancelled!":
msg = "timedout"
error "Failed to ping the peer", peer=peerInfo, err=msg

proc main(rng: ref HmacDrbgContext): Future[int] {.async.} =
let conf: WakuCanaryConf = WakuCanaryConf.load()

Expand Down Expand Up @@ -173,8 +190,19 @@ proc main(rng: ref HmacDrbgContext): Future[int] {.async.} =

let node = builder.build().tryGet()

if conf.ping:
try:
await mountLibp2pPing(node)
except CatchableError:
error "failed to mount libp2p ping protocol: " & getCurrentExceptionMsg()
return 1

await node.start()

var pingFut:Future[bool]
if conf.ping:
pingFut = pingNode(node, peer).withTimeout(conf.timeout)

let timedOut = not await node.connectToNodes(@[peer]).withTimeout(conf.timeout)
if timedOut:
error "Timedout after", timeout = conf.timeout
Expand All @@ -183,6 +211,9 @@ proc main(rng: ref HmacDrbgContext): Future[int] {.async.} =
let lp2pPeerStore = node.switch.peerStore
let conStatus = node.peerManager.peerStore[ConnectionBook][peer.peerId]

if conf.ping:
discard await pingFut

if conStatus in [Connected, CanConnect]:
let nodeProtocols = lp2pPeerStore[ProtoBook][peer.peerId]
if not areProtocolsSupported(conf.protocols, nodeProtocols):
Expand Down

0 comments on commit 6cb9a8d

Please sign in to comment.