-
Notifications
You must be signed in to change notification settings - Fork 660
/
client.go
75 lines (62 loc) · 2.19 KB
/
client.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
// Copyright (C) 2019-2021, Ava Labs, Inc. All rights reserved.
// See the file LICENSE for licensing terms.
package health
import (
"context"
"time"
"github.com/ava-labs/avalanchego/utils/rpc"
)
var _ Client = &client{}
// Client interface for Avalanche Health API Endpoint
type Client interface {
// Readiness returns if the node has finished initialization
Readiness(context.Context, ...rpc.Option) (*APIHealthReply, error)
// Health returns a summation of the health of the node
Health(context.Context, ...rpc.Option) (*APIHealthReply, error)
// Liveness returns if the node is in need of a restart
Liveness(context.Context, ...rpc.Option) (*APIHealthReply, error)
// AwaitHealthy queries the Health endpoint with a pause of [interval]
// in between checks and returns early if Health returns healthy
AwaitHealthy(ctx context.Context, freq time.Duration, options ...rpc.Option) (bool, error)
}
// Client implementation for Avalanche Health API Endpoint
type client struct {
requester rpc.EndpointRequester
}
// NewClient returns a client to interact with Health API endpoint
func NewClient(uri string) Client {
return &client{requester: rpc.NewEndpointRequester(
uri+"/ext/health",
"health",
)}
}
func (c *client) Readiness(ctx context.Context, options ...rpc.Option) (*APIHealthReply, error) {
res := &APIHealthReply{}
err := c.requester.SendRequest(ctx, "readiness", struct{}{}, res, options...)
return res, err
}
func (c *client) Health(ctx context.Context, options ...rpc.Option) (*APIHealthReply, error) {
res := &APIHealthReply{}
err := c.requester.SendRequest(ctx, "health", struct{}{}, res, options...)
return res, err
}
func (c *client) Liveness(ctx context.Context, options ...rpc.Option) (*APIHealthReply, error) {
res := &APIHealthReply{}
err := c.requester.SendRequest(ctx, "liveness", struct{}{}, res, options...)
return res, err
}
func (c *client) AwaitHealthy(ctx context.Context, freq time.Duration, options ...rpc.Option) (bool, error) {
ticker := time.NewTicker(freq)
defer ticker.Stop()
for {
res, err := c.Health(ctx, options...)
if err == nil && res.Healthy {
return true, nil
}
select {
case <-ticker.C:
case <-ctx.Done():
return false, ctx.Err()
}
}
}