From 339d588f99048b5738a1e90ca8913a545648f360 Mon Sep 17 00:00:00 2001 From: mykola2312 <49044616+mykola2312@users.noreply.github.com> Date: Sun, 26 Jan 2025 02:28:37 +0200 Subject: [PATCH] implement node multicast timer --- main.go | 28 +++++++++++++++++++++++++++- 1 file changed, 27 insertions(+), 1 deletion(-) diff --git a/main.go b/main.go index af01935..bf8c140 100644 --- a/main.go +++ b/main.go @@ -13,6 +13,7 @@ import ( "strconv" "strings" "syscall" + "time" "github.com/op/go-logging" "github.com/sevlyar/go-daemon" @@ -45,6 +46,8 @@ type NodeConfig struct { Address string `xml:"address"` } `xml:"neighbor"` + Sync int `xml:"sync"` + RPCEndpoints []string `xml:"rpc"` Log LogConfig `xml:"log"` @@ -272,7 +275,29 @@ func nodeMain() { // start node node.Start() - // TODO: daemonize + // node state sync scheduled task + stopChan := make(chan struct{}) + + if config.Sync != 0 { + ticker := time.NewTicker(time.Duration(config.Sync) * time.Second) + + go func() { + for { + select { + case <-ticker.C: + err := node.MulticastSync() + if err != nil { + log.Errorf("MulticastSync err: %v", err) + } + case <-stopChan: + ticker.Stop() + return + } + } + }() + } else { + log.Info("sync interval is not set. Node will not sync with neighbors") + } // register go channel to receive unix signals, // while hogging main thread to read them and take action. @@ -293,6 +318,7 @@ signaling: } // stop daemon + close(stopChan) node.Stop() }