kopia lustrzana https://github.com/bugout-dev/moonstream
AddClient with AddClientNode united
rodzic
a0e50b3722
commit
f031de68a4
|
@ -11,7 +11,9 @@ import (
|
||||||
|
|
||||||
var clientPool ClientPool
|
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
|
var client *Client
|
||||||
for _, c := range cpool.Clients {
|
for _, c := range cpool.Clients {
|
||||||
if c.IP == ip {
|
if c.IP == ip {
|
||||||
|
@ -26,31 +28,22 @@ func (cpool *ClientPool) AddClient(ip string) {
|
||||||
}
|
}
|
||||||
cpool.Clients = append(cpool.Clients, client)
|
cpool.Clients = append(cpool.Clients, client)
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
func (cpool *ClientPool) AddClientNode(ip, blockchain string, node *Node) {
|
newNode := true
|
||||||
ts := time.Now().Unix()
|
for _, cn := range client.ClientNodes {
|
||||||
|
if reflect.DeepEqual(cn.Node, node) {
|
||||||
for _, c := range cpool.Clients {
|
cn.LastCallTs = ts
|
||||||
if c.IP == ip {
|
newNode = false
|
||||||
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,
|
|
||||||
})
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if newNode {
|
||||||
|
client.ClientNodes = append(client.ClientNodes, ClientNode{
|
||||||
|
Blockchain: blockchain,
|
||||||
|
Node: node,
|
||||||
|
LastCallTs: ts,
|
||||||
|
})
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (cpool *ClientPool) GetClientNode(blockchain, ip string) *Node {
|
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 cn.Blockchain == blockchain {
|
||||||
if ts-cn.LastCallTs < configs.NB_CLIENT_NODE_KEEP_ALIVE {
|
if ts-cn.LastCallTs < configs.NB_CLIENT_NODE_KEEP_ALIVE {
|
||||||
cn.LastCallTs = ts
|
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
|
return cn.Node
|
||||||
} else {
|
} else {
|
||||||
fmt.Println("Client node outdated, remove it")
|
fmt.Println("Client node outdated, remove it")
|
||||||
|
|
|
@ -8,29 +8,6 @@ import (
|
||||||
configs "github.com/bugout-dev/moonstream/nodes/node_balancer/configs"
|
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) {
|
func TestCleanInactiveClientNodes(t *testing.T) {
|
||||||
ts := time.Now().Unix()
|
ts := time.Now().Unix()
|
||||||
|
|
||||||
|
|
|
@ -29,7 +29,7 @@ func initHealthCheck(debug bool) {
|
||||||
blockchainPool.HealthCheck()
|
blockchainPool.HealthCheck()
|
||||||
clientPool.CleanInactiveClientNodes()
|
clientPool.CleanInactiveClientNodes()
|
||||||
if debug {
|
if debug {
|
||||||
blockchainPool.StatusLog()
|
// blockchainPool.StatusLog()
|
||||||
clientPool.StatusLog()
|
clientPool.StatusLog()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -109,8 +109,6 @@ func InitServer() {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
clientPool.AddClient("localhost")
|
|
||||||
|
|
||||||
// Configure Humbug reporter to handle errors
|
// Configure Humbug reporter to handle errors
|
||||||
var err error
|
var err error
|
||||||
sessionID := uuid.New().String()
|
sessionID := uuid.New().String()
|
||||||
|
|
|
@ -96,7 +96,7 @@ var NB_HEALTH_CHECK_INTERVAL = time.Second * 5
|
||||||
var NB_HEALTH_CHECK_CALL_TIMEOUT = time.Second * 2
|
var NB_HEALTH_CHECK_CALL_TIMEOUT = time.Second * 2
|
||||||
|
|
||||||
// Client config
|
// Client config
|
||||||
var NB_CLIENT_NODE_KEEP_ALIVE = int64(10) // Seconds
|
var NB_CLIENT_NODE_KEEP_ALIVE = int64(5) // Seconds
|
||||||
|
|
||||||
// Humbug config
|
// Humbug config
|
||||||
var HUMBUG_REPORTER_NODE_BALANCER_TOKEN = os.Getenv("HUMBUG_REPORTER_NODE_BALANCER_TOKEN")
|
var HUMBUG_REPORTER_NODE_BALANCER_TOKEN = os.Getenv("HUMBUG_REPORTER_NODE_BALANCER_TOKEN")
|
||||||
|
|
Ładowanie…
Reference in New Issue