mirror of
https://github.com/eosswedenorg/antelope-api-healthcheck
synced 2026-07-04 12:03:43 +02:00
src/server.go: refactor eosio v1/v2 api code into it's own module that implements a common interface
This commit is contained in:
parent
fa9a943215
commit
ddc7406a4c
6 changed files with 173 additions and 93 deletions
2
go.mod
2
go.mod
|
|
@ -12,6 +12,8 @@ require (
|
||||||
github.com/mattn/go-colorable v0.1.12 // indirect
|
github.com/mattn/go-colorable v0.1.12 // indirect
|
||||||
github.com/pborman/getopt/v2 v2.1.0
|
github.com/pborman/getopt/v2 v2.1.0
|
||||||
internal/utils v1.0.0
|
internal/utils v1.0.0
|
||||||
|
internal/api v1.0.0
|
||||||
)
|
)
|
||||||
|
|
||||||
replace internal/utils => ./src/utils
|
replace internal/utils => ./src/utils
|
||||||
|
replace internal/api => ./src/api
|
||||||
|
|
|
||||||
3
src/api/go.mod
Normal file
3
src/api/go.mod
Normal file
|
|
@ -0,0 +1,3 @@
|
||||||
|
module internal/api
|
||||||
|
|
||||||
|
go 1.14
|
||||||
11
src/api/interface.go
Normal file
11
src/api/interface.go
Normal file
|
|
@ -0,0 +1,11 @@
|
||||||
|
|
||||||
|
package api
|
||||||
|
|
||||||
|
import (
|
||||||
|
"github.com/eosswedenorg-go/haproxy"
|
||||||
|
)
|
||||||
|
|
||||||
|
type ApiInterface interface {
|
||||||
|
Name() string
|
||||||
|
Call() (haproxy.HealthCheckStatus, string)
|
||||||
|
}
|
||||||
55
src/api/v1.go
Normal file
55
src/api/v1.go
Normal file
|
|
@ -0,0 +1,55 @@
|
||||||
|
|
||||||
|
package api
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"time"
|
||||||
|
"github.com/eosswedenorg-go/haproxy"
|
||||||
|
"github.com/eosswedenorg-go/eosapi"
|
||||||
|
)
|
||||||
|
|
||||||
|
type EosioV1 struct {
|
||||||
|
params eosapi.ReqParams
|
||||||
|
block_time float64
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewEosioV1(params eosapi.ReqParams, block_time float64) EosioV1 {
|
||||||
|
return EosioV1{
|
||||||
|
params: params,
|
||||||
|
block_time: block_time,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (e EosioV1) Name() string {
|
||||||
|
return "v1"
|
||||||
|
}
|
||||||
|
|
||||||
|
// check_api - Validates head block time.
|
||||||
|
// ---------------------------------------------------------
|
||||||
|
func (e EosioV1) Call() (haproxy.HealthCheckStatus, string) {
|
||||||
|
|
||||||
|
info, err := eosapi.GetInfo(e.params)
|
||||||
|
if err != nil {
|
||||||
|
msg := fmt.Sprintf("%s", err);
|
||||||
|
return haproxy.HealthCheckFailed, msg
|
||||||
|
}
|
||||||
|
|
||||||
|
// Check HTTP Status Code
|
||||||
|
if info.HTTPStatusCode > 299 {
|
||||||
|
return haproxy.HealthCheckDown,
|
||||||
|
fmt.Sprintf("Taking offline because %v was received from backend", info.HTTPStatusCode)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Validate head block.
|
||||||
|
now := time.Now().In(time.UTC)
|
||||||
|
diff := now.Sub(info.HeadBlockTime).Seconds()
|
||||||
|
|
||||||
|
if diff > e.block_time {
|
||||||
|
return haproxy.HealthCheckDown,
|
||||||
|
fmt.Sprintf("Taking offline because head block is lagging %.0f seconds", diff)
|
||||||
|
} else if diff < -e.block_time {
|
||||||
|
return haproxy.HealthCheckDown,
|
||||||
|
fmt.Sprintf("Taking offline because head block is %.0f seconds into the future", diff)
|
||||||
|
}
|
||||||
|
return haproxy.HealthCheckUp, "OK"
|
||||||
|
}
|
||||||
72
src/api/v2.go
Normal file
72
src/api/v2.go
Normal file
|
|
@ -0,0 +1,72 @@
|
||||||
|
|
||||||
|
package api
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"internal/utils"
|
||||||
|
"github.com/eosswedenorg-go/haproxy"
|
||||||
|
"github.com/eosswedenorg-go/eosapi"
|
||||||
|
)
|
||||||
|
|
||||||
|
type EosioV2 struct {
|
||||||
|
params eosapi.ReqParams
|
||||||
|
offset int64
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewEosioV2(params eosapi.ReqParams, offset int64) EosioV2 {
|
||||||
|
return EosioV2{
|
||||||
|
params: params,
|
||||||
|
offset: offset,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (e EosioV2) Name() string {
|
||||||
|
return "v2"
|
||||||
|
}
|
||||||
|
|
||||||
|
// check_api - Validates head block time.
|
||||||
|
// ---------------------------------------------------------
|
||||||
|
func (e EosioV2) Call() (haproxy.HealthCheckStatus, string) {
|
||||||
|
|
||||||
|
health, err := eosapi.GetHealth(e.params)
|
||||||
|
if err != nil {
|
||||||
|
msg := fmt.Sprintf("%s", err);
|
||||||
|
return haproxy.HealthCheckFailed, msg
|
||||||
|
}
|
||||||
|
|
||||||
|
// Check HTTP Status Code
|
||||||
|
if health.HTTPStatusCode > 299 {
|
||||||
|
return haproxy.HealthCheckDown,
|
||||||
|
fmt.Sprintf("Taking offline because %v was received from backend", health.HTTPStatusCode)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Fetch elasticsearch and nodeos block numbers from json.
|
||||||
|
var es_block int64 = 0
|
||||||
|
var node_block int64 = 0
|
||||||
|
|
||||||
|
for _, v := range health.Health {
|
||||||
|
if v.Name == "Elasticsearch" {
|
||||||
|
es_block = utils.JsonGetInt64(v.Data["last_indexed_block"])
|
||||||
|
} else if v.Name == "NodeosRPC" {
|
||||||
|
node_block = utils.JsonGetInt64(v.Data["head_block_num"])
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Error out if ether or both are zero.
|
||||||
|
if es_block == 0 || node_block == 0 {
|
||||||
|
msg := fmt.Sprintf("Failed to get Elasticsearch and/or nodeos " +
|
||||||
|
"block numbers (es: %d, eos: %d)", es_block, node_block)
|
||||||
|
return haproxy.HealthCheckFailed, msg
|
||||||
|
}
|
||||||
|
|
||||||
|
// Check if ES is behind or in the future.
|
||||||
|
diff := node_block - es_block;
|
||||||
|
if diff > e.offset {
|
||||||
|
return haproxy.HealthCheckDown,
|
||||||
|
fmt.Sprintf("Taking offline because Elastic is %d blocks behind", diff)
|
||||||
|
} else if diff < -e.offset {
|
||||||
|
return haproxy.HealthCheckDown,
|
||||||
|
fmt.Sprintf("Taking offline because Elastic is %d blocks into the future", -1 * diff)
|
||||||
|
}
|
||||||
|
return haproxy.HealthCheckUp, "OK"
|
||||||
|
}
|
||||||
123
src/server.go
123
src/server.go
|
|
@ -2,141 +2,78 @@ package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"time"
|
|
||||||
"strings"
|
"strings"
|
||||||
"strconv"
|
"strconv"
|
||||||
"internal/utils"
|
"internal/api"
|
||||||
"github.com/eosswedenorg-go/eosapi"
|
"github.com/eosswedenorg-go/eosapi"
|
||||||
"github.com/eosswedenorg-go/haproxy"
|
"github.com/eosswedenorg-go/haproxy"
|
||||||
"github.com/eosswedenorg-go/tcp_server"
|
"github.com/eosswedenorg-go/tcp_server"
|
||||||
)
|
)
|
||||||
|
|
||||||
// check_api - Validates head block time.
|
type arguments struct {
|
||||||
// ---------------------------------------------------------
|
api string
|
||||||
func check_api(p eosapi.ReqParams, block_time float64) (haproxy.HealthCheckStatus, string) {
|
url string
|
||||||
|
host string
|
||||||
info, err := eosapi.GetInfo(p)
|
block_time int
|
||||||
if err != nil {
|
|
||||||
msg := fmt.Sprintf("%s", err);
|
|
||||||
return haproxy.HealthCheckFailed, msg
|
|
||||||
}
|
|
||||||
|
|
||||||
// Check HTTP Status Code
|
|
||||||
if info.HTTPStatusCode > 299 {
|
|
||||||
return haproxy.HealthCheckDown,
|
|
||||||
fmt.Sprintf("Taking offline because %v was received from backend", info.HTTPStatusCode)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Validate head block.
|
|
||||||
now := time.Now().In(time.UTC)
|
|
||||||
diff := now.Sub(info.HeadBlockTime).Seconds()
|
|
||||||
|
|
||||||
if diff > block_time {
|
|
||||||
return haproxy.HealthCheckDown,
|
|
||||||
fmt.Sprintf("Taking offline because head block is lagging %.0f seconds", diff)
|
|
||||||
} else if diff < -block_time {
|
|
||||||
return haproxy.HealthCheckDown,
|
|
||||||
fmt.Sprintf("Taking offline because head block is %.0f seconds into the future", diff)
|
|
||||||
}
|
|
||||||
return haproxy.HealthCheckUp, "OK"
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// check_api_v2 (hyperion)
|
func createApi(a *arguments) api.ApiInterface {
|
||||||
// Validates block num diff between
|
|
||||||
// nodeos and elasticsearch
|
|
||||||
// ---------------------------------------------------------
|
|
||||||
func check_api_v2(p eosapi.ReqParams, offset int64) (haproxy.HealthCheckStatus, string) {
|
|
||||||
|
|
||||||
health, err := eosapi.GetHealth(p)
|
switch a.api {
|
||||||
if err != nil {
|
case "v2":
|
||||||
msg := fmt.Sprintf("%s", err);
|
return api.NewEosioV2(eosapi.ReqParams{Url: a.url, Host: a.host}, int64(a.block_time / 2))
|
||||||
return haproxy.HealthCheckFailed, msg
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Check HTTP Status Code
|
return api.NewEosioV1(eosapi.ReqParams{Url: a.url, Host: a.host}, float64(a.block_time))
|
||||||
if health.HTTPStatusCode > 299 {
|
|
||||||
return haproxy.HealthCheckDown,
|
|
||||||
fmt.Sprintf("Taking offline because %v was received from backend", health.HTTPStatusCode)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Fetch elasticsearch and nodeos block numbers from json.
|
|
||||||
var es_block int64 = 0
|
|
||||||
var node_block int64 = 0
|
|
||||||
|
|
||||||
for _, v := range health.Health {
|
|
||||||
if v.Name == "Elasticsearch" {
|
|
||||||
es_block = utils.JsonGetInt64(v.Data["last_indexed_block"])
|
|
||||||
} else if v.Name == "NodeosRPC" {
|
|
||||||
node_block = utils.JsonGetInt64(v.Data["head_block_num"])
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Error out if ether or both are zero.
|
|
||||||
if es_block == 0 || node_block == 0 {
|
|
||||||
msg := fmt.Sprintf("Failed to get Elasticsearch and/or nodeos " +
|
|
||||||
"block numbers (es: %d, eos: %d)", es_block, node_block)
|
|
||||||
return haproxy.HealthCheckFailed, msg
|
|
||||||
}
|
|
||||||
|
|
||||||
// Check if ES is behind or in the future.
|
|
||||||
diff := node_block - es_block;
|
|
||||||
if diff > offset {
|
|
||||||
return haproxy.HealthCheckDown,
|
|
||||||
fmt.Sprintf("Taking offline because Elastic is %d blocks behind", diff)
|
|
||||||
} else if diff < -offset {
|
|
||||||
return haproxy.HealthCheckDown,
|
|
||||||
fmt.Sprintf("Taking offline because Elastic is %d blocks into the future", -1 * diff)
|
|
||||||
}
|
|
||||||
return haproxy.HealthCheckUp, "OK"
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// onTcpMessage callback function
|
// onTcpMessage callback function
|
||||||
// ---------------------------------------------------------
|
// ---------------------------------------------------------
|
||||||
|
|
||||||
func onTcpMessage(c *tcp_server.Client, args string) {
|
func onTcpMessage(c *tcp_server.Client, args string) {
|
||||||
params := eosapi.ReqParams{}
|
a := arguments{
|
||||||
var block_time int = 10
|
api: "v1",
|
||||||
var version string = "v1"
|
block_time: 10,
|
||||||
|
}
|
||||||
|
|
||||||
|
// var url string
|
||||||
|
// var host string
|
||||||
|
// var block_time int = 10
|
||||||
|
// var version string = "v1"
|
||||||
|
|
||||||
// Parse arguments.
|
// Parse arguments.
|
||||||
// -------------------
|
// -------------------
|
||||||
split := strings.Split(strings.TrimSpace(args), "|")
|
split := strings.Split(strings.TrimSpace(args), "|")
|
||||||
|
|
||||||
// 1. url (scheme + ip/domain + port)
|
// 1. url (scheme + ip/domain + port)
|
||||||
params.Url = split[0]
|
a.url = split[0]
|
||||||
|
|
||||||
// 2. Block time.
|
// 2. Block time.
|
||||||
if len(split) > 1 {
|
if len(split) > 1 {
|
||||||
p, err := strconv.ParseInt(split[1], 10, 32)
|
num, err := strconv.ParseInt(split[1], 10, 32)
|
||||||
if err == nil {
|
if err == nil {
|
||||||
block_time = int(p)
|
a.block_time = int(num)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// 3. Version
|
// 3. Api
|
||||||
if len(split) > 2 {
|
if len(split) > 2 {
|
||||||
version = split[2]
|
a.api = split[2]
|
||||||
}
|
}
|
||||||
|
|
||||||
// 4. Host
|
// 4. Host
|
||||||
if len(split) > 3 {
|
if len(split) > 3 {
|
||||||
params.Host = split[3]
|
a.host = split[3]
|
||||||
}
|
}
|
||||||
|
|
||||||
// Check api.
|
// Check api.
|
||||||
// -------------------
|
// -------------------
|
||||||
var status haproxy.HealthCheckStatus
|
healthCheckApi := createApi(&a)
|
||||||
var msg string
|
|
||||||
|
|
||||||
if version == "v2" {
|
status, msg := healthCheckApi.Call()
|
||||||
status, msg = check_api_v2(params, int64(block_time / 2))
|
|
||||||
} else {
|
|
||||||
version = "v1"
|
|
||||||
status, msg = check_api(params, float64(block_time))
|
|
||||||
}
|
|
||||||
|
|
||||||
logger.Info("API Check", "version", version, "url", params.Url,
|
logger.Info("API Check", "type", healthCheckApi.Name(), "url", a.url,
|
||||||
"block", block_time / 2, "status", status)
|
"block", a.block_time / 2, "status", status)
|
||||||
|
|
||||||
if status != haproxy.HealthCheckUp && len(msg) > 0 {
|
if status != haproxy.HealthCheckUp && len(msg) > 0 {
|
||||||
logger.Warn("API Check Failed", "message", msg)
|
logger.Warn("API Check Failed", "message", msg)
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue