-
Notifications
You must be signed in to change notification settings - Fork 669
/
client.go
51 lines (43 loc) · 1.38 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
// (c) 2020, Ava Labs, Inc. All rights reserved.
// See the file LICENSE for licensing terms.
package health
import (
"time"
"github.com/ava-labs/avalanchego/utils/rpc"
)
// Client 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, requestTimeout time.Duration) *Client {
return &Client{
requester: rpc.NewEndpointRequester(uri, "/ext/health", "health", requestTimeout),
}
}
// GetLiveness returns a health check on the Avalanche node
func (c *Client) GetLiveness() (*APIHealthReply, error) {
res := &APIHealthReply{}
err := c.requester.SendRequest("getLiveness", struct{}{}, res)
return res, err
}
// Health returns a health check on the Avalanche node
func (c *Client) Health() (*APIHealthReply, error) {
res := &APIHealthReply{}
err := c.requester.SendRequest("health", struct{}{}, res)
return res, err
}
// AwaitHealthy queries the GetLiveness endpoint [checks] times, with a pause of [interval]
// in between checks and returns early if GetLiveness returns healthy
func (c *Client) AwaitHealthy(checks int, interval time.Duration) (bool, error) {
var err error
for i := 0; i < checks; i++ {
var res *APIHealthReply
res, err = c.GetLiveness()
if err == nil && res.Healthy {
return true, nil
}
time.Sleep(interval)
}
return false, err
}