kopia lustrzana https://github.com/bugout-dev/moonstream
AddClient with AddClientNode united
rodzic
a0e50b3722
commit
f031de68a4
|
@ -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,16 +28,9 @@ 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 {
|
||||
for _, cn := range client.ClientNodes {
|
||||
if reflect.DeepEqual(cn.Node, node) {
|
||||
cn.LastCallTs = ts
|
||||
newNode = false
|
||||
|
@ -43,15 +38,13 @@ func (cpool *ClientPool) AddClientNode(ip, blockchain string, node *Node) {
|
|||
}
|
||||
|
||||
if newNode {
|
||||
c.ClientNodes = append(c.ClientNodes, ClientNode{
|
||||
client.ClientNodes = append(client.ClientNodes, ClientNode{
|
||||
Blockchain: blockchain,
|
||||
Node: node,
|
||||
LastCallTs: ts,
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (cpool *ClientPool) GetClientNode(blockchain, ip string) *Node {
|
||||
ts := time.Now().Unix()
|
||||
|
@ -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")
|
||||
|
|
|
@ -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()
|
||||
|
||||
|
|
|
@ -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()
|
||||
|
|
|
@ -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")
|
||||
|
|
Ładowanie…
Reference in New Issue