Skip to content

Commit eed8c78

Browse files
authored
fix: rest probe endpoint (#39)
Fix health probe endpoints. <!-- This is an auto-generated comment: release notes by coderabbit.ai --> ## Summary by CodeRabbit * **Bug Fixes** * Fixed a potential crash in load balancing when no servers are available. * Updated API endpoints to use standardized blockchain protocol paths for improved compatibility. * Simplified error reporting for status validation. * **Chores** * Updated dependency management for monitoring library. <!-- end of auto-generated comment: release notes by coderabbit.ai -->
1 parent 486052c commit eed8c78

File tree

4 files changed

+20
-6
lines changed

4 files changed

+20
-6
lines changed

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ require (
66
github.com/cometbft/cometbft v0.38.12
77
github.com/cosmos/cosmos-sdk v0.50.13
88
github.com/prometheus/client_golang v1.22.0
9+
github.com/prometheus/client_model v0.6.1
910
github.com/spf13/cobra v1.9.1
1011
github.com/spf13/viper v1.19.0
1112
github.com/stretchr/testify v1.10.0
@@ -112,7 +113,6 @@ require (
112113
github.com/petermattis/goid v0.0.0-20240813172612-4fcff4a6cae7 // indirect
113114
github.com/pkg/errors v0.9.1 // indirect
114115
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 // indirect
115-
github.com/prometheus/client_model v0.6.1 // indirect
116116
github.com/prometheus/common v0.62.0 // indirect
117117
github.com/prometheus/procfs v0.15.1 // indirect
118118
github.com/rcrowley/go-metrics v0.0.0-20201227073835-cf1acfcdf475 // indirect

internal/proxy/balancer.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,10 @@ func (rr *LatencyBased) NextServer(_ *http.Request) *Server {
128128
}
129129

130130
// Fallback, shouldn't be reached if rates are normalized
131-
return rr.servers[len(rr.servers)-1].Server
131+
if len(rr.servers) > 0 {
132+
return rr.servers[len(rr.servers)-1].Server
133+
}
134+
return nil
132135
}
133136

134137
// Update updates the list of available servers and their corresponding rates

internal/proxy/balancer_test.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,17 @@ func TestRoundRobin_NextServer(t *testing.T) {
7979
}
8080
}
8181

82+
func TestLatencyBased_NextServer_EmptyServers(t *testing.T) {
83+
lb := NewLatencyBased(slog.New(slog.NewTextHandler(os.Stdout, nil)))
84+
lb.Update([]*Server{})
85+
86+
// Should return nil when no servers are available
87+
server := lb.NextServer(nil)
88+
if server != nil {
89+
t.Errorf("expected nil server when no servers available, got %v", server)
90+
}
91+
}
92+
8293
func TestLatencyBased_NextServer(t *testing.T) {
8394
servers := []*Server{
8495
{

internal/seed/probes.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ func RESTProbe(ctx context.Context, node Node) (Status, error) {
118118
start := time.Now()
119119
client := &http.Client{}
120120

121-
req, err := http.NewRequest("GET", fmt.Sprintf("%s/syncing", node.Address), nil)
121+
req, err := http.NewRequest("GET", fmt.Sprintf("%s/cosmos/base/tendermint/v1beta1/syncing", node.Address), nil)
122122
if err != nil {
123123
return Status{}, fmt.Errorf("creating REST client request: %w", err)
124124
}
@@ -130,7 +130,7 @@ func RESTProbe(ctx context.Context, node Node) (Status, error) {
130130
defer resp.Body.Close()
131131

132132
if resp.StatusCode != 200 {
133-
return Status{}, fmt.Errorf("unexpected status from REST client [%d %s]: %w", resp.StatusCode, resp.Status, err)
133+
return Status{}, fmt.Errorf("unexpected status from REST client [%d %s]", resp.StatusCode, resp.Status)
134134
}
135135

136136
body, err := io.ReadAll(resp.Body)
@@ -143,7 +143,7 @@ func RESTProbe(ctx context.Context, node Node) (Status, error) {
143143
return Status{}, fmt.Errorf("unmarshaling body from REST client response: %w", err)
144144
}
145145

146-
latestBlockReq, err := http.NewRequest("GET", fmt.Sprintf("%s/blocks/latest", node.Address), nil)
146+
latestBlockReq, err := http.NewRequest("GET", fmt.Sprintf("%s/cosmos/base/tendermint/v1beta1/blocks/latest", node.Address), nil)
147147
if err != nil {
148148
return Status{}, fmt.Errorf("creating REST client request: %w", err)
149149
}
@@ -155,7 +155,7 @@ func RESTProbe(ctx context.Context, node Node) (Status, error) {
155155
defer latestBlockResp.Body.Close()
156156

157157
if latestBlockResp.StatusCode != 200 {
158-
return Status{}, fmt.Errorf("unexpected status from REST client [%d %s]: %w", latestBlockResp.StatusCode, latestBlockResp.Status, err)
158+
return Status{}, fmt.Errorf("unexpected status from REST client [%d %s]", latestBlockResp.StatusCode, latestBlockResp.Status)
159159
}
160160

161161
latestBlockBody, err := io.ReadAll(latestBlockResp.Body)

0 commit comments

Comments
 (0)