refactor to reduce number of goroutines

This commit is contained in:
Matthew R. Kasun
2022-02-18 14:56:26 -05:00
parent 52ac4c3bc3
commit c7cf5fb2fb
3 changed files with 106 additions and 103 deletions

View File

@@ -14,7 +14,7 @@ import (
// Checkin -- go routine that checks for public or local ip changes, publishes changes
// if there are no updates, simply "pings" the server as a checkin
func Checkin(ctx context.Context, wg *sync.WaitGroup, cfg *config.ClientConfig, network string) {
func Checkin(ctx context.Context, wg sync.WaitGroup) {
defer wg.Done()
for {
select {
@@ -25,48 +25,59 @@ func Checkin(ctx context.Context, wg *sync.WaitGroup, cfg *config.ClientConfig,
case <-time.After(time.Second * 60):
// ncutils.Log("Checkin running")
//read latest config
cfg.ReadConfig()
if cfg.Node.IsStatic != "yes" {
extIP, err := ncutils.GetPublicIP()
if err != nil {
ncutils.PrintLog("error encountered checking public ip addresses: "+err.Error(), 1)
}
if cfg.Node.Endpoint != extIP && extIP != "" {
ncutils.PrintLog("endpoint has changed from "+cfg.Node.Endpoint+" to "+extIP, 1)
cfg.Node.Endpoint = extIP
if err := PublishNodeUpdate(cfg); err != nil {
ncutils.Log("could not publish endpoint change")
}
}
intIP, err := getPrivateAddr()
if err != nil {
ncutils.PrintLog("error encountered checking private ip addresses: "+err.Error(), 1)
}
if cfg.Node.LocalAddress != intIP && intIP != "" {
ncutils.PrintLog("local Address has changed from "+cfg.Node.LocalAddress+" to "+intIP, 1)
cfg.Node.LocalAddress = intIP
if err := PublishNodeUpdate(cfg); err != nil {
ncutils.Log("could not publish local address change")
}
}
} else if cfg.Node.IsLocal == "yes" && cfg.Node.LocalRange != "" {
localIP, err := ncutils.GetLocalIP(cfg.Node.LocalRange)
if err != nil {
ncutils.PrintLog("error encountered checking local ip addresses: "+err.Error(), 1)
}
if cfg.Node.Endpoint != localIP && localIP != "" {
ncutils.PrintLog("endpoint has changed from "+cfg.Node.Endpoint+" to "+localIP, 1)
cfg.Node.Endpoint = localIP
if err := PublishNodeUpdate(cfg); err != nil {
ncutils.Log("could not publish localip change")
}
}
networks, err := ncutils.GetSystemNetworks()
if err != nil {
return
}
if err := PingServer(cfg); err != nil {
ncutils.PrintLog("could not ping server "+err.Error(), 0)
for _, network := range networks {
if network == ncutils.COMMS_NETWORK_NAME {
continue
}
var cfg *config.ClientConfig
cfg.Network = network
cfg.ReadConfig()
if cfg.Node.IsStatic != "yes" {
extIP, err := ncutils.GetPublicIP()
if err != nil {
ncutils.PrintLog("error encountered checking public ip addresses: "+err.Error(), 1)
}
if cfg.Node.Endpoint != extIP && extIP != "" {
ncutils.PrintLog("endpoint has changed from "+cfg.Node.Endpoint+" to "+extIP, 1)
cfg.Node.Endpoint = extIP
if err := PublishNodeUpdate(cfg); err != nil {
ncutils.Log("could not publish endpoint change")
}
}
intIP, err := getPrivateAddr()
if err != nil {
ncutils.PrintLog("error encountered checking private ip addresses: "+err.Error(), 1)
}
if cfg.Node.LocalAddress != intIP && intIP != "" {
ncutils.PrintLog("local Address has changed from "+cfg.Node.LocalAddress+" to "+intIP, 1)
cfg.Node.LocalAddress = intIP
if err := PublishNodeUpdate(cfg); err != nil {
ncutils.Log("could not publish local address change")
}
}
} else if cfg.Node.IsLocal == "yes" && cfg.Node.LocalRange != "" {
localIP, err := ncutils.GetLocalIP(cfg.Node.LocalRange)
if err != nil {
ncutils.PrintLog("error encountered checking local ip addresses: "+err.Error(), 1)
}
if cfg.Node.Endpoint != localIP && localIP != "" {
ncutils.PrintLog("endpoint has changed from "+cfg.Node.Endpoint+" to "+localIP, 1)
cfg.Node.Endpoint = localIP
if err := PublishNodeUpdate(cfg); err != nil {
ncutils.Log("could not publish localip change")
}
}
}
if err := PingServer(cfg); err != nil {
ncutils.PrintLog("could not ping server "+err.Error(), 0)
}
Hello(cfg, network)
// ncutils.Log("Checkin complete")
}
Hello(cfg, network)
// ncutils.Log("Checkin complete")
}
}
}