Files
zeekayandHanzo Dev f7aefc8d57 Our nodes serve /v1/, so ask them for /v1/
luxd registers /v1/ and nothing else; api.lux.network, api.zoo.ngo,
api.hanzo.network, api.pars.network and api.osage.network all answer 200 on
/v1/bc/C/rpc and 404 on /ext/bc/C/rpc. Every /ext/ path aimed at one of our
hosts was a request that could only fail.

/ext/ is Avalanche's prefix. Endpoints on api.avax.network keep it — they are
someone else's convention and a real bridge corridor depends on them.

Co-authored-by: Hanzo Dev <[email protected]>
2026-08-29 10:45:40 -07:00

86 lines
2.7 KiB
Go

// Copyright (C) 2019-2025, Lux Industries, Inc. All rights reserved.
// See the file LICENSE for licensing terms.
package health
import (
"context"
"time"
apihealth "github.com/luxfi/api/health"
"github.com/luxfi/rpc"
)
type Client struct {
Requester rpc.EndpointRequester
}
func NewClient(uri string) *Client {
return &Client{Requester: rpc.NewEndpointRequester(
uri + "/v1/health",
)}
}
// Readiness returns if the node has finished initialization
func (c *Client) Readiness(ctx context.Context, tags []string, options ...rpc.Option) (*apihealth.APIReply, error) {
res := &apihealth.APIReply{}
err := c.Requester.SendRequest(ctx, "health.readiness", &apihealth.APIArgs{Tags: tags}, res, options...)
return res, err
}
// Health returns a summation of the health of the node
func (c *Client) Health(ctx context.Context, tags []string, options ...rpc.Option) (*apihealth.APIReply, error) {
res := &apihealth.APIReply{}
err := c.Requester.SendRequest(ctx, "health.health", &apihealth.APIArgs{Tags: tags}, res, options...)
return res, err
}
// Liveness returns if the node is in need of a restart
func (c *Client) Liveness(ctx context.Context, tags []string, options ...rpc.Option) (*apihealth.APIReply, error) {
res := &apihealth.APIReply{}
err := c.Requester.SendRequest(ctx, "health.liveness", &apihealth.APIArgs{Tags: tags}, res, options...)
return res, err
}
// AwaitReady polls the node every [freq] until the node reports ready.
// Only returns an error if [ctx] returns an error.
func AwaitReady(ctx context.Context, c *Client, freq time.Duration, tags []string, options ...rpc.Option) (bool, error) {
return await(ctx, freq, c.Readiness, tags, options...)
}
// AwaitHealthy polls the node every [freq] until the node reports healthy.
// Only returns an error if [ctx] returns an error.
func AwaitHealthy(ctx context.Context, c *Client, freq time.Duration, tags []string, options ...rpc.Option) (bool, error) {
return await(ctx, freq, c.Health, tags, options...)
}
// AwaitAlive polls the node every [freq] until the node reports liveness.
// Only returns an error if [ctx] returns an error.
func AwaitAlive(ctx context.Context, c *Client, freq time.Duration, tags []string, options ...rpc.Option) (bool, error) {
return await(ctx, freq, c.Liveness, tags, options...)
}
func await(
ctx context.Context,
freq time.Duration,
check func(ctx context.Context, tags []string, options ...rpc.Option) (*apihealth.APIReply, error),
tags []string,
options ...rpc.Option,
) (bool, error) {
ticker := time.NewTicker(freq)
defer ticker.Stop()
for {
res, err := check(ctx, tags, options...)
if err == nil && res.Healthy {
return true, nil
}
select {
case <-ticker.C:
case <-ctx.Done():
return false, ctx.Err()
}
}
}