diff --git a/nodes/node_balancer/README.md b/nodes/node_balancer/README.md index 62fd4de8..349cda04 100644 --- a/nodes/node_balancer/README.md +++ b/nodes/node_balancer/README.md @@ -1,17 +1,17 @@ # Node Balancer application -# Installation +## Installation -- Prepare environment variables +- Prepare environment variables, according with `sample.env`. - Build application ```bash go build -o nodebalancer . ``` -# Work with nodebalancer +## Work with nodebalancer -## add-access +### add-access Add new access for user: @@ -25,7 +25,7 @@ nodebalancer add-access \ --blockchain--access true ``` -## delete-access +### delete-access Delete user access: @@ -37,7 +37,7 @@ nodebalancer delete-access \ If `access-id` not specified, all user accesses will be deleted. -## users +### users ```bash nodebalancer users | jq . @@ -67,7 +67,7 @@ This command will return a list of bugout resources of registered users to acces `extended_methods` - boolean which allow you to call not whitelisted method to blockchain node, by default for new user this is equal to `false` -## server +### server ```bash nodebalancer server -host 0.0.0.0 -port 8544 -healthcheck @@ -76,17 +76,17 @@ nodebalancer server -host 0.0.0.0 -port 8544 -healthcheck Flag `--healthcheck` will execute background process to ping-pong available nodes to keep their status and current block number. Flag `--debug` will extend output of each request to server and healthchecks summary. -# Work with node +## Work with node Common request to fetch block number ```bash -curl --request GET 'http://127.0.0.1:8544/nb/ethereum/jsonrpc?access_id=&data_source=' \ +curl --request POST 'http://127.0.0.1:8544/nb/ethereum/jsonrpc?access_id=&data_source=' \ --header 'Content-Type: application/json' \ --data-raw '{ "jsonrpc":"2.0", "method":"eth_getBlockByNumber", - "params":["0xb71b64", false], + "params":["latest", false], "id":1 }' ``` diff --git a/nodes/node_balancer/cmd/nodebalancer/balancer.go b/nodes/node_balancer/cmd/nodebalancer/balancer.go index a79f838c..00e4b42e 100644 --- a/nodes/node_balancer/cmd/nodebalancer/balancer.go +++ b/nodes/node_balancer/cmd/nodebalancer/balancer.go @@ -206,16 +206,18 @@ func (bpool *BlockchainPool) HealthCheck() { if err != nil { n.UpdateNodeState(0, alive) log.Printf("Unable to reach node: %s", n.Endpoint.Host) + resp.Body.Close() continue } - defer resp.Body.Close() body, err := ioutil.ReadAll(resp.Body) if err != nil { n.UpdateNodeState(0, alive) log.Printf("Unable to parse response from %s node, err %v", n.Endpoint.Host, err) + resp.Body.Close() continue } + resp.Body.Close() var statusResponse NodeStatusResponse err = json.Unmarshal(body, &statusResponse) diff --git a/nodes/node_balancer/cmd/nodebalancer/clients.go b/nodes/node_balancer/cmd/nodebalancer/clients.go index f86c38f8..492d2e20 100644 --- a/nodes/node_balancer/cmd/nodebalancer/clients.go +++ b/nodes/node_balancer/cmd/nodebalancer/clients.go @@ -42,7 +42,7 @@ type ClientPool struct { func CreateClientPools() { clientPool = make(map[string]ClientPool) - for b := range configBlockchains { + for b := range supportedBlockchains { clientPool[b] = ClientPool{} if cp, ok := clientPool[b]; ok { cp.Client = make(map[string]*Client) @@ -54,7 +54,7 @@ func CreateClientPools() { // Return client pool corresponding to provided blockchain func GetClientPool(blockchain string) *ClientPool { var cpool *ClientPool - for b := range configBlockchains { + for b := range supportedBlockchains { if b == blockchain { c := clientPool[blockchain] cpool = &c diff --git a/nodes/node_balancer/cmd/nodebalancer/configs.go b/nodes/node_balancer/cmd/nodebalancer/configs.go index 358eb598..62b31bd8 100644 --- a/nodes/node_balancer/cmd/nodebalancer/configs.go +++ b/nodes/node_balancer/cmd/nodebalancer/configs.go @@ -17,6 +17,8 @@ import ( var ( nodeConfigs []NodeConfig + supportedBlockchains map[string]bool + // Bugout and application configuration BUGOUT_AUTH_URL = os.Getenv("BUGOUT_AUTH_URL") BUGOUT_AUTH_CALL_TIMEOUT = time.Second * 5 @@ -26,7 +28,7 @@ var ( NB_CONNECTION_RETRIES = 2 NB_CONNECTION_RETRIES_INTERVAL = time.Millisecond * 10 - NB_HEALTH_CHECK_INTERVAL = time.Second * 5 + NB_HEALTH_CHECK_INTERVAL = time.Millisecond * 5000 NB_HEALTH_CHECK_CALL_TIMEOUT = time.Second * 2 NB_CACHE_CLEANING_INTERVAL = time.Second * 10 @@ -108,7 +110,7 @@ func GetConfigPath(providedPath string) (*ConfigPlacement, error) { return nil, fmt.Errorf("Unable to find user home directory, %v", err) } configDirPath = fmt.Sprintf("%s/.nodebalancer", homeDir) - configPath = fmt.Sprintf("%s/config.txt", configDirPath) + configPath = fmt.Sprintf("%s/config.json", configDirPath) } else { configPath = strings.TrimSuffix(providedPath, "/") configDirPath = filepath.Dir(configPath) diff --git a/nodes/node_balancer/cmd/nodebalancer/routes.go b/nodes/node_balancer/cmd/nodebalancer/routes.go index b9929369..08aeec19 100644 --- a/nodes/node_balancer/cmd/nodebalancer/routes.go +++ b/nodes/node_balancer/cmd/nodebalancer/routes.go @@ -42,7 +42,7 @@ func lbHandler(w http.ResponseWriter, r *http.Request) { } var blockchain string - for b := range configBlockchains { + for b := range supportedBlockchains { if strings.HasPrefix(r.URL.Path, fmt.Sprintf("/nb/%s/", b)) { blockchain = b break diff --git a/nodes/node_balancer/cmd/nodebalancer/server.go b/nodes/node_balancer/cmd/nodebalancer/server.go index f7b47c41..bffb1502 100644 --- a/nodes/node_balancer/cmd/nodebalancer/server.go +++ b/nodes/node_balancer/cmd/nodebalancer/server.go @@ -22,8 +22,6 @@ import ( var ( internalCrawlersAccess ClientResourceData - configBlockchains map[string]bool - // Crash reporter reporter *humbug.HumbugReporter ) @@ -36,7 +34,7 @@ func initHealthCheck(debug bool) { case <-t.C: blockchainPool.HealthCheck() logStr := "Client pool healthcheck." - for b := range configBlockchains { + for b := range supportedBlockchains { cp := clientPool[b] clients := cp.CleanInactiveClientNodes() logStr += fmt.Sprintf(" Active %s clients: %d.", b, clients) @@ -170,7 +168,7 @@ func Server() { fmt.Println(err) os.Exit(1) } - configBlockchains = make(map[string]bool) + supportedBlockchains = make(map[string]bool) // Parse nodes and set list of proxies for i, nodeConfig := range nodeConfigs { @@ -181,7 +179,7 @@ func Server() { } // Append to supported blockchain set - configBlockchains[nodeConfig.Blockchain] = true + supportedBlockchains[nodeConfig.Blockchain] = true proxyToEndpoint := httputil.NewSingleHostReverseProxy(endpoint) // If required detailed timeout configuration, define node.GethReverseProxy.Transport = &http.Transport{}