-
Notifications
You must be signed in to change notification settings - Fork 54
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(networking): properly get number of relay connections
- Loading branch information
1 parent
418efca
commit 0352e21
Showing
3 changed files
with
159 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
when (NimMajor, NimMinor) < (1, 4): | ||
{.push raises: [Defect].} | ||
else: | ||
{.push raises: [].} | ||
|
||
import sequtils | ||
import chronos, chronicles | ||
|
||
# Taken from: https://github.com/status-im/nim-libp2p/blob/master/libp2p/utils/heartbeat.nim | ||
|
||
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: | ||
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 | ||
await sleepAsync(nextHeartbeat - now) |