Skip to content

Commit 2ddfa05

Browse files
Simplify json-rpc client handling (#4079)
1 parent 66ce7a7 commit 2ddfa05

File tree

22 files changed

+356
-583
lines changed

22 files changed

+356
-583
lines changed

api/admin/client.go

Lines changed: 30 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -14,86 +14,65 @@ import (
1414
"github.com/ava-labs/avalanchego/utils/rpc"
1515
)
1616

17-
var _ Client = (*client)(nil)
18-
19-
// Client interface for the Avalanche Platform Info API Endpoint
20-
type Client interface {
21-
StartCPUProfiler(context.Context, ...rpc.Option) error
22-
StopCPUProfiler(context.Context, ...rpc.Option) error
23-
MemoryProfile(context.Context, ...rpc.Option) error
24-
LockProfile(context.Context, ...rpc.Option) error
25-
Alias(ctx context.Context, endpoint string, alias string, options ...rpc.Option) error
26-
AliasChain(ctx context.Context, chainID string, alias string, options ...rpc.Option) error
27-
GetChainAliases(ctx context.Context, chainID string, options ...rpc.Option) ([]string, error)
28-
Stacktrace(context.Context, ...rpc.Option) error
29-
LoadVMs(context.Context, ...rpc.Option) (map[ids.ID][]string, map[ids.ID]string, error)
30-
SetLoggerLevel(ctx context.Context, loggerName, logLevel, displayLevel string, options ...rpc.Option) (map[string]LogAndDisplayLevels, error)
31-
GetLoggerLevel(ctx context.Context, loggerName string, options ...rpc.Option) (map[string]LogAndDisplayLevels, error)
32-
GetConfig(ctx context.Context, options ...rpc.Option) (interface{}, error)
33-
DBGet(ctx context.Context, key []byte, options ...rpc.Option) ([]byte, error)
17+
type Client struct {
18+
Requester rpc.EndpointRequester
3419
}
3520

36-
// Client implementation for the Avalanche Platform Info API Endpoint
37-
type client struct {
38-
requester rpc.EndpointRequester
39-
}
40-
41-
// NewClient returns a new Info API Client
42-
func NewClient(uri string) Client {
43-
return &client{requester: rpc.NewEndpointRequester(
21+
func NewClient(uri string) *Client {
22+
return &Client{Requester: rpc.NewEndpointRequester(
4423
uri + "/ext/admin",
4524
)}
4625
}
4726

48-
func (c *client) StartCPUProfiler(ctx context.Context, options ...rpc.Option) error {
49-
return c.requester.SendRequest(ctx, "admin.startCPUProfiler", struct{}{}, &api.EmptyReply{}, options...)
27+
func (c *Client) StartCPUProfiler(ctx context.Context, options ...rpc.Option) error {
28+
return c.Requester.SendRequest(ctx, "admin.startCPUProfiler", struct{}{}, &api.EmptyReply{}, options...)
5029
}
5130

52-
func (c *client) StopCPUProfiler(ctx context.Context, options ...rpc.Option) error {
53-
return c.requester.SendRequest(ctx, "admin.stopCPUProfiler", struct{}{}, &api.EmptyReply{}, options...)
31+
func (c *Client) StopCPUProfiler(ctx context.Context, options ...rpc.Option) error {
32+
return c.Requester.SendRequest(ctx, "admin.stopCPUProfiler", struct{}{}, &api.EmptyReply{}, options...)
5433
}
5534

56-
func (c *client) MemoryProfile(ctx context.Context, options ...rpc.Option) error {
57-
return c.requester.SendRequest(ctx, "admin.memoryProfile", struct{}{}, &api.EmptyReply{}, options...)
35+
func (c *Client) MemoryProfile(ctx context.Context, options ...rpc.Option) error {
36+
return c.Requester.SendRequest(ctx, "admin.memoryProfile", struct{}{}, &api.EmptyReply{}, options...)
5837
}
5938

60-
func (c *client) LockProfile(ctx context.Context, options ...rpc.Option) error {
61-
return c.requester.SendRequest(ctx, "admin.lockProfile", struct{}{}, &api.EmptyReply{}, options...)
39+
func (c *Client) LockProfile(ctx context.Context, options ...rpc.Option) error {
40+
return c.Requester.SendRequest(ctx, "admin.lockProfile", struct{}{}, &api.EmptyReply{}, options...)
6241
}
6342

64-
func (c *client) Alias(ctx context.Context, endpoint, alias string, options ...rpc.Option) error {
65-
return c.requester.SendRequest(ctx, "admin.alias", &AliasArgs{
43+
func (c *Client) Alias(ctx context.Context, endpoint, alias string, options ...rpc.Option) error {
44+
return c.Requester.SendRequest(ctx, "admin.alias", &AliasArgs{
6645
Endpoint: endpoint,
6746
Alias: alias,
6847
}, &api.EmptyReply{}, options...)
6948
}
7049

71-
func (c *client) AliasChain(ctx context.Context, chain, alias string, options ...rpc.Option) error {
72-
return c.requester.SendRequest(ctx, "admin.aliasChain", &AliasChainArgs{
50+
func (c *Client) AliasChain(ctx context.Context, chain, alias string, options ...rpc.Option) error {
51+
return c.Requester.SendRequest(ctx, "admin.aliasChain", &AliasChainArgs{
7352
Chain: chain,
7453
Alias: alias,
7554
}, &api.EmptyReply{}, options...)
7655
}
7756

78-
func (c *client) GetChainAliases(ctx context.Context, chain string, options ...rpc.Option) ([]string, error) {
57+
func (c *Client) GetChainAliases(ctx context.Context, chain string, options ...rpc.Option) ([]string, error) {
7958
res := &GetChainAliasesReply{}
80-
err := c.requester.SendRequest(ctx, "admin.getChainAliases", &GetChainAliasesArgs{
59+
err := c.Requester.SendRequest(ctx, "admin.getChainAliases", &GetChainAliasesArgs{
8160
Chain: chain,
8261
}, res, options...)
8362
return res.Aliases, err
8463
}
8564

86-
func (c *client) Stacktrace(ctx context.Context, options ...rpc.Option) error {
87-
return c.requester.SendRequest(ctx, "admin.stacktrace", struct{}{}, &api.EmptyReply{}, options...)
65+
func (c *Client) Stacktrace(ctx context.Context, options ...rpc.Option) error {
66+
return c.Requester.SendRequest(ctx, "admin.stacktrace", struct{}{}, &api.EmptyReply{}, options...)
8867
}
8968

90-
func (c *client) LoadVMs(ctx context.Context, options ...rpc.Option) (map[ids.ID][]string, map[ids.ID]string, error) {
69+
func (c *Client) LoadVMs(ctx context.Context, options ...rpc.Option) (map[ids.ID][]string, map[ids.ID]string, error) {
9170
res := &LoadVMsReply{}
92-
err := c.requester.SendRequest(ctx, "admin.loadVMs", struct{}{}, res, options...)
71+
err := c.Requester.SendRequest(ctx, "admin.loadVMs", struct{}{}, res, options...)
9372
return res.NewVMs, res.FailedVMs, err
9473
}
9574

96-
func (c *client) SetLoggerLevel(
75+
func (c *Client) SetLoggerLevel(
9776
ctx context.Context,
9877
loggerName,
9978
logLevel,
@@ -118,40 +97,40 @@ func (c *client) SetLoggerLevel(
11897
}
11998
}
12099
res := &LoggerLevelReply{}
121-
err = c.requester.SendRequest(ctx, "admin.setLoggerLevel", &SetLoggerLevelArgs{
100+
err = c.Requester.SendRequest(ctx, "admin.setLoggerLevel", &SetLoggerLevelArgs{
122101
LoggerName: loggerName,
123102
LogLevel: &logLevelArg,
124103
DisplayLevel: &displayLevelArg,
125104
}, res, options...)
126105
return res.LoggerLevels, err
127106
}
128107

129-
func (c *client) GetLoggerLevel(
108+
func (c *Client) GetLoggerLevel(
130109
ctx context.Context,
131110
loggerName string,
132111
options ...rpc.Option,
133112
) (map[string]LogAndDisplayLevels, error) {
134113
res := &LoggerLevelReply{}
135-
err := c.requester.SendRequest(ctx, "admin.getLoggerLevel", &GetLoggerLevelArgs{
114+
err := c.Requester.SendRequest(ctx, "admin.getLoggerLevel", &GetLoggerLevelArgs{
136115
LoggerName: loggerName,
137116
}, res, options...)
138117
return res.LoggerLevels, err
139118
}
140119

141-
func (c *client) GetConfig(ctx context.Context, options ...rpc.Option) (interface{}, error) {
120+
func (c *Client) GetConfig(ctx context.Context, options ...rpc.Option) (interface{}, error) {
142121
var res interface{}
143-
err := c.requester.SendRequest(ctx, "admin.getConfig", struct{}{}, &res, options...)
122+
err := c.Requester.SendRequest(ctx, "admin.getConfig", struct{}{}, &res, options...)
144123
return res, err
145124
}
146125

147-
func (c *client) DBGet(ctx context.Context, key []byte, options ...rpc.Option) ([]byte, error) {
126+
func (c *Client) DBGet(ctx context.Context, key []byte, options ...rpc.Option) ([]byte, error) {
148127
keyStr, err := formatting.Encode(formatting.HexNC, key)
149128
if err != nil {
150129
return nil, err
151130
}
152131

153132
res := &DBGetReply{}
154-
err = c.requester.SendRequest(ctx, "admin.dbGet", &DBGetArgs{
133+
err = c.Requester.SendRequest(ctx, "admin.dbGet", &DBGetArgs{
155134
Key: keyStr,
156135
}, res, options...)
157136
if err != nil {

api/admin/client_test.go

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ func (mc *mockClient) SendRequest(_ context.Context, _ string, _ interface{}, re
7777
func TestStartCPUProfiler(t *testing.T) {
7878
for _, test := range SuccessResponseTests {
7979
t.Run(test.name, func(t *testing.T) {
80-
mockClient := client{requester: NewMockClient(&api.EmptyReply{}, test.expectedErr)}
80+
mockClient := Client{Requester: NewMockClient(&api.EmptyReply{}, test.expectedErr)}
8181
err := mockClient.StartCPUProfiler(context.Background())
8282
require.ErrorIs(t, err, test.expectedErr)
8383
})
@@ -87,7 +87,7 @@ func TestStartCPUProfiler(t *testing.T) {
8787
func TestStopCPUProfiler(t *testing.T) {
8888
for _, test := range SuccessResponseTests {
8989
t.Run(test.name, func(t *testing.T) {
90-
mockClient := client{requester: NewMockClient(&api.EmptyReply{}, test.expectedErr)}
90+
mockClient := Client{Requester: NewMockClient(&api.EmptyReply{}, test.expectedErr)}
9191
err := mockClient.StopCPUProfiler(context.Background())
9292
require.ErrorIs(t, err, test.expectedErr)
9393
})
@@ -97,7 +97,7 @@ func TestStopCPUProfiler(t *testing.T) {
9797
func TestMemoryProfile(t *testing.T) {
9898
for _, test := range SuccessResponseTests {
9999
t.Run(test.name, func(t *testing.T) {
100-
mockClient := client{requester: NewMockClient(&api.EmptyReply{}, test.expectedErr)}
100+
mockClient := Client{Requester: NewMockClient(&api.EmptyReply{}, test.expectedErr)}
101101
err := mockClient.MemoryProfile(context.Background())
102102
require.ErrorIs(t, err, test.expectedErr)
103103
})
@@ -107,7 +107,7 @@ func TestMemoryProfile(t *testing.T) {
107107
func TestLockProfile(t *testing.T) {
108108
for _, test := range SuccessResponseTests {
109109
t.Run(test.name, func(t *testing.T) {
110-
mockClient := client{requester: NewMockClient(&api.EmptyReply{}, test.expectedErr)}
110+
mockClient := Client{Requester: NewMockClient(&api.EmptyReply{}, test.expectedErr)}
111111
err := mockClient.LockProfile(context.Background())
112112
require.ErrorIs(t, err, test.expectedErr)
113113
})
@@ -117,7 +117,7 @@ func TestLockProfile(t *testing.T) {
117117
func TestAlias(t *testing.T) {
118118
for _, test := range SuccessResponseTests {
119119
t.Run(test.name, func(t *testing.T) {
120-
mockClient := client{requester: NewMockClient(&api.EmptyReply{}, test.expectedErr)}
120+
mockClient := Client{Requester: NewMockClient(&api.EmptyReply{}, test.expectedErr)}
121121
err := mockClient.Alias(context.Background(), "alias", "alias2")
122122
require.ErrorIs(t, err, test.expectedErr)
123123
})
@@ -127,7 +127,7 @@ func TestAlias(t *testing.T) {
127127
func TestAliasChain(t *testing.T) {
128128
for _, test := range SuccessResponseTests {
129129
t.Run(test.name, func(t *testing.T) {
130-
mockClient := client{requester: NewMockClient(&api.EmptyReply{}, test.expectedErr)}
130+
mockClient := Client{Requester: NewMockClient(&api.EmptyReply{}, test.expectedErr)}
131131
err := mockClient.AliasChain(context.Background(), "chain", "chain-alias")
132132
require.ErrorIs(t, err, test.expectedErr)
133133
})
@@ -139,7 +139,7 @@ func TestGetChainAliases(t *testing.T) {
139139
require := require.New(t)
140140

141141
expectedReply := []string{"alias1", "alias2"}
142-
mockClient := client{requester: NewMockClient(&GetChainAliasesReply{
142+
mockClient := Client{Requester: NewMockClient(&GetChainAliasesReply{
143143
Aliases: expectedReply,
144144
}, nil)}
145145

@@ -149,7 +149,7 @@ func TestGetChainAliases(t *testing.T) {
149149
})
150150

151151
t.Run("failure", func(t *testing.T) {
152-
mockClient := client{requester: NewMockClient(&GetChainAliasesReply{}, errTest)}
152+
mockClient := Client{Requester: NewMockClient(&GetChainAliasesReply{}, errTest)}
153153
_, err := mockClient.GetChainAliases(context.Background(), "chain")
154154
require.ErrorIs(t, err, errTest)
155155
})
@@ -158,7 +158,7 @@ func TestGetChainAliases(t *testing.T) {
158158
func TestStacktrace(t *testing.T) {
159159
for _, test := range SuccessResponseTests {
160160
t.Run(test.name, func(t *testing.T) {
161-
mockClient := client{requester: NewMockClient(&api.EmptyReply{}, test.expectedErr)}
161+
mockClient := Client{Requester: NewMockClient(&api.EmptyReply{}, test.expectedErr)}
162162
err := mockClient.Stacktrace(context.Background())
163163
require.ErrorIs(t, err, test.expectedErr)
164164
})
@@ -177,7 +177,7 @@ func TestReloadInstalledVMs(t *testing.T) {
177177
ids.GenerateTestID(): "oops",
178178
ids.GenerateTestID(): "uh-oh",
179179
}
180-
mockClient := client{requester: NewMockClient(&LoadVMsReply{
180+
mockClient := Client{Requester: NewMockClient(&LoadVMsReply{
181181
NewVMs: expectedNewVMs,
182182
FailedVMs: expectedFailedVMs,
183183
}, nil)}
@@ -189,7 +189,7 @@ func TestReloadInstalledVMs(t *testing.T) {
189189
})
190190

191191
t.Run("failure", func(t *testing.T) {
192-
mockClient := client{requester: NewMockClient(&LoadVMsReply{}, errTest)}
192+
mockClient := Client{Requester: NewMockClient(&LoadVMsReply{}, errTest)}
193193
_, _, err := mockClient.LoadVMs(context.Background())
194194
require.ErrorIs(t, err, errTest)
195195
})
@@ -244,8 +244,8 @@ func TestSetLoggerLevel(t *testing.T) {
244244
t.Run(tt.name, func(t *testing.T) {
245245
require := require.New(t)
246246

247-
c := client{
248-
requester: NewMockClient(
247+
c := Client{
248+
Requester: NewMockClient(
249249
&LoggerLevelReply{
250250
LoggerLevels: tt.serviceResponse,
251251
},
@@ -297,8 +297,8 @@ func TestGetLoggerLevel(t *testing.T) {
297297
t.Run(tt.name, func(t *testing.T) {
298298
require := require.New(t)
299299

300-
c := client{
301-
requester: NewMockClient(
300+
c := Client{
301+
Requester: NewMockClient(
302302
&LoggerLevelReply{
303303
LoggerLevels: tt.serviceResponse,
304304
},
@@ -344,8 +344,8 @@ func TestGetConfig(t *testing.T) {
344344
t.Run(tt.name, func(t *testing.T) {
345345
require := require.New(t)
346346

347-
c := client{
348-
requester: NewMockClient(tt.expectedResponse, tt.serviceErr),
347+
c := Client{
348+
Requester: NewMockClient(tt.expectedResponse, tt.serviceErr),
349349
}
350350
res, err := c.GetConfig(context.Background())
351351
require.ErrorIs(err, tt.clientErr)

api/admin/key_value_reader.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,10 @@ import (
1212
var _ database.KeyValueReader = (*KeyValueReader)(nil)
1313

1414
type KeyValueReader struct {
15-
client Client
15+
client *Client
1616
}
1717

18-
func NewKeyValueReader(client Client) *KeyValueReader {
18+
func NewKeyValueReader(client *Client) *KeyValueReader {
1919
return &KeyValueReader{
2020
client: client,
2121
}

api/health/client.go

Lines changed: 16 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -10,65 +10,52 @@ import (
1010
"github.com/ava-labs/avalanchego/utils/rpc"
1111
)
1212

13-
var _ Client = (*client)(nil)
14-
15-
// Client interface for Avalanche Health API Endpoint
16-
// For helpers to wait for Readiness, Health, or Liveness, see AwaitReady,
17-
// AwaitHealthy, and AwaitAlive.
18-
type Client interface {
19-
// Readiness returns if the node has finished initialization
20-
Readiness(ctx context.Context, tags []string, options ...rpc.Option) (*APIReply, error)
21-
// Health returns a summation of the health of the node
22-
Health(ctx context.Context, tags []string, options ...rpc.Option) (*APIReply, error)
23-
// Liveness returns if the node is in need of a restart
24-
Liveness(ctx context.Context, tags []string, options ...rpc.Option) (*APIReply, error)
25-
}
26-
27-
// Client implementation for Avalanche Health API Endpoint
28-
type client struct {
29-
requester rpc.EndpointRequester
13+
type Client struct {
14+
Requester rpc.EndpointRequester
3015
}
3116

32-
// NewClient returns a client to interact with Health API endpoint
33-
func NewClient(uri string) Client {
34-
return &client{requester: rpc.NewEndpointRequester(
17+
func NewClient(uri string) *Client {
18+
return &Client{Requester: rpc.NewEndpointRequester(
3519
uri + "/ext/health",
3620
)}
3721
}
3822

39-
func (c *client) Readiness(ctx context.Context, tags []string, options ...rpc.Option) (*APIReply, error) {
23+
// Readiness returns if the node has finished initialization
24+
func (c *Client) Readiness(ctx context.Context, tags []string, options ...rpc.Option) (*APIReply, error) {
4025
res := &APIReply{}
41-
err := c.requester.SendRequest(ctx, "health.readiness", &APIArgs{Tags: tags}, res, options...)
26+
err := c.Requester.SendRequest(ctx, "health.readiness", &APIArgs{Tags: tags}, res, options...)
4227
return res, err
4328
}
4429

45-
func (c *client) Health(ctx context.Context, tags []string, options ...rpc.Option) (*APIReply, error) {
30+
// Health returns a summation of the health of the node
31+
func (c *Client) Health(ctx context.Context, tags []string, options ...rpc.Option) (*APIReply, error) {
4632
res := &APIReply{}
47-
err := c.requester.SendRequest(ctx, "health.health", &APIArgs{Tags: tags}, res, options...)
33+
err := c.Requester.SendRequest(ctx, "health.health", &APIArgs{Tags: tags}, res, options...)
4834
return res, err
4935
}
5036

51-
func (c *client) Liveness(ctx context.Context, tags []string, options ...rpc.Option) (*APIReply, error) {
37+
// Liveness returns if the node is in need of a restart
38+
func (c *Client) Liveness(ctx context.Context, tags []string, options ...rpc.Option) (*APIReply, error) {
5239
res := &APIReply{}
53-
err := c.requester.SendRequest(ctx, "health.liveness", &APIArgs{Tags: tags}, res, options...)
40+
err := c.Requester.SendRequest(ctx, "health.liveness", &APIArgs{Tags: tags}, res, options...)
5441
return res, err
5542
}
5643

5744
// AwaitReady polls the node every [freq] until the node reports ready.
5845
// Only returns an error if [ctx] returns an error.
59-
func AwaitReady(ctx context.Context, c Client, freq time.Duration, tags []string, options ...rpc.Option) (bool, error) {
46+
func AwaitReady(ctx context.Context, c *Client, freq time.Duration, tags []string, options ...rpc.Option) (bool, error) {
6047
return await(ctx, freq, c.Readiness, tags, options...)
6148
}
6249

6350
// AwaitHealthy polls the node every [freq] until the node reports healthy.
6451
// Only returns an error if [ctx] returns an error.
65-
func AwaitHealthy(ctx context.Context, c Client, freq time.Duration, tags []string, options ...rpc.Option) (bool, error) {
52+
func AwaitHealthy(ctx context.Context, c *Client, freq time.Duration, tags []string, options ...rpc.Option) (bool, error) {
6653
return await(ctx, freq, c.Health, tags, options...)
6754
}
6855

6956
// AwaitAlive polls the node every [freq] until the node reports liveness.
7057
// Only returns an error if [ctx] returns an error.
71-
func AwaitAlive(ctx context.Context, c Client, freq time.Duration, tags []string, options ...rpc.Option) (bool, error) {
58+
func AwaitAlive(ctx context.Context, c *Client, freq time.Duration, tags []string, options ...rpc.Option) (bool, error) {
7259
return await(ctx, freq, c.Liveness, tags, options...)
7360
}
7461

api/health/client_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,8 @@ func TestClient(t *testing.T) {
4343
err: nil,
4444
onCall: func() {},
4545
}
46-
c := &client{
47-
requester: mc,
46+
c := &Client{
47+
Requester: mc,
4848
}
4949

5050
{

0 commit comments

Comments
 (0)