Created
May 2, 2020 23:37
-
-
Save lbarasti/2e5fe95aa09a4590c9c85d462f03edc8 to your computer and use it in GitHub Desktop.
select use case 5: Heartbeats
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
def log(msg) | |
puts "#{Fiber.current.name}: #{msg}" | |
end | |
record Heartbeat, id : UInt64 = Fiber.current.object_id | |
Diagnostic = Channel(Heartbeat).new | |
diagnostic_enabled = true # tweak this value | |
if diagnostic_enabled | |
spawn(name: "diagnostic_deamon") do | |
loop do | |
case msg = Diagnostic.receive | |
when Heartbeat | |
log "received Heartbeat from #{msg.id}" | |
else | |
end | |
end | |
end | |
end | |
spawn(name: "worker") do | |
select | |
when Diagnostic.send Heartbeat.new | |
log "sent heartbeat" | |
else | |
log "suppressed heartbeat" | |
end | |
end | |
sleep 2 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment