1
0
Fork 0
mirror of https://github.com/eosswedenorg/antelope-api-healthcheck synced 2026-06-16 04:44:55 +02:00

Adding src/api/eosio-contract.go

This commit is contained in:
Henrik Hautakoski 2022-02-21 13:43:00 +01:00
parent be945886e4
commit 415b1346dc
No known key found for this signature in database
GPG key ID: 608414D93E862CCD
3 changed files with 82 additions and 1 deletions

4
go.mod
View file

@ -3,6 +3,7 @@ module github.com/eosswedenorg/eosio-api-healthcheck
go 1.14
require (
github.com/eosswedenorg-go/eos-contract-api-client v0.0.0-20220221105418-dc591fcc0dc5 // indirect
github.com/eosswedenorg-go/eosapi v0.1.0
github.com/eosswedenorg-go/haproxy v0.0.0-20220101140534-fccfdd93a8cd
github.com/eosswedenorg-go/pid v1.0.0
@ -11,9 +12,10 @@ require (
github.com/inconshreveable/log15 v0.0.0-20201112154412-8562bdadbbac
github.com/mattn/go-colorable v0.1.12 // indirect
github.com/pborman/getopt/v2 v2.1.0
internal/api v1.0.0
internal/utils v1.0.0
internal/api v1.0.0
)
replace internal/utils => ./src/utils
replace internal/api => ./src/api

5
go.sum
View file

@ -1,6 +1,8 @@
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/eosswedenorg-go/eos-contract-api-client v0.0.0-20220221105418-dc591fcc0dc5 h1:Xd6K6u8Bt1s9Szh3TydkkdwOEWBh4ThkHU0rbiO5BAI=
github.com/eosswedenorg-go/eos-contract-api-client v0.0.0-20220221105418-dc591fcc0dc5/go.mod h1:o/H/XtSXtaNe2ooXIAcp9vApmy2pY5YyeSl3yWgEnLw=
github.com/eosswedenorg-go/eosapi v0.1.0 h1:SnVMx1QGPBZoQknjnAiGzjL6hVfrXPLrOdxoUstYUrk=
github.com/eosswedenorg-go/eosapi v0.1.0/go.mod h1:7VrkU30cSqRtGDE6bXygWqMcEhCyWOaC9yVA34QIQzM=
github.com/eosswedenorg-go/haproxy v0.0.0-20220101140534-fccfdd93a8cd h1:e59v3HnnG60uE50OtziOxyiSNDADr1lDbzn33btn3yE=
@ -37,6 +39,9 @@ github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZN
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.3.0 h1:TivCn/peBQ7UY8ooIcPgZFpTNSz0Q2U6UrFlUfqbe0Q=
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
golang.org/x/sys v0.0.0-20210630005230-0f9fa26af87c/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20210927094055-39ccf1dd6fa6 h1:foEbQz/B0Oz6YIqu/69kfXPYeFQAuuMYFkjaqXzl5Wo=
golang.org/x/sys v0.0.0-20210927094055-39ccf1dd6fa6/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=

74
src/api/eosio-contract.go Normal file
View file

@ -0,0 +1,74 @@
package api
import (
"fmt"
"time"
"github.com/eosswedenorg-go/haproxy"
contract_api "github.com/eosswedenorg-go/eos-contract-api-client"
)
type EosioContract struct {
client contract_api.Client
block_time float64
}
func NewEosioContract(url string, block_time float64) EosioContract {
return EosioContract{
client: contract_api.Client{
Url: url,
},
block_time: block_time,
}
}
func (e EosioContract) LogInfo() LogParams {
return LogParams{
"type", "eosio-contract",
"url", e.client.Url,
"block_time", e.block_time,
}
}
// check_api - Validates head block time.
// ---------------------------------------------------------
func (e EosioContract) Call() (haproxy.HealthCheckStatus, string) {
h, err := e.client.GetHealth()
if err != nil {
msg := fmt.Sprintf("%s", err);
return haproxy.HealthCheckFailed, msg
}
// Check HTTP Status Code
if h.HTTPStatusCode > 299 {
return haproxy.HealthCheckDown,
fmt.Sprintf("Taking offline because %v was received from backend", h.HTTPStatusCode)
}
// Check postgres
if h.Data.Postgres.Status != "OK" {
return haproxy.HealthCheckDown,
fmt.Sprintf("Taking offline because Postgres reported '%s'", h.Data.Postgres.Status)
}
// Check redis
if h.Data.Redis.Status != "OK" {
return haproxy.HealthCheckDown,
fmt.Sprintf("Taking offline because Redis reported '%s'", h.Data.Postgres.Status)
}
// Validate head block.
now := time.Now().In(time.UTC)
diff := now.Sub(h.Data.Chain.HeadTime).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"
}