AddClient with AddClientNode united

pull/519/head
kompotkot 2022-01-11 20:45:43 +00:00
rodzic a0e50b3722
commit f031de68a4
4 zmienionych plików z 19 dodań i 51 usunięć

Wyświetl plik

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

Wyświetl plik

@ -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()

Wyświetl plik

@ -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()

Wyświetl plik

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