kopia lustrzana https://github.com/bugout-dev/moonstream
Work with client id header instead of ip
rodzic
c2f95e9217
commit
723c9db6be
|
@ -6,9 +6,12 @@ package cmd
|
||||||
import (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"log"
|
||||||
"net"
|
"net"
|
||||||
"net/http"
|
"net/http"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
|
configs "github.com/bugout-dev/moonstream/nodes/node_balancer/configs"
|
||||||
)
|
)
|
||||||
|
|
||||||
// pingRoute response with status of load balancer server itself
|
// pingRoute response with status of load balancer server itself
|
||||||
|
@ -31,27 +34,29 @@ func lbHandler(w http.ResponseWriter, r *http.Request) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// Chose one node
|
clientId := w.Header().Get(configs.MOONSTREAM_CLIENT_ID_HEADER)
|
||||||
ip, _, err := net.SplitHostPort(r.RemoteAddr)
|
if clientId == "" {
|
||||||
if err != nil {
|
log.Printf("Empty client id provided")
|
||||||
http.Error(w, "Unable to parse client IP", http.StatusInternalServerError)
|
// TODO(kompotkot): After all internal crawlers and services start
|
||||||
return
|
// providing client id header, then replace to http.Error
|
||||||
|
clientId = "none"
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Chose one node
|
||||||
var node *Node
|
var node *Node
|
||||||
cpool, err := GetClientPool(blockchain)
|
cpool, err := GetClientPool(blockchain)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
http.Error(w, fmt.Sprintf("Unacceptable blockchain provided %s", blockchain), http.StatusBadRequest)
|
http.Error(w, fmt.Sprintf("Unacceptable blockchain provided %s", blockchain), http.StatusBadRequest)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
cpool.GetClientNode(ip)
|
cpool.GetClientNode(clientId)
|
||||||
if node == nil {
|
if node == nil {
|
||||||
node = blockchainPool.GetNextNode(blockchain)
|
node = blockchainPool.GetNextNode(blockchain)
|
||||||
if node == nil {
|
if node == nil {
|
||||||
http.Error(w, "There are no nodes available", http.StatusServiceUnavailable)
|
http.Error(w, "There are no nodes available", http.StatusServiceUnavailable)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
cpool.AddClientNode(ip, node)
|
cpool.AddClientNode(clientId, node)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Save origin path, to use in proxyErrorHandler if node will not response
|
// Save origin path, to use in proxyErrorHandler if node will not response
|
||||||
|
|
|
@ -35,6 +35,7 @@ var MOONSTREAM_NODE_POLYGON_A_IPC_ADDR = os.Getenv("MOONSTREAM_NODE_POLYGON_A_IP
|
||||||
var MOONSTREAM_NODE_POLYGON_B_IPC_ADDR = os.Getenv("MOONSTREAM_NODE_POLYGON_B_IPC_ADDR")
|
var MOONSTREAM_NODE_POLYGON_B_IPC_ADDR = os.Getenv("MOONSTREAM_NODE_POLYGON_B_IPC_ADDR")
|
||||||
var MOONSTREAM_NODE_POLYGON_IPC_PORT = os.Getenv("MOONSTREAM_NODE_POLYGON_IPC_PORT")
|
var MOONSTREAM_NODE_POLYGON_IPC_PORT = os.Getenv("MOONSTREAM_NODE_POLYGON_IPC_PORT")
|
||||||
var MOONSTREAM_NODES_SERVER_PORT = os.Getenv("MOONSTREAM_NODES_SERVER_PORT")
|
var MOONSTREAM_NODES_SERVER_PORT = os.Getenv("MOONSTREAM_NODES_SERVER_PORT")
|
||||||
|
var MOONSTREAM_CLIENT_ID_HEADER = os.Getenv("MOONSTREAM_CLIENT_ID_HEADER")
|
||||||
|
|
||||||
func checkEnvVarSet() {
|
func checkEnvVarSet() {
|
||||||
if MOONSTREAM_NODE_ETHEREUM_A_IPC_ADDR == "" {
|
if MOONSTREAM_NODE_ETHEREUM_A_IPC_ADDR == "" {
|
||||||
|
@ -51,6 +52,10 @@ func checkEnvVarSet() {
|
||||||
MOONSTREAM_NODE_POLYGON_B_IPC_ADDR = "b.polygon.moonstream.internal"
|
MOONSTREAM_NODE_POLYGON_B_IPC_ADDR = "b.polygon.moonstream.internal"
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if MOONSTREAM_CLIENT_ID_HEADER == "" {
|
||||||
|
MOONSTREAM_CLIENT_ID_HEADER = "x-moonstream-client-id"
|
||||||
|
}
|
||||||
|
|
||||||
if MOONSTREAM_NODES_SERVER_PORT == "" || MOONSTREAM_NODE_ETHEREUM_IPC_PORT == "" || MOONSTREAM_NODE_POLYGON_IPC_PORT == "" {
|
if MOONSTREAM_NODES_SERVER_PORT == "" || MOONSTREAM_NODE_ETHEREUM_IPC_PORT == "" || MOONSTREAM_NODE_POLYGON_IPC_PORT == "" {
|
||||||
log.Fatal("Some of environment variables not set")
|
log.Fatal("Some of environment variables not set")
|
||||||
}
|
}
|
||||||
|
|
Ładowanie…
Reference in New Issue