From f031de68a400b90e1cc57a6d82e35f44bb0b60ff Mon Sep 17 00:00:00 2001 From: kompotkot Date: Tue, 11 Jan 2022 20:45:43 +0000 Subject: [PATCH] AddClient with AddClientNode united --- nodes/node_balancer/cmd/clients.go | 41 ++++++++++--------------- nodes/node_balancer/cmd/clients_test.go | 23 -------------- nodes/node_balancer/cmd/server.go | 4 +-- nodes/node_balancer/configs/settings.go | 2 +- 4 files changed, 19 insertions(+), 51 deletions(-) diff --git a/nodes/node_balancer/cmd/clients.go b/nodes/node_balancer/cmd/clients.go index 40974129..703e5fee 100644 --- a/nodes/node_balancer/cmd/clients.go +++ b/nodes/node_balancer/cmd/clients.go @@ -11,7 +11,9 @@ import ( var clientPool ClientPool -func (cpool *ClientPool) AddClient(ip string) { +func (cpool *ClientPool) AddClientNode(ip, blockchain string, node *Node) { + ts := time.Now().Unix() + var client *Client for _, c := range cpool.Clients { if c.IP == ip { @@ -26,31 +28,22 @@ func (cpool *ClientPool) AddClient(ip string) { } cpool.Clients = append(cpool.Clients, client) } -} -func (cpool *ClientPool) AddClientNode(ip, blockchain string, node *Node) { - ts := time.Now().Unix() - - for _, c := range cpool.Clients { - if c.IP == ip { - newNode := true - - for _, cn := range c.ClientNodes { - if reflect.DeepEqual(cn.Node, node) { - cn.LastCallTs = ts - newNode = false - } - } - - if newNode { - c.ClientNodes = append(c.ClientNodes, ClientNode{ - Blockchain: blockchain, - Node: node, - LastCallTs: ts, - }) - } + newNode := true + for _, cn := range client.ClientNodes { + if reflect.DeepEqual(cn.Node, node) { + cn.LastCallTs = ts + newNode = false } } + + if newNode { + client.ClientNodes = append(client.ClientNodes, ClientNode{ + Blockchain: blockchain, + Node: node, + LastCallTs: ts, + }) + } } func (cpool *ClientPool) GetClientNode(blockchain, ip string) *Node { @@ -62,7 +55,7 @@ func (cpool *ClientPool) GetClientNode(blockchain, ip string) *Node { if cn.Blockchain == blockchain { if ts-cn.LastCallTs < configs.NB_CLIENT_NODE_KEEP_ALIVE { cn.LastCallTs = ts - fmt.Println("Hot client node found, re-use it") + fmt.Printf("Hot client node found: %s, re-use it", cn.Node.GethURL) return cn.Node } else { fmt.Println("Client node outdated, remove it") diff --git a/nodes/node_balancer/cmd/clients_test.go b/nodes/node_balancer/cmd/clients_test.go index ae91a8c2..9d98064e 100644 --- a/nodes/node_balancer/cmd/clients_test.go +++ b/nodes/node_balancer/cmd/clients_test.go @@ -8,29 +8,6 @@ import ( configs "github.com/bugout-dev/moonstream/nodes/node_balancer/configs" ) -func TestAddClient(t *testing.T) { - var cases = []struct { - clients []Client - ip string - expected int - }{ - {[]Client{}, "localhost", 1}, - {[]Client{{IP: "localhost"}}, "192.168.1.2", 2}, - } - for _, c := range cases { - clientPool.Clients = []*Client{} - for _, client := range c.clients { - clientPool.Clients = append(clientPool.Clients, &client) - } - - clientPool.AddClient(c.ip) - if len(clientPool.Clients) != c.expected { - t.Log("Wrong number of clients") - t.Fatal() - } - } -} - func TestCleanInactiveClientNodes(t *testing.T) { ts := time.Now().Unix() diff --git a/nodes/node_balancer/cmd/server.go b/nodes/node_balancer/cmd/server.go index 063520d1..13e00717 100644 --- a/nodes/node_balancer/cmd/server.go +++ b/nodes/node_balancer/cmd/server.go @@ -29,7 +29,7 @@ func initHealthCheck(debug bool) { blockchainPool.HealthCheck() clientPool.CleanInactiveClientNodes() if debug { - blockchainPool.StatusLog() + // blockchainPool.StatusLog() clientPool.StatusLog() } } @@ -109,8 +109,6 @@ func InitServer() { return } - clientPool.AddClient("localhost") - // Configure Humbug reporter to handle errors var err error sessionID := uuid.New().String() diff --git a/nodes/node_balancer/configs/settings.go b/nodes/node_balancer/configs/settings.go index 62452890..a2c917f6 100644 --- a/nodes/node_balancer/configs/settings.go +++ b/nodes/node_balancer/configs/settings.go @@ -96,7 +96,7 @@ var NB_HEALTH_CHECK_INTERVAL = time.Second * 5 var NB_HEALTH_CHECK_CALL_TIMEOUT = time.Second * 2 // Client config -var NB_CLIENT_NODE_KEEP_ALIVE = int64(10) // Seconds +var NB_CLIENT_NODE_KEEP_ALIVE = int64(5) // Seconds // Humbug config var HUMBUG_REPORTER_NODE_BALANCER_TOKEN = os.Getenv("HUMBUG_REPORTER_NODE_BALANCER_TOKEN")