mirror of
https://github.com/eosswedenorg/antelope-api-healthcheck
synced 2026-06-18 05:00:03 +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
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"
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue