Skip to content

Commit

Permalink
Fix infinite recursion in client discovery
Browse files Browse the repository at this point in the history
  • Loading branch information
aalda committed Mar 15, 2019
1 parent 3f2cfbe commit b1dc866
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 26 deletions.
6 changes: 3 additions & 3 deletions api/apihttp/apihttp_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -295,7 +295,7 @@ func TestIncremental(t *testing.T) {

func TestAuthHandlerMiddleware(t *testing.T) {

req, err := http.NewRequest("GET", "/health-check", nil)
req, err := http.NewRequest("HEAD", "/healthcheck", nil)
if err != nil {
t.Fatal(err)
}
Expand All @@ -312,9 +312,9 @@ func TestAuthHandlerMiddleware(t *testing.T) {
handler.ServeHTTP(rr, req)

// Check the status code is what we expect.
if status := rr.Code; status != http.StatusOK {
if status := rr.Code; status != http.StatusNoContent {
t.Errorf("handler returned wrong status code: got %v want %v",
status, http.StatusOK)
status, http.StatusNoContent)
}
}

Expand Down
49 changes: 27 additions & 22 deletions client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -335,7 +335,6 @@ func (c *HTTPClient) healthCheck(timeout time.Duration) {
// the goroutines execute the health-check HTTP request and sets status
go func(endpointURL string) {

fmt.Printf("start healthcheck goroutine: %s\n", endpoint.URL())
// Run a GET request against QED with a timeout
// TODO it should be a HEAD instead of a GET
ctx, cancel := context.WithTimeout(context.Background(), timeout)
Expand Down Expand Up @@ -379,37 +378,43 @@ func (c *HTTPClient) discover() error {
return nil
}

body, err := c.callAny("GET", "/info/shards", nil)
info := make(map[string]interface{})
err = json.Unmarshal(body, &info)
if err != nil {
return err
}

clusterMeta := info["meta"].(map[string]interface{})
primaryID := info["leaderID"].(string)
scheme := info["URIScheme"].(string)
for _, e := range c.topology.Endpoints() {
body, err := c.doReq("GET", e, "/info/shards", nil)
if err == nil {
info := make(map[string]interface{})
err = json.Unmarshal(body, &info)
if err != nil {
return err
}

var prim string
secondaries := make([]string, 0)
for id, nodeMeta := range clusterMeta {
for k, address := range nodeMeta.(map[string]interface{}) {
if k == "HTTPAddr" {
if id == primaryID {
prim = scheme + address.(string)
} else {
secondaries = append(secondaries, scheme+address.(string))
clusterMeta := info["meta"].(map[string]interface{})
primaryID := info["leaderID"].(string)
scheme := info["URIScheme"].(string)

var prim string
secondaries := make([]string, 0)
for id, nodeMeta := range clusterMeta {
for k, address := range nodeMeta.(map[string]interface{}) {
if k == "HTTPAddr" {
if id == primaryID {
prim = scheme + address.(string)
} else {
secondaries = append(secondaries, scheme+address.(string))
}
}
}
}
c.topology.Update(prim, secondaries...)
break
}
}
c.topology.Update(prim, secondaries...)

return nil
}

// Ping will do a healthcheck request to the primary node
func (c *HTTPClient) Ping() error {
_, err := c.callPrimary("GET", "/health-check", nil)
_, err := c.callPrimary("GET", "/healthcheck", nil)
if err != nil {
return err
}
Expand Down
7 changes: 7 additions & 0 deletions client/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,13 @@ func SetURLs(primary string, secondaries ...string) HTTPClientOptionF {
}
}

func SetAttemptToReviveEndpoints(value bool) HTTPClientOptionF {
return func(c *HTTPClient) error {
c.topology.attemptToRevive = value
return nil
}
}

func SetRequestRetrier(retrier RequestRetrier) HTTPClientOptionF {
return func(c *HTTPClient) error {
if retrier != nil {
Expand Down
3 changes: 2 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module github.com/bbva/qed

go 1.12
go 1.12.1

require (
github.com/BurntSushi/toml v0.3.1 // indirect
Expand All @@ -17,6 +17,7 @@ require (
github.com/imdario/mergo v0.3.7
github.com/inconshreveable/mousetrap v1.0.0 // indirect
github.com/mitchellh/go-homedir v1.1.0
github.com/pkg/errors v0.8.0
github.com/prometheus/client_golang v0.9.2
github.com/spf13/cobra v0.0.3
github.com/spf13/viper v1.3.1
Expand Down

0 comments on commit b1dc866

Please sign in to comment.