Skip to content

Commit

Permalink
[node] Use URL builder to get reachability URL (#575)
Browse files Browse the repository at this point in the history
  • Loading branch information
ian-shim authored May 22, 2024
1 parent 1f91e4a commit fa5866c
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 6 deletions.
28 changes: 22 additions & 6 deletions node/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import (
"net/http"
"net/url"
"os"
"strings"
"sync"
"time"

Expand Down Expand Up @@ -478,9 +477,9 @@ func (n *Node) checkNodeReachability() {
return
}

checkUrl, err := url.Parse(fmt.Sprintf("%s/api/v1/operators-info/port-check?operator_id=%s", strings.TrimSuffix(n.Config.DataApiUrl, "/"), n.Config.ID.Hex()))
checkURL, err := GetReachabilityURL(n.Config.DataApiUrl, n.Config.ID.Hex())
if err != nil {
n.Logger.Error("Reachability check failed - invalid check url", err, "checkUrl", checkUrl.String())
n.Logger.Error("Failed to get reachability check URL", err)
return
}

Expand All @@ -491,16 +490,16 @@ func (n *Node) checkNodeReachability() {
for {
<-ticker.C

n.Logger.Debug("Calling reachability check", "url", checkUrl.String())
n.Logger.Debug("Calling reachability check", "url", checkURL)

resp, err := http.Get(checkUrl.String())
resp, err := http.Get(checkURL)
if err != nil {
n.Logger.Error("Reachability check request failed", err)
continue
} else if resp.StatusCode == 404 {
body, _ := io.ReadAll(resp.Body)
if string(body) == "404 page not found" {
n.Logger.Error("Invalid reachability check url", "checkUrl", checkUrl.String())
n.Logger.Error("Invalid reachability check url", "checkUrl", checkURL)
} else {
n.Logger.Warn("Reachability check operator id not found", "status", resp.StatusCode, "operator_id", n.Config.ID.Hex())
}
Expand Down Expand Up @@ -539,3 +538,20 @@ func (n *Node) checkNodeReachability() {
}
}
}

func GetReachabilityURL(dataApiUrl, operatorID string) (string, error) {
checkURLString, err := url.JoinPath(dataApiUrl, "/api/v1/operators-info/port-check")
if err != nil {
return "", err
}
checkURL, err := url.Parse(checkURLString)
if err != nil {
return "", err
}

q := checkURL.Query()
q.Set("operator_id", operatorID)
checkURL.RawQuery = q.Encode()

return checkURL.String(), nil
}
9 changes: 9 additions & 0 deletions node/node_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -139,3 +139,12 @@ func TestNodeStartOperatorIDDoesNotMatch(t *testing.T) {
err := c.node.Start(context.Background())
assert.ErrorContains(t, err, "operator ID mismatch")
}

func TestGetReachabilityURL(t *testing.T) {
url, err := node.GetReachabilityURL("https://dataapi.eigenda.xyz/", "123123123")
assert.NoError(t, err)
assert.Equal(t, "https://dataapi.eigenda.xyz/api/v1/operators-info/port-check?operator_id=123123123", url)
url, err = node.GetReachabilityURL("https://dataapi.eigenda.xyz", "123123123")
assert.NoError(t, err)
assert.Equal(t, "https://dataapi.eigenda.xyz/api/v1/operators-info/port-check?operator_id=123123123", url)
}

0 comments on commit fa5866c

Please sign in to comment.