Skip to content

Commit

Permalink
Add /network/health endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
muXxer committed Mar 19, 2024
1 parent 6304dd5 commit 091b492
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 0 deletions.
5 changes: 5 additions & 0 deletions api/endpoints.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,10 @@ const (
// MIMEApplicationVendorIOTASerializerV2 => bytes.
CoreEndpointInfo = "/info"

// CoreEndpointNetworkHealth is the endpoint for getting the network health.
// GET returns http status code 200 if the network is healthy (finalization is not delayed).
CoreEndpointNetworkHealth = "/network/health"

// CoreEndpointNetworkMetrics is the endpoint for getting the network metrics.
// GET returns the network metrics.
// "Accept" header:
Expand Down Expand Up @@ -289,6 +293,7 @@ const (

var (
CoreRouteInfo = route(CorePluginName, CoreEndpointInfo)
CoreRouteNetworkHealth = route(CorePluginName, CoreEndpointNetworkHealth)
CoreRouteNetworkMetrics = route(CorePluginName, CoreEndpointNetworkMetrics)
CoreRouteBlocks = route(CorePluginName, CoreEndpointBlocks)
CoreRouteBlock = route(CorePluginName, CoreEndpointBlock)
Expand Down
14 changes: 14 additions & 0 deletions nodeclient/http_api_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,20 @@ func (client *Client) Info(ctx context.Context) (*api.InfoResponse, error) {
return res, nil
}

// NetworkHealth returns whether the network is healthy (finalization is not delayed).
func (client *Client) NetworkHealth(ctx context.Context) (bool, error) {
//nolint:bodyclose
if _, err := client.Do(ctx, http.MethodGet, api.CoreRouteNetworkHealth, nil, nil); err != nil {
if ierrors.Is(err, ErrHTTPServiceUnavailable) {
return false, nil
}

return false, err
}

return true, nil
}

// NetworkMetrics gets the current network metrics.
func (client *Client) NetworkMetrics(ctx context.Context) (*api.NetworkMetricsResponse, error) {
res := new(api.NetworkMetricsResponse)
Expand Down
21 changes: 21 additions & 0 deletions nodeclient/http_api_client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,27 @@ func TestClient_Health(t *testing.T) {
require.False(t, healthy)
}

func TestClient_NetworkHealth(t *testing.T) {
defer gock.Off()

gock.New(nodeAPIUrl).
Get(api.CoreRouteNetworkHealth).
Reply(200)

nodeAPI := nodeClient(t)
healthy, err := nodeAPI.NetworkHealth(context.Background())
require.NoError(t, err)
require.True(t, healthy)

gock.New(nodeAPIUrl).
Get(api.CoreRouteNetworkHealth).
Reply(503)

healthy, err = nodeAPI.NetworkHealth(context.Background())
require.NoError(t, err)
require.False(t, healthy)
}

func TestClient_NetworkMetrics(t *testing.T) {
defer gock.Off()

Expand Down

0 comments on commit 091b492

Please sign in to comment.