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 (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"log"
|
||||
"net"
|
||||
"net/http"
|
||||
"strings"
|
||||
|
||||
configs "github.com/bugout-dev/moonstream/nodes/node_balancer/configs"
|
||||
)
|
||||
|
||||
// pingRoute response with status of load balancer server itself
|
||||
|
@ -31,27 +34,29 @@ func lbHandler(w http.ResponseWriter, r *http.Request) {
|
|||
return
|
||||
}
|
||||
|
||||
// Chose one node
|
||||
ip, _, err := net.SplitHostPort(r.RemoteAddr)
|
||||
if err != nil {
|
||||
http.Error(w, "Unable to parse client IP", http.StatusInternalServerError)
|
||||
return
|
||||
clientId := w.Header().Get(configs.MOONSTREAM_CLIENT_ID_HEADER)
|
||||
if clientId == "" {
|
||||
log.Printf("Empty client id provided")
|
||||
// TODO(kompotkot): After all internal crawlers and services start
|
||||
// providing client id header, then replace to http.Error
|
||||
clientId = "none"
|
||||
}
|
||||
|
||||
// Chose one node
|
||||
var node *Node
|
||||
cpool, err := GetClientPool(blockchain)
|
||||
if err != nil {
|
||||
http.Error(w, fmt.Sprintf("Unacceptable blockchain provided %s", blockchain), http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
cpool.GetClientNode(ip)
|
||||
cpool.GetClientNode(clientId)
|
||||
if node == nil {
|
||||
node = blockchainPool.GetNextNode(blockchain)
|
||||
if node == nil {
|
||||
http.Error(w, "There are no nodes available", http.StatusServiceUnavailable)
|
||||
return
|
||||
}
|
||||
cpool.AddClientNode(ip, node)
|
||||
cpool.AddClientNode(clientId, node)
|
||||
}
|
||||
|
||||
// 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_IPC_PORT = os.Getenv("MOONSTREAM_NODE_POLYGON_IPC_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() {
|
||||
if MOONSTREAM_NODE_ETHEREUM_A_IPC_ADDR == "" {
|
||||
|
@ -51,6 +52,10 @@ func checkEnvVarSet() {
|
|||
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 == "" {
|
||||
log.Fatal("Some of environment variables not set")
|
||||
}
|
||||
|
|
Ładowanie…
Reference in New Issue