pull/786/head
kompotkot 2023-05-16 10:47:41 +00:00
rodzic 8e7da79fb1
commit 363388857b
6 zmienionych plików z 23 dodań i 21 usunięć

Wyświetl plik

@ -1,17 +1,17 @@
# Node Balancer application # Node Balancer application
# Installation ## Installation
- Prepare environment variables - Prepare environment variables, according with `sample.env`.
- Build application - Build application
```bash ```bash
go build -o nodebalancer . go build -o nodebalancer .
``` ```
# Work with nodebalancer ## Work with nodebalancer
## add-access ### add-access
Add new access for user: Add new access for user:
@ -25,7 +25,7 @@ nodebalancer add-access \
--blockchain--access true --blockchain--access true
``` ```
## delete-access ### delete-access
Delete user access: Delete user access:
@ -37,7 +37,7 @@ nodebalancer delete-access \
If `access-id` not specified, all user accesses will be deleted. If `access-id` not specified, all user accesses will be deleted.
## users ### users
```bash ```bash
nodebalancer users | jq . 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` `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 ```bash
nodebalancer server -host 0.0.0.0 -port 8544 -healthcheck 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 `--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. 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 Common request to fetch block number
```bash ```bash
curl --request GET 'http://127.0.0.1:8544/nb/ethereum/jsonrpc?access_id=<access_id>&data_source=<blockchain/database>' \ curl --request POST 'http://127.0.0.1:8544/nb/ethereum/jsonrpc?access_id=<access_id>&data_source=<blockchain/database>' \
--header 'Content-Type: application/json' \ --header 'Content-Type: application/json' \
--data-raw '{ --data-raw '{
"jsonrpc":"2.0", "jsonrpc":"2.0",
"method":"eth_getBlockByNumber", "method":"eth_getBlockByNumber",
"params":["0xb71b64", false], "params":["latest", false],
"id":1 "id":1
}' }'
``` ```

Wyświetl plik

@ -206,16 +206,18 @@ func (bpool *BlockchainPool) HealthCheck() {
if err != nil { if err != nil {
n.UpdateNodeState(0, alive) n.UpdateNodeState(0, alive)
log.Printf("Unable to reach node: %s", n.Endpoint.Host) log.Printf("Unable to reach node: %s", n.Endpoint.Host)
resp.Body.Close()
continue continue
} }
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body) body, err := ioutil.ReadAll(resp.Body)
if err != nil { if err != nil {
n.UpdateNodeState(0, alive) n.UpdateNodeState(0, alive)
log.Printf("Unable to parse response from %s node, err %v", n.Endpoint.Host, err) log.Printf("Unable to parse response from %s node, err %v", n.Endpoint.Host, err)
resp.Body.Close()
continue continue
} }
resp.Body.Close()
var statusResponse NodeStatusResponse var statusResponse NodeStatusResponse
err = json.Unmarshal(body, &statusResponse) err = json.Unmarshal(body, &statusResponse)

Wyświetl plik

@ -42,7 +42,7 @@ type ClientPool struct {
func CreateClientPools() { func CreateClientPools() {
clientPool = make(map[string]ClientPool) clientPool = make(map[string]ClientPool)
for b := range configBlockchains { for b := range supportedBlockchains {
clientPool[b] = ClientPool{} clientPool[b] = ClientPool{}
if cp, ok := clientPool[b]; ok { if cp, ok := clientPool[b]; ok {
cp.Client = make(map[string]*Client) cp.Client = make(map[string]*Client)
@ -54,7 +54,7 @@ func CreateClientPools() {
// Return client pool corresponding to provided blockchain // Return client pool corresponding to provided blockchain
func GetClientPool(blockchain string) *ClientPool { func GetClientPool(blockchain string) *ClientPool {
var cpool *ClientPool var cpool *ClientPool
for b := range configBlockchains { for b := range supportedBlockchains {
if b == blockchain { if b == blockchain {
c := clientPool[blockchain] c := clientPool[blockchain]
cpool = &c cpool = &c

Wyświetl plik

@ -17,6 +17,8 @@ import (
var ( var (
nodeConfigs []NodeConfig nodeConfigs []NodeConfig
supportedBlockchains map[string]bool
// Bugout and application configuration // Bugout and application configuration
BUGOUT_AUTH_URL = os.Getenv("BUGOUT_AUTH_URL") BUGOUT_AUTH_URL = os.Getenv("BUGOUT_AUTH_URL")
BUGOUT_AUTH_CALL_TIMEOUT = time.Second * 5 BUGOUT_AUTH_CALL_TIMEOUT = time.Second * 5
@ -26,7 +28,7 @@ var (
NB_CONNECTION_RETRIES = 2 NB_CONNECTION_RETRIES = 2
NB_CONNECTION_RETRIES_INTERVAL = time.Millisecond * 10 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_HEALTH_CHECK_CALL_TIMEOUT = time.Second * 2
NB_CACHE_CLEANING_INTERVAL = time.Second * 10 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) return nil, fmt.Errorf("Unable to find user home directory, %v", err)
} }
configDirPath = fmt.Sprintf("%s/.nodebalancer", homeDir) configDirPath = fmt.Sprintf("%s/.nodebalancer", homeDir)
configPath = fmt.Sprintf("%s/config.txt", configDirPath) configPath = fmt.Sprintf("%s/config.json", configDirPath)
} else { } else {
configPath = strings.TrimSuffix(providedPath, "/") configPath = strings.TrimSuffix(providedPath, "/")
configDirPath = filepath.Dir(configPath) configDirPath = filepath.Dir(configPath)

Wyświetl plik

@ -42,7 +42,7 @@ func lbHandler(w http.ResponseWriter, r *http.Request) {
} }
var blockchain string var blockchain string
for b := range configBlockchains { for b := range supportedBlockchains {
if strings.HasPrefix(r.URL.Path, fmt.Sprintf("/nb/%s/", b)) { if strings.HasPrefix(r.URL.Path, fmt.Sprintf("/nb/%s/", b)) {
blockchain = b blockchain = b
break break

Wyświetl plik

@ -22,8 +22,6 @@ import (
var ( var (
internalCrawlersAccess ClientResourceData internalCrawlersAccess ClientResourceData
configBlockchains map[string]bool
// Crash reporter // Crash reporter
reporter *humbug.HumbugReporter reporter *humbug.HumbugReporter
) )
@ -36,7 +34,7 @@ func initHealthCheck(debug bool) {
case <-t.C: case <-t.C:
blockchainPool.HealthCheck() blockchainPool.HealthCheck()
logStr := "Client pool healthcheck." logStr := "Client pool healthcheck."
for b := range configBlockchains { for b := range supportedBlockchains {
cp := clientPool[b] cp := clientPool[b]
clients := cp.CleanInactiveClientNodes() clients := cp.CleanInactiveClientNodes()
logStr += fmt.Sprintf(" Active %s clients: %d.", b, clients) logStr += fmt.Sprintf(" Active %s clients: %d.", b, clients)
@ -170,7 +168,7 @@ func Server() {
fmt.Println(err) fmt.Println(err)
os.Exit(1) os.Exit(1)
} }
configBlockchains = make(map[string]bool) supportedBlockchains = make(map[string]bool)
// Parse nodes and set list of proxies // Parse nodes and set list of proxies
for i, nodeConfig := range nodeConfigs { for i, nodeConfig := range nodeConfigs {
@ -181,7 +179,7 @@ func Server() {
} }
// Append to supported blockchain set // Append to supported blockchain set
configBlockchains[nodeConfig.Blockchain] = true supportedBlockchains[nodeConfig.Blockchain] = true
proxyToEndpoint := httputil.NewSingleHostReverseProxy(endpoint) proxyToEndpoint := httputil.NewSingleHostReverseProxy(endpoint)
// If required detailed timeout configuration, define node.GethReverseProxy.Transport = &http.Transport{} // If required detailed timeout configuration, define node.GethReverseProxy.Transport = &http.Transport{}