From dee0243268b6a2f67f40463b4dcab71da8d47f80 Mon Sep 17 00:00:00 2001 From: Zuckerberg Date: Tue, 7 Mar 2023 22:55:37 -0700 Subject: [PATCH] Peer to peer connection keepalive task --- common/network/ping.nix | 59 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 59 insertions(+) create mode 100644 common/network/ping.nix diff --git a/common/network/ping.nix b/common/network/ping.nix new file mode 100644 index 0000000..5f4c92e --- /dev/null +++ b/common/network/ping.nix @@ -0,0 +1,59 @@ +{ config, pkgs, lib, ... }: + +# keeps peer to peer connections alive with a periodic ping + +with lib; +with builtins; + +# todo auto restart + +let + cfg = config.keepalive-ping; + + # keepalive-ping = { + serviceTemplate = host: + { + "keepalive-ping@${host}" = { + description = "Periodic ping keep alive for ${host} connection"; + + requires = [ "network-online.target" ]; + after = [ "network.target" "network-online.target" ]; + wantedBy = [ "multi-user.target" ]; + serviceConfig.Restart="always"; + + path = with pkgs; [ iputils ]; + + script = '' + ping -i ${cfg.delay} ${host} &>/dev/null + ''; + }; + }; + + combineAttrs = foldl recursiveUpdate {}; + + serviceList = map serviceTemplate cfg.hosts; + + services = combineAttrs serviceList; +in { + options.keepalive-ping = { + enable = mkEnableOption "Enable keep alive ping task"; + hosts = mkOption { + type = types.listOf types.str; + default = []; + description = '' + Hosts to ping periodically + ''; + }; + delay = mkOption { + type = types.str; + default = "60"; + description = '' + Ping interval in seconds of periodic ping per host being pinged + ''; + }; + }; + + config = mkIf cfg.enable { + systemd.services = services; + }; +} \ No newline at end of file