mirror of
https://github.com/gravitl/netmaker.git
synced 2025-10-05 08:47:35 +08:00
refactor to reduce number of goroutines
This commit is contained in:
@@ -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")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user