1
0
Fork 0
mirror of https://github.com/eosswedenorg/antelope-api-healthcheck synced 2026-06-18 05:00:03 +02:00

internal/api/interface.go: make Call() accept a context as parameter.

This commit is contained in:
Henrik Hautakoski 2023-02-06 14:20:09 +01:00
parent b815bcee44
commit dbf59d1305
No known key found for this signature in database
GPG key ID: 217490840C18A5D9
10 changed files with 47 additions and 30 deletions

View file

@ -1,6 +1,7 @@
package api
import (
"context"
"fmt"
"github.com/eosswedenorg-go/haproxy/agentcheck"
@ -44,7 +45,8 @@ func (e AntelopeV1) LogInfo() LogParams {
return p
}
func (e AntelopeV1) Call() (agentcheck.Response, string) {
func (e AntelopeV1) Call(ctx context.Context) (agentcheck.Response, string) {
// TODO: Pass context
info, err := e.client.GetInfo()
if err != nil {
resp := agentcheck.NewStatusMessageResponse(agentcheck.Fail, "")

View file

@ -1,6 +1,7 @@
package api
import (
"context"
"net/http"
"net/http/httptest"
"testing"
@ -51,7 +52,7 @@ func TestAntelopeV1_JsonFailure(t *testing.T) {
}))
api := NewAntelopeV1(srv.URL, "", 120)
check, _ := api.Call()
check, _ := api.Call(context.Background())
expected := agentcheck.NewStatusMessageResponse(agentcheck.Fail, "")
assert.Equal(t, expected, check)
@ -65,7 +66,7 @@ func TestAntelopeV1_HTTP500Failed(t *testing.T) {
}))
api := NewAntelopeV1(srv.URL, "", 120)
check, status := api.Call()
check, status := api.Call(context.Background())
assert.Equal(t, "server returned HTTP 500 Internal Server Error", status)
@ -89,7 +90,7 @@ func TestAntelopeV1_LaggingUp(t *testing.T) {
api := NewAntelopeV1(srv.URL, "", 60)
api.SetTime(time.Date(2022, 2, 24, 13, 38, 0, 0, time.UTC))
check, status := api.Call()
check, status := api.Call(context.Background())
assert.Equal(t, "OK", status)
@ -113,7 +114,7 @@ func TestAntelopeV1_LaggingDown(t *testing.T) {
api := NewAntelopeV1(srv.URL, "", 60)
api.SetTime(time.Date(2018, time.January, 1, 13, 38, 2, 0, time.UTC))
check, status := api.Call()
check, status := api.Call(context.Background())
assert.Equal(t, "Taking offline because head block is lagging 61 seconds", status)
@ -137,7 +138,7 @@ func TestAntelopeV1_TimeInFutureUP(t *testing.T) {
api := NewAntelopeV1(srv.URL, "", 120)
api.SetTime(time.Date(2020, 9, 22, 9, 30, 0, 0, time.UTC))
check, status := api.Call()
check, status := api.Call(context.Background())
assert.Equal(t, "OK", status)
@ -161,7 +162,7 @@ func TestAntelopeV1_TimeInFutureDown(t *testing.T) {
api := NewAntelopeV1(srv.URL, "", 120)
api.SetTime(time.Date(2019, time.April, 14, 12, 0, 0, 0, time.UTC))
check, status := api.Call()
check, status := api.Call(context.Background())
assert.Equal(t, "Taking offline because head block is -121 seconds into the future", status)

View file

@ -1,6 +1,7 @@
package api
import (
"context"
"fmt"
"github.com/eosswedenorg-go/haproxy/agentcheck"
@ -43,7 +44,8 @@ func (e AntelopeV2) LogInfo() LogParams {
return p
}
func (e AntelopeV2) Call() (agentcheck.Response, string) {
func (e AntelopeV2) Call(ctx context.Context) (agentcheck.Response, string) {
// TODO: Pass context
health, err := e.client.GetHealth()
if err != nil {
resp := agentcheck.NewStatusMessageResponse(agentcheck.Fail, "")

View file

@ -1,6 +1,7 @@
package api
import (
"context"
"net/http"
"net/http/httptest"
"testing"
@ -39,7 +40,7 @@ func TestAntelopeV2_JsonFailure(t *testing.T) {
}))
api := NewAntelopeV2(srv.URL, "", 120)
check, _ := api.Call()
check, _ := api.Call(context.Background())
expected := agentcheck.NewStatusMessageResponse(agentcheck.Fail, "")
assert.Equal(t, expected, check)
@ -53,7 +54,7 @@ func TestAntelopeV2_HTTP500Failed(t *testing.T) {
}))
api := NewAntelopeV2(srv.URL, "", 120)
check, status := api.Call()
check, status := api.Call(context.Background())
assert.Equal(t, "server returned HTTP 500 Internal Server Error", status)
@ -100,7 +101,7 @@ func TestAntelopeV2_LaggingUp(t *testing.T) {
}))
api := NewAntelopeV2(srv.URL, "", 500)
check, status := api.Call()
check, status := api.Call(context.Background())
assert.Equal(t, "OK", status)
@ -147,7 +148,7 @@ func TestAntelopeV2_LaggingDown(t *testing.T) {
}))
api := NewAntelopeV2(srv.URL, "", 499)
check, status := api.Call()
check, status := api.Call(context.Background())
assert.Equal(t, "Taking offline because Elastic is 500 blocks behind", status)
@ -194,7 +195,7 @@ func TestAntelopeV2_LaggingESInFutureUP(t *testing.T) {
}))
api := NewAntelopeV2(srv.URL, "", 200)
check, status := api.Call()
check, status := api.Call(context.Background())
assert.Equal(t, "OK", status)
@ -241,7 +242,7 @@ func TestAntelopeV2_LaggingESInFutureDown(t *testing.T) {
}))
api := NewAntelopeV2(srv.URL, "", 200)
check, status := api.Call()
check, status := api.Call(context.Background())
assert.Equal(t, "Taking offline because Elastic is 201 blocks into the future", status)
@ -288,7 +289,7 @@ func TestAntelopeV2_ElasticsFailed(t *testing.T) {
}))
api := NewAntelopeV2(srv.URL, "", 500)
check, status := api.Call()
check, status := api.Call(context.Background())
assert.Equal(t, "Failed to get Elasticsearch and/or nodeos block numbers (es: 0, eos: 263148621)", status)
@ -335,7 +336,7 @@ func TestAntelopeV2_NodeosRPCFailed(t *testing.T) {
}))
api := NewAntelopeV2(srv.URL, "", 500)
check, status := api.Call()
check, status := api.Call(context.Background())
assert.Equal(t, "Failed to get Elasticsearch and/or nodeos block numbers (es: 263148121, eos: 0)", status)
@ -372,7 +373,7 @@ func TestAntelopeV2_ElasticsNodeosRPCFailed(t *testing.T) {
}))
api := NewAntelopeV2(srv.URL, "", 500)
check, status := api.Call()
check, status := api.Call(context.Background())
assert.Equal(t, "Failed to get Elasticsearch and/or nodeos block numbers (es: 0, eos: 0)", status)

View file

@ -1,6 +1,7 @@
package api
import (
"context"
"fmt"
"github.com/eosswedenorg-go/atomicasset"
@ -35,7 +36,8 @@ func (e AtomicAsset) LogInfo() LogParams {
}
}
func (e AtomicAsset) Call() (agentcheck.Response, string) {
func (e AtomicAsset) Call(ctx context.Context) (agentcheck.Response, string) {
// TODO: Pass context
h, err := e.client.GetHealth()
if err != nil {
resp := agentcheck.NewStatusMessageResponse(agentcheck.Fail, "")

View file

@ -1,6 +1,7 @@
package api
import (
"context"
"net/http"
"net/http/httptest"
"testing"
@ -50,7 +51,7 @@ func TestAtomicAsset_JsonFailure(t *testing.T) {
}))
api := NewAtomicAsset(srv.URL, 120)
check, _ := api.Call()
check, _ := api.Call(context.Background())
expected := agentcheck.NewStatusMessageResponse(agentcheck.Fail, "")
assert.Equal(t, expected, check)
@ -65,7 +66,7 @@ func TestAtomicAsset_HTTP500Down(t *testing.T) {
}))
api := NewAtomicAsset(srv.URL, 120)
check, status := api.Call()
check, status := api.Call(context.Background())
assert.Equal(t, "Taking offline because 500 was received from backend", status)
@ -104,7 +105,7 @@ func TestAtomicAsset_LaggingUp(t *testing.T) {
api := NewAtomicAsset(srv.URL, 120)
api.SetTime(time.Date(2025, 10, 8, 20, 7, 27, 0, time.UTC))
check, status := api.Call()
check, status := api.Call(context.Background())
assert.Equal(t, "OK", status)
@ -143,7 +144,7 @@ func TestAtomicAsset_LaggingDown(t *testing.T) {
api := NewAtomicAsset(srv.URL, 120)
api.SetTime(time.Date(2018, 8, 5, 6, 53, 35, 0, time.UTC))
check, status := api.Call()
check, status := api.Call(context.Background())
assert.Equal(t, "Taking offline because head block is lagging 121 seconds", status)
@ -182,7 +183,7 @@ func TestAtomicAsset_InFutureUp(t *testing.T) {
api := NewAtomicAsset(srv.URL, 120)
api.SetTime(time.Date(2024, 10, 15, 1, 9, 16, 500, time.UTC))
check, status := api.Call()
check, status := api.Call(context.Background())
assert.Equal(t, "OK", status)
@ -221,7 +222,7 @@ func TestAtomicAsset_InFutureDown(t *testing.T) {
api := NewAtomicAsset(srv.URL, 120)
api.SetTime(time.Date(2002, 12, 29, 0, 45, 0o3, 500, time.UTC))
check, status := api.Call()
check, status := api.Call(context.Background())
assert.Equal(t, "Taking offline because head block is -121 seconds into the future", status)
@ -260,7 +261,7 @@ func TestAtomicAsset_RedisDown(t *testing.T) {
api := NewAtomicAsset(srv.URL, 120)
api.SetTime(time.Date(2015, 3, 11, 11, 19, 30, 500, time.UTC))
check, status := api.Call()
check, status := api.Call(context.Background())
assert.Equal(t, "Taking offline because Redis reported 'DOWN'", status)
@ -299,7 +300,7 @@ func TestAtomicAsset_PostgresDown(t *testing.T) {
api := NewAtomicAsset(srv.URL, 120)
api.SetTime(time.Date(2019, 7, 11, 18, 6, 11, 500, time.UTC))
check, status := api.Call()
check, status := api.Call(context.Background())
assert.Equal(t, "Taking offline because Postgres reported 'DOWN'", status)

View file

@ -1,6 +1,7 @@
package api
import (
"context"
"strings"
"github.com/eosswedenorg-go/haproxy/agentcheck"
@ -41,6 +42,6 @@ func (d DebugApi) LogInfo() LogParams {
}
}
func (d DebugApi) Call() (agentcheck.Response, string) {
func (d DebugApi) Call(_ context.Context) (agentcheck.Response, string) {
return d.response, ""
}

View file

@ -1,6 +1,7 @@
package api
import (
"context"
"reflect"
"testing"
@ -59,7 +60,7 @@ func TestDebugApi_Call(t *testing.T) {
response: expected,
}
response, msg := api.Call()
response, msg := api.Call(context.Background())
assert.Equal(t, response, expected)
assert.Equal(t, msg, "")

View file

@ -1,6 +1,8 @@
package api
import (
"context"
"github.com/eosswedenorg-go/haproxy/agentcheck"
)
@ -27,5 +29,5 @@ type ApiInterface interface {
LogInfo() LogParams
// Call api and validate it's status.
Call() (agentcheck.Response, string)
Call(ctx context.Context) (agentcheck.Response, string)
}

View file

@ -108,8 +108,12 @@ func (s *Server) OnTraffic(c gnet.Conn) gnet.Action {
// gnet library does not like blocking calls.
// as we do a blocking http call here, we need to wrap it in a goroutine.
go func() {
// Make a context with 30 sec timeout per default. Should be "enough" for most cases.
ctx, cancel := context.WithTimeout(context.Background(), time.Second*30)
defer cancel()
t := time.Now()
status, msg := healthCheckApi.Call()
status, msg := healthCheckApi.Call(ctx)
req_time := time.Since(t)
params := api.LogParams{}