From 25f319f96d42d6e141f4ae39e71215a6367d9a74 Mon Sep 17 00:00:00 2001
From: Jake Taylor <jake@elixxir.io>
Date: Mon, 12 Apr 2021 14:20:44 -0500
Subject: [PATCH] hostpool improvements

---
 api/client.go               |  2 +-
 cmd/root.go                 |  2 +-
 network/gateway/hostPool.go | 10 ++++++----
 network/gateway/sender.go   | 24 ++++++++++++++++++++++--
 4 files changed, 30 insertions(+), 8 deletions(-)

diff --git a/api/client.go b/api/client.go
index 1af9755c0..8ce14676d 100644
--- a/api/client.go
+++ b/api/client.go
@@ -446,7 +446,7 @@ func (c *Client) StopNetworkFollower(timeout time.Duration) error {
 	return nil
 }
 
-// Gets the state of the network follower. Returns:
+// NetworkFollowerStatus Gets the state of the network follower. Returns:
 // Stopped 	- 0
 // Starting - 1000
 // Running	- 2000
diff --git a/cmd/root.go b/cmd/root.go
index 4078f628d..96a6f11f7 100644
--- a/cmd/root.go
+++ b/cmd/root.go
@@ -78,7 +78,7 @@ var rootCmd = &cobra.Command{
 		jww.INFO.Printf("Message ListenerID: %v", listenerID)
 
 		// Set up auth request handler, which simply prints the
-		// user id of the requestor.
+		// user id of the requester.
 		authMgr := client.GetAuthRegistrar()
 		authMgr.AddGeneralRequestCallback(printChanRequest)
 
diff --git a/network/gateway/hostPool.go b/network/gateway/hostPool.go
index 08c12bb18..7d96d85e1 100644
--- a/network/gateway/hostPool.go
+++ b/network/gateway/hostPool.go
@@ -116,7 +116,8 @@ func (h *HostPool) getAny(length uint32, excluded []*id.ID) []*connect.Host {
 		length = h.poolParams.PoolSize
 	}
 	if excluded != nil {
-		for _, gwId := range excluded {
+		for i := range excluded {
+			gwId := excluded[i]
 			if idx, ok := h.hostMap[*gwId]; ok {
 				checked[idx] = nil
 			}
@@ -314,7 +315,7 @@ func (h *HostPool) updateConns() error {
 	for gwId := range h.ndfMap {
 		if _, ok := newMap[gwId]; !ok {
 			// If GwId in ndfMap is not in newMap, remove the Gateway
-			h.removeGateway(&gwId)
+			h.removeGateway(gwId.DeepCopy())
 		}
 	}
 
@@ -322,7 +323,7 @@ func (h *HostPool) updateConns() error {
 	for gwId, ndfIdx := range newMap {
 		if _, ok := h.ndfMap[gwId]; !ok {
 			// If GwId in newMap is not in ndfMap, add the Gateway
-			h.addGateway(&gwId, ndfIdx)
+			h.addGateway(gwId.DeepCopy(), ndfIdx)
 		}
 	}
 
@@ -339,7 +340,8 @@ func convertNdfToMap(ndf *ndf.NetworkDefinition) (map[id.ID]int, error) {
 	}
 
 	// Process gateway Id's into set
-	for i, gw := range ndf.Gateways {
+	for i := range ndf.Gateways {
+		gw := ndf.Gateways[i]
 		gwId, err := id.Unmarshal(gw.ID)
 		if err != nil {
 			return nil, err
diff --git a/network/gateway/sender.go b/network/gateway/sender.go
index afa459304..90294a398 100644
--- a/network/gateway/sender.go
+++ b/network/gateway/sender.go
@@ -10,6 +10,7 @@ package gateway
 
 import (
 	"github.com/pkg/errors"
+	jww "github.com/spf13/jwalterweatherman"
 	"gitlab.com/elixxir/client/storage"
 	"gitlab.com/elixxir/comms/network"
 	"gitlab.com/xx_network/comms/connect"
@@ -40,31 +41,44 @@ func (m *Sender) SendToSpecific(target *id.ID,
 	sendFunc func(host *connect.Host, target *id.ID) (interface{}, error)) (interface{}, error) {
 	host, ok := m.getSpecific(target)
 	if ok {
+		jww.WARN.Printf("SendToSpecific %s", host.GetId().String())
 		result, err := sendFunc(host, target)
 		if err == nil {
 			return result, m.forceAdd([]*id.ID{host.GetId()})
+		} else {
+			jww.WARN.Printf("Unable to SendToSpecific %s: %+v", host.GetId().String(), err)
 		}
 	}
 
 	proxies := m.getAny(m.poolParams.ProxyAttempts, []*id.ID{target})
 	for proxyIdx := 0; proxyIdx < len(proxies); proxyIdx++ {
+		jww.WARN.Printf("SendToSpecific proxy %s", proxies[proxyIdx].GetId().String())
 		result, err := sendFunc(proxies[proxyIdx], target)
 		if err == nil {
 			return result, nil
+		} else {
+			jww.WARN.Printf("Unable to SendToSpecific proxy %s: %+v", proxies[proxyIdx].GetId().String(), err)
 		}
 	}
 
-	return nil, errors.Errorf("Unable to send to any specifics with proxies")
+	return nil, errors.Errorf("Unable to send to specific with proxies")
 }
 
 // SendToAny Call given sendFunc to any Host in the HostPool, attempting with up to numProxies destinations
 func (m *Sender) SendToAny(sendFunc func(host *connect.Host) (interface{}, error)) (interface{}, error) {
 
 	proxies := m.getAny(m.poolParams.ProxyAttempts, nil)
-	for _, proxy := range proxies {
+	for i := range proxies {
+		jww.WARN.Printf("Sender %d %s", i, proxies[i].GetId().String())
+	}
+	for i := range proxies {
+		proxy := proxies[i]
+		jww.WARN.Printf("SendToAny %s", proxy.GetId().String())
 		result, err := sendFunc(proxy)
 		if err == nil {
 			return result, nil
+		} else {
+			jww.WARN.Printf("Unable to SendToAny %s: %+v", proxy.GetId().String(), err)
 		}
 	}
 
@@ -76,17 +90,23 @@ func (m *Sender) SendToPreferred(targets []*id.ID,
 	sendFunc func(host *connect.Host, target *id.ID) (interface{}, error)) (interface{}, error) {
 	targetHosts := m.getPreferred(targets)
 	for i, host := range targetHosts {
+		jww.WARN.Printf("SendToPreferred %s", host.GetId().String())
 		result, err := sendFunc(host, targets[i])
 		if err == nil {
 			return result, nil
+		} else {
+			jww.WARN.Printf("Unable to SendToPreferred %s: %+v", host.GetId().String(), err)
 		}
 	}
 
 	proxies := m.getAny(m.poolParams.ProxyAttempts, targets)
 	for i, proxy := range proxies {
+		jww.WARN.Printf("SendToPreferred proxy %s", proxy.GetId().String())
 		result, err := sendFunc(proxy, targets[i%len(targets)])
 		if err == nil {
 			return result, nil
+		} else {
+			jww.WARN.Printf("Unable to SendToPreferred proxy %s: %+v", proxy.GetId().String(), err)
 		}
 	}
 
-- 
GitLab