implement node multicast timer

This commit is contained in:
mykola2312 2025-01-26 02:28:37 +02:00
parent f75fa492a6
commit 339d588f99

28
main.go
View file

@ -13,6 +13,7 @@ import (
"strconv" "strconv"
"strings" "strings"
"syscall" "syscall"
"time"
"github.com/op/go-logging" "github.com/op/go-logging"
"github.com/sevlyar/go-daemon" "github.com/sevlyar/go-daemon"
@ -45,6 +46,8 @@ type NodeConfig struct {
Address string `xml:"address"` Address string `xml:"address"`
} `xml:"neighbor"` } `xml:"neighbor"`
Sync int `xml:"sync"`
RPCEndpoints []string `xml:"rpc"` RPCEndpoints []string `xml:"rpc"`
Log LogConfig `xml:"log"` Log LogConfig `xml:"log"`
@ -272,7 +275,29 @@ func nodeMain() {
// start node // start node
node.Start() 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, // register go channel to receive unix signals,
// while hogging main thread to read them and take action. // while hogging main thread to read them and take action.
@ -293,6 +318,7 @@ signaling:
} }
// stop daemon // stop daemon
close(stopChan)
node.Stop() node.Stop()
} }