From add4e8dbcf4826fd5f5b62bee9f1b7b21b45eea7 Mon Sep 17 00:00:00 2001 From: Ruakij Date: Thu, 30 Mar 2023 12:39:38 +0200 Subject: [PATCH] Add function to run periodic checks --- cmd/app/main.go | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/cmd/app/main.go b/cmd/app/main.go index f74bed9..78798b0 100644 --- a/cmd/app/main.go +++ b/cmd/app/main.go @@ -4,6 +4,7 @@ import ( "net" "os" "strconv" + "time" envChecks "git.ruekov.eu/ruakij/routingtabletowg/lib/environmentchecks" ip2Map "git.ruekov.eu/ruakij/routingtabletowg/lib/iproute2mapping" @@ -104,9 +105,29 @@ func main() { logger.Info.Printf("Initially setting all current routes") syncCurrentRoutesToHandler(routeSubChan, routeList) + if(periodicSync > 0){ + go runPeriodicSync(periodicSync, link, routeSubChan) + } + select {} } +func runPeriodicSync(seconds int, link netlink.Link, routeSubChan chan netlink.RouteUpdate){ + interval := time.Duration(seconds) * time.Second + for { + time.Sleep(interval) + + // Get routing-table entries from device + routeList, err := netlink.RouteList(link, netlink.FAMILY_ALL) + if err != nil { + logger.Error.Fatalf("Couldn't get route-entries: %s", err) + } + + logger.Info.Printf("Periodically syncing all routes") + syncCurrentRoutesToHandler(routeSubChan, routeList) + } +} + func syncCurrentRoutesToHandler(routeSubChan chan netlink.RouteUpdate, routeList []netlink.Route){ for _, route := range routeList {