Fixed add client node

pull/519/head
kompotkot 2022-01-14 18:10:23 +00:00
rodzic dd91735887
commit 491a374437
2 zmienionych plików z 40 dodań i 21 usunięć

Wyświetl plik

@ -1,7 +1,6 @@
package cmd package cmd
import ( import (
// "fmt"
"log" "log"
"reflect" "reflect"
"time" "time"
@ -11,33 +10,31 @@ import (
var clientPool ClientPool var clientPool ClientPool
func (client *Client) GetClientNode() (clientNode *Node) { // func (client *Client) GetClientNode() (clientNode *Node) {
client.mux.RLock() // client.mux.RLock()
clientNode = client.Node // clientNode = client.Node
client.mux.RUnlock() // client.mux.RUnlock()
return clientNode // return clientNode
} // }
// Add client node and client itself if doesn't exist // Add client node and client itself if doesn't exist
// TODO(kompotkot): Add mutex as for balancer // TODO(kompotkot): Add mutex as for balancer
func (cpool *ClientPool) AddClientNode(id, blockchain string, node *Node) *Client { func (cpool *ClientPool) AddClientNode(id, blockchain string, node *Node) {
ts := time.Now().Unix() ts := time.Now().Unix()
currentClient := cpool.Client[id]
// Find clint with same ID and update timestamp or // Find clint with same ID and update timestamp or
// add new one if doesn't exist // add new one if doesn't exist
if currentClient != nil { if cpool.Client[id] != nil {
if reflect.DeepEqual(currentClient.Node, node) { if reflect.DeepEqual(cpool.Client[id].Node, node) {
currentClient.LastCallTs = ts cpool.Client[id].LastCallTs = ts
return currentClient return
} }
} }
cpool.Client[id] = &Client{
currentClient.Blockchain = blockchain Blockchain: blockchain,
currentClient.Node = node Node: node,
currentClient.LastCallTs = ts LastCallTs: ts,
return currentClient }
} }
// Get client hot node if exists // Get client hot node if exists

Wyświetl plik

@ -8,6 +8,27 @@ import (
configs "github.com/bugout-dev/moonstream/nodes/node_balancer/configs" configs "github.com/bugout-dev/moonstream/nodes/node_balancer/configs"
) )
func TestAddClientNode(t *testing.T) {
var cases = []struct {
clients map[string]*Client
expected string
}{
{map[string]*Client{"1": {Blockchain: "ethereum", Node: &Node{Alive: true}}}, "1"},
}
for _, c := range cases {
clientPool.Client = make(map[string]*Client)
for id, client := range c.clients {
clientPool.AddClientNode(id, client.Blockchain, client.Node)
}
for id := range clientPool.Client {
if id != c.expected {
t.Log("Wrong client was added")
t.Fatal()
}
}
}
}
func TestGetClientNode(t *testing.T) { func TestGetClientNode(t *testing.T) {
ts := time.Now().Unix() ts := time.Now().Unix()
@ -16,6 +37,7 @@ func TestGetClientNode(t *testing.T) {
id string id string
expected *Node expected *Node
}{ }{
{map[string]*Client{}, "1", nil},
{map[string]*Client{"1": {Blockchain: "ethereum", LastCallTs: ts, Node: &Node{Alive: true}}}, "1", &Node{Alive: true}}, {map[string]*Client{"1": {Blockchain: "ethereum", LastCallTs: ts, Node: &Node{Alive: true}}}, "1", &Node{Alive: true}},
{map[string]*Client{"2": {Blockchain: "polygon", LastCallTs: ts, Node: &Node{Alive: true}}}, "1", nil}, {map[string]*Client{"2": {Blockchain: "polygon", LastCallTs: ts, Node: &Node{Alive: true}}}, "1", nil},
{map[string]*Client{"1": {Blockchain: "ethereum", LastCallTs: ts - configs.NB_CLIENT_NODE_KEEP_ALIVE, Node: &Node{Alive: true}}}, "1", nil}, {map[string]*Client{"1": {Blockchain: "ethereum", LastCallTs: ts - configs.NB_CLIENT_NODE_KEEP_ALIVE, Node: &Node{Alive: true}}}, "1", nil},
@ -56,8 +78,8 @@ func TestCleanInactiveClientNodes(t *testing.T) {
} }
clientPool.CleanInactiveClientNodes() clientPool.CleanInactiveClientNodes()
for key := range clientPool.Client { for id := range clientPool.Client {
if key != c.expected { if id != c.expected {
t.Log("Wrong client was removed") t.Log("Wrong client was removed")
t.Fatal() t.Fatal()
} }