2022-05-11 08:38:43 +00:00
|
|
|
# Nim-Libp2p
|
|
|
|
# Copyright (c) 2022 Status Research & Development GmbH
|
|
|
|
# Licensed under either of
|
|
|
|
# * Apache License, version 2.0, ([LICENSE-APACHE](LICENSE-APACHE))
|
|
|
|
# * MIT license ([LICENSE-MIT](LICENSE-MIT))
|
|
|
|
# at your option.
|
|
|
|
# This file may not be copied, modified, or distributed except according to
|
|
|
|
# those terms.
|
|
|
|
|
2022-08-03 11:33:19 +00:00
|
|
|
when (NimMajor, NimMinor) < (1, 4):
|
|
|
|
{.push raises: [Defect].}
|
|
|
|
else:
|
|
|
|
{.push raises: [].}
|
2022-05-11 08:38:43 +00:00
|
|
|
|
|
|
|
import sequtils
|
|
|
|
import chronos, chronicles
|
|
|
|
|
|
|
|
export chronicles
|
|
|
|
|
|
|
|
template heartbeat*(name: string, interval: Duration, body: untyped): untyped =
|
|
|
|
var nextHeartbeat = Moment.now()
|
|
|
|
while true:
|
|
|
|
body
|
|
|
|
|
|
|
|
nextHeartbeat += interval
|
|
|
|
let now = Moment.now()
|
|
|
|
if nextHeartbeat < now:
|
2022-09-12 15:09:10 +00:00
|
|
|
let
|
|
|
|
delay = now - nextHeartbeat
|
|
|
|
itv = interval
|
|
|
|
if delay > itv:
|
|
|
|
info "Missed multiple heartbeats", heartbeat = name,
|
|
|
|
delay = delay, hinterval = itv
|
|
|
|
else:
|
|
|
|
debug "Missed heartbeat", heartbeat = name,
|
|
|
|
delay = delay, hinterval = itv
|
|
|
|
nextHeartbeat = now + itv
|
2022-05-11 08:38:43 +00:00
|
|
|
await sleepAsync(nextHeartbeat - now)
|