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

@@ -35,50 +35,27 @@ type cachedMessage struct {
// Daemon runs netclient daemon from command line
func Daemon() error {
networks, err := ncutils.GetSystemNetworks()
if err != nil {
return err
}
client := setupMQTT(false)
defer client.Disconnect(250)
wg := sync.WaitGroup{}
ctx, cancel := context.WithCancel(context.Background())
go func(context.Context) {
for _, network := range networks {
//skip comms network
if network == ncutils.COMMS_NETWORK_NAME {
continue
}
MessageQueue(ctx, network)
}
}(ctx)
networks, _ := ncutils.GetSystemNetworks()
for _, network := range networks {
var cfg config.ClientConfig
cfg.Network = network
cfg.ReadConfig()
initialPull(cfg.Network)
}
wg.Add(1)
go Checkin(ctx, wg)
quit := make(chan os.Signal, 1)
signal.Notify(quit, syscall.SIGTERM, os.Interrupt)
<-quit
cancel()
ncutils.Log("all done")
return nil
}
// MessageQueue sets up Message Queue and subsribes/publishes updates to/from server
func MessageQueue(ctx context.Context, network string) {
ncutils.Log("netclient go routine started for " + network)
var cfg config.ClientConfig
cfg.Network = network
initialPull(cfg.Network)
cfg.ReadConfig()
ncutils.Log("daemon started for network: " + network)
client := setupMQTT(false)
defer client.Disconnect(250)
wg := &sync.WaitGroup{}
wg.Add(2)
checkinctx, checkincancel := context.WithCancel(context.Background())
go Checkin(checkinctx, wg, &cfg, network)
<-ctx.Done()
checkincancel()
ncutils.Log("shutting down message queue for network " + network)
ncutils.Log("shutting down message queue ")
wg.Wait()
ncutils.Log("shutdown complete")
return nil
}
// UpdateKeys -- updates private key and returns new publickey
@@ -142,31 +119,11 @@ func setupMQTT(publish bool) mqtt.Client {
opts.SetWriteTimeout(time.Minute)
opts.SetOnConnectHandler(func(client mqtt.Client) {
if !publish {
if cfg.DebugOn {
if token := client.Subscribe("#", 0, nil); token.Wait() && token.Error() != nil {
ncutils.Log(token.Error().Error())
return
}
ncutils.Log("subscribed to all topics for debugging purposes")
}
if token := client.Subscribe(fmt.Sprintf("update/%s/%s", cfg.Node.Network, cfg.Node.ID), 0, mqtt.MessageHandler(NodeUpdate)); token.Wait() && token.Error() != nil {
ncutils.Log(token.Error().Error())
return
}
if cfg.DebugOn {
ncutils.Log(fmt.Sprintf("subscribed to node updates for node %s update/%s/%s", cfg.Node.Name, cfg.Node.Network, cfg.Node.ID))
}
if token := client.Subscribe(fmt.Sprintf("peers/%s/%s", cfg.Node.Network, cfg.Node.ID), 0, mqtt.MessageHandler(UpdatePeers)); token.Wait() && token.Error() != nil {
ncutils.Log(token.Error().Error())
return
}
if cfg.DebugOn {
ncutils.Log(fmt.Sprintf("subscribed to peer updates for node %s peers/%s/%s", cfg.Node.Name, cfg.Node.Network, cfg.Node.ID))
}
opts.SetOrderMatters(true)
opts.SetResumeSubs(true)
SetSubscriptions(client, cfg)
}
})
opts.SetOrderMatters(true)
opts.SetResumeSubs(true)
opts.SetConnectionLostHandler(func(c mqtt.Client, e error) {
ncutils.Log("detected broker connection lost, running pull for " + cfg.Node.Network)
_, err := Pull(cfg.Node.Network, true)
@@ -215,6 +172,41 @@ func setupMQTT(publish bool) mqtt.Client {
return client
}
// SetSubscriptions - sets MQ subscriptions
func SetSubscriptions(client mqtt.Client, cfg *config.ClientConfig) {
if cfg.DebugOn {
if token := client.Subscribe("#", 0, nil); token.Wait() && token.Error() != nil {
ncutils.Log(token.Error().Error())
return
}
ncutils.Log("subscribed to all topics for debugging purposes")
}
networks, err := ncutils.GetSystemNetworks()
if err != nil {
ncutils.Log("error retriving networks " + err.Error())
}
for _, network := range networks {
var cfg config.ClientConfig
cfg.Network = network
cfg.ReadConfig()
if token := client.Subscribe(fmt.Sprintf("update/%s/%s", cfg.Node.Network, cfg.Node.ID), 0, mqtt.MessageHandler(NodeUpdate)); token.Wait() && token.Error() != nil {
ncutils.Log(token.Error().Error())
return
}
if cfg.DebugOn {
ncutils.Log(fmt.Sprintf("subscribed to node updates for node %s update/%s/%s", cfg.Node.Name, cfg.Node.Network, cfg.Node.ID))
}
if token := client.Subscribe(fmt.Sprintf("peers/%s/%s", cfg.Node.Network, cfg.Node.ID), 0, mqtt.MessageHandler(UpdatePeers)); token.Wait() && token.Error() != nil {
ncutils.Log(token.Error().Error())
return
}
if cfg.DebugOn {
ncutils.Log(fmt.Sprintf("subscribed to peer updates for node %s peers/%s/%s", cfg.Node.Name, cfg.Node.Network, cfg.Node.ID))
}
}
}
// publishes a message to server to update peers on this peer's behalf
func publishSignal(cfg *config.ClientConfig, signal byte) error {
if err := publish(cfg, fmt.Sprintf("signal/%s", cfg.Node.ID), []byte{signal}, 1); err != nil {

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")
}
}
}

View File

@@ -7,6 +7,6 @@ const (
DONE = 2
// KEY - key update completed signal for MQ
KEY = 3
// COMMS_NETWORK_NAME - name of signalling network
COMMS_NETWORK_NAME = "n37m8k3r"
)
const COMMS_NETWORK_NAME = "n37m8k3r"