Skip to content

Commit

Permalink
Refactor HTTPClient constructor parameters
Browse files Browse the repository at this point in the history
  • Loading branch information
Jose Luis Lucas authored and iknite committed Feb 21, 2019
1 parent 78868e6 commit 0b41e5b
Show file tree
Hide file tree
Showing 7 changed files with 32 additions and 45 deletions.
33 changes: 12 additions & 21 deletions client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import (

"github.com/bbva/qed/balloon"
"github.com/bbva/qed/hashing"
"github.com/bbva/qed/log"
"github.com/bbva/qed/protocol"
)

Expand Down Expand Up @@ -65,11 +66,14 @@ func NewHTTPClient(conf Config) *HTTPClient {
},
}

if len(conf.Cluster.Endpoints) == 1 {
conf.Cluster.Leader = conf.Cluster.Endpoints[0]
} else {
client.updateClusterLeader()
conf.ClusterLeader = conf.Endpoints[0]

info, err := client.getClusterInfo()
if err != nil {
log.Errorf("Failed to get raft cluster info: %v", err)
return nil
}
client.updateConf(info)

return client
}
Expand All @@ -93,23 +97,10 @@ func (c *HTTPClient) exponentialBackoff(req *http.Request) (*http.Response, erro
}
}

func (c *HTTPClient) updateClusterLeader() {
info, _ := c.getClusterInfo()
if isLeader, ok := info["isLeader"]; ok {
if isLeader.(bool) {
c.conf.Cluster.Leader = info["httpEndpoint"].(string)
} else {
time.Sleep(100 * time.Millisecond)
c.conf.Cluster.Leader = c.conf.Cluster.Endpoints[rand.Int()%len(c.conf.Cluster.Endpoints)]
c.updateClusterLeader()
}
}
}

func (c HTTPClient) getClusterInfo() (map[string]interface{}, error) {
info := make(map[string]interface{})

req, err := http.NewRequest("GET", c.conf.Cluster.Leader+"/info/shards", bytes.NewBuffer([]byte{}))
req, err := http.NewRequest("GET", c.conf.ClusterLeader+"/info/shards", bytes.NewBuffer([]byte{}))
if err != nil {
return info, err
}
Expand Down Expand Up @@ -149,7 +140,7 @@ func (c *HTTPClient) updateConf(info map[string]interface{}) {
leaderAddr = scheme + addr.(string)
}
}
c.conf.Cluster.Leader = leaderAddr
c.conf.ClusterLeader = leaderAddr

for _, nodeMeta := range clusterMeta {
for k, address := range nodeMeta.(map[string]interface{}) {
Expand All @@ -159,11 +150,11 @@ func (c *HTTPClient) updateConf(info map[string]interface{}) {
}
}
}
c.conf.Cluster.Endpoints = endpoints
c.conf.Endpoints = endpoints
}

func (c HTTPClient) doReq(method, path string, data []byte) ([]byte, error) {
url, err := url.Parse(c.conf.Cluster.Leader + path)
url, err := url.Parse(c.conf.ClusterLeader + path)
if err != nil {
panic(err)
}
Expand Down
6 changes: 3 additions & 3 deletions client/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,9 @@ func setup() func() {
mux.HandleFunc("/info/shards", infoHandler(server.URL))

client = NewHTTPClient(Config{
Cluster: QEDCluster{Endpoints: []string{server.URL}, Leader: server.URL},
APIKey: "my-awesome-api-key",
Insecure: false,
Endpoints: []string{server.URL},
APIKey: "my-awesome-api-key",
Insecure: false,
})
return func() {
server.Close()
Expand Down
15 changes: 7 additions & 8 deletions client/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,12 @@
package client

type Config struct {
// Cluster [host:port,host:port,...] to consult.
Cluster QEDCluster
// Endpoint [host:port] to operate.
// Must be the QED cluster leader.
ClusterLeader string

// Endpoints [host:port,host:port,...] to ask for QED cluster-topology.
Endpoints []string

// ApiKey to query the server endpoint.
APIKey string
Expand All @@ -36,14 +40,9 @@ type Config struct {
HandshakeTimeoutSeconds int
}

type QEDCluster struct {
Endpoints []string
Leader string
}

func DefaultConfig() *Config {
return &Config{
Cluster: QEDCluster{[]string{"127.0.0.1:8800"}, ""},
Endpoints: []string{"127.0.0.1:8800"},
APIKey: "my-key",
Insecure: true,
TimeoutSeconds: 10,
Expand Down
4 changes: 2 additions & 2 deletions cmd/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ func newClientCommand(ctx *cmdContext) *cobra.Command {
}

f := cmd.PersistentFlags()
f.StringSliceVarP(&clientCtx.config.Cluster, "endpoints", "e", []string{"127.0.0.1:8800"}, "Endpoint for REST requests on (host:port)")
f.StringSliceVarP(&clientCtx.config.Endpoints, "endpoints", "e", []string{"127.0.0.1:8800"}, "Endpoint for REST requests on (host:port)")
f.BoolVar(&clientCtx.config.Insecure, "insecure", false, "Allow self signed certificates")
f.IntVar(&clientCtx.config.TimeoutSeconds, "timeout-seconds", 10, "Seconds to cut the connection")
f.IntVar(&clientCtx.config.DialTimeoutSeconds, "dial-timeout-seconds", 5, "Seconds to cut the dialing")
Expand All @@ -52,7 +52,7 @@ func newClientCommand(ctx *cmdContext) *cobra.Command {
log.SetLogger("QEDClient", ctx.logLevel)

clientCtx.config.APIKey = ctx.apiKey
clientCtx.config.Cluster = v.GetStringSlice("client.endpoints")
clientCtx.config.Endpoints = v.GetStringSlice("client.endpoints")
clientCtx.config.Insecure = v.GetBool("client.insecure")
clientCtx.config.TimeoutSeconds = v.GetInt("client.timeout.connection")
clientCtx.config.DialTimeoutSeconds = v.GetInt("client.timeout.dial")
Expand Down
4 changes: 2 additions & 2 deletions gossip/auditor/auditor.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,8 @@ func NewAuditor(conf Config) (*Auditor, error) {
metrics.Qed_auditor_instances_count.Inc()
auditor := Auditor{
qed: client.NewHTTPClient(client.Config{
Cluster: client.QEDCluster{Endpoints: conf.QEDUrls, Leader: conf.QEDUrls[0]},
APIKey: conf.APIKey,
Endpoints: conf.QEDUrls,
APIKey: conf.APIKey,
Insecure: false,
}),
conf: conf,
Expand Down
6 changes: 3 additions & 3 deletions gossip/monitor/monitor.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,9 +70,9 @@ func NewMonitor(conf Config) (*Monitor, error) {

monitor := Monitor{
client: client.NewHTTPClient(client.Config{
Cluster: client.QEDCluster{Endpoints: conf.QEDUrls, Leader: conf.QEDUrls[0]},
APIKey: conf.APIKey,
Insecure: false,
Endpoints: conf.QedUrls,
APIKey: conf.APIKey,
Insecure: false,
}),
conf: conf,
taskCh: make(chan QueryTask, 100),
Expand Down
9 changes: 3 additions & 6 deletions tests/e2e/setup.go
Original file line number Diff line number Diff line change
Expand Up @@ -259,11 +259,8 @@ func endPoint(id int) string {

func getClient(id int) *client.HTTPClient {
return client.NewHTTPClient(client.Config{
Cluster: client.QEDCluster{
Endpoints: []string{endPoint(id)},
Leader: endPoint(id),
},
APIKey: APIKey,
Insecure: false,
Endpoints: []string{endPoint(id)},
APIKey: APIKey,
Insecure: false,
})
}

0 comments on commit 0b41e5b

Please sign in to comment.