Skip to content

Commit 5079089

Browse files
Simplify change
1 parent 5dc5744 commit 5079089

File tree

3 files changed

+38
-34
lines changed

3 files changed

+38
-34
lines changed

pkg/throttle/throttler.go

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,6 @@ type Throttler struct {
7272

7373
nonLowPriorityAppRequestsThrottled *cache.Cache
7474
httpClient *http.Client
75-
vitessClient *vitess.Client
7675
}
7776

7877
func NewThrottler() *Throttler {
@@ -321,13 +320,10 @@ func (throttler *Throttler) refreshMySQLInventory() error {
321320
}
322321

323322
if !clusterSettings.VitessSettings.IsEmpty() {
324-
if throttler.vitessClient == nil {
325-
throttler.vitessClient = vitess.New()
326-
}
327323
log.Debugf("getting vitess data from %s", clusterSettings.VitessSettings.API)
328324
keyspace := clusterSettings.VitessSettings.Keyspace
329325
shard := clusterSettings.VitessSettings.Shard
330-
tablets, err := throttler.vitessClient.ParseTablets(clusterSettings.VitessSettings.API, keyspace, shard, clusterSettings.VitessSettings.TimeoutSecs)
326+
tablets, err := vitess.ParseTablets(clusterSettings.VitessSettings)
331327
if err != nil {
332328
return log.Errorf("Unable to get vitess hosts from %s, %s/%s: %+v", clusterSettings.VitessSettings.API, keyspace, shard, err)
333329
}

pkg/vitess/api_client.go

Lines changed: 14 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import (
88
"strings"
99
"time"
1010

11-
"github.com/github/freno/pkg/base"
11+
"github.com/github/freno/pkg/config"
1212
"vitess.io/vitess/go/vt/proto/topodata"
1313
)
1414

@@ -26,12 +26,16 @@ func (t Tablet) IsValidReplica() bool {
2626
return t.Type == topodata.TabletType_REPLICA
2727
}
2828

29-
func constructAPIURL(api string, keyspace string, shard string) (url string) {
30-
api = strings.TrimRight(api, "/")
29+
var httpClient = http.Client{
30+
Timeout: defaultTimeout,
31+
}
32+
33+
func constructAPIURL(settings config.VitessConfigurationSettings) (url string) {
34+
api := strings.TrimRight(settings.API, "/")
3135
if !strings.HasSuffix(api, "/api") {
3236
api = fmt.Sprintf("%s/api", api)
3337
}
34-
url = fmt.Sprintf("%s/keyspace/%s/tablets/%s", api, keyspace, shard)
38+
url = fmt.Sprintf("%s/keyspace/%s/tablets/%s", api, settings.Keyspace, settings.Shard)
3539

3640
return url
3741
}
@@ -46,27 +50,17 @@ func filterReplicaTablets(tablets []Tablet) (replicas []Tablet) {
4650
return replicas
4751
}
4852

49-
// Client gathers info from the Vitess API
50-
type Client struct {
51-
client *http.Client
52-
}
53-
54-
// New returns a new Client
55-
func New() *Client {
56-
return &Client{client: base.SetupHttpClient(0)}
57-
}
58-
5953
// ParseTablets reads from vitess /api/ks_tablets/<keyspace>/[shard] and returns a
6054
// listing (mysql_hostname, mysql_port, type) of REPLICA tablets
61-
func (c *Client) ParseTablets(api, keyspace, shard string, timeoutSecs float64) (tablets []Tablet, err error) {
62-
if timeoutSecs == 0 {
63-
c.client.Timeout = defaultTimeout
55+
func ParseTablets(settings config.VitessConfigurationSettings) (tablets []Tablet, err error) {
56+
if settings.TimeoutSecs == 0 {
57+
httpClient.Timeout = defaultTimeout
6458
} else {
65-
c.client.Timeout = time.Duration(timeoutSecs) * time.Second
59+
httpClient.Timeout = time.Duration(settings.TimeoutSecs) * time.Second
6660
}
6761

68-
url := constructAPIURL(api, keyspace, shard)
69-
resp, err := c.client.Get(url)
62+
url := constructAPIURL(settings)
63+
resp, err := httpClient.Get(url)
7064
if err != nil {
7165
return tablets, err
7266
}

pkg/vitess/api_client_test.go

Lines changed: 23 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"testing"
99
"time"
1010

11+
"github.com/github/freno/pkg/config"
1112
"vitess.io/vitess/go/vt/proto/topodata"
1213
)
1314

@@ -50,10 +51,13 @@ func TestParseTablets(t *testing.T) {
5051
}))
5152
defer vitessApi.Close()
5253

53-
vtClient := New()
54-
5554
t.Run("success", func(t *testing.T) {
56-
tablets, err := vtClient.ParseTablets(vitessApi.URL, "test", "00", 1)
55+
tablets, err := ParseTablets(config.VitessConfigurationSettings{
56+
API: vitessApi.URL,
57+
Keyspace: "test",
58+
Shard: "00",
59+
TimeoutSecs: 1.0,
60+
})
5761
if err != nil {
5862
t.Fatalf("Expected no error, got %q", err)
5963
}
@@ -66,13 +70,18 @@ func TestParseTablets(t *testing.T) {
6670
t.Fatalf("Expected hostname %q, got %q", "replica", tablets[0].MysqlHostname)
6771
}
6872

69-
if vtClient.client.Timeout != time.Second {
70-
t.Fatalf("Expected vitess client timeout of %v, got %v", time.Second, vtClient.client.Timeout)
73+
if httpClient.Timeout != time.Second {
74+
t.Fatalf("Expected vitess client timeout of %v, got %v", time.Second, httpClient.Timeout)
7175
}
7276
})
7377

7478
t.Run("not-found", func(t *testing.T) {
75-
tablets, err := vtClient.ParseTablets(vitessApi.URL, "not-found", "00", 0)
79+
tablets, err := ParseTablets(config.VitessConfigurationSettings{
80+
API: vitessApi.URL,
81+
Keyspace: "not-found",
82+
Shard: "40-80",
83+
TimeoutSecs: 0.0,
84+
})
7685
if err != nil {
7786
t.Fatalf("Expected no error, got %q", err)
7887
}
@@ -81,14 +90,19 @@ func TestParseTablets(t *testing.T) {
8190
t.Fatalf("Expected 0 tablets, got %d", len(tablets))
8291
}
8392

84-
if vtClient.client.Timeout != defaultTimeout {
85-
t.Fatalf("Expected vitess client timeout of %v, got %v", defaultTimeout, vtClient.client.Timeout)
93+
if httpClient.Timeout != defaultTimeout {
94+
t.Fatalf("Expected vitess client timeout of %v, got %v", defaultTimeout, httpClient.Timeout)
8695
}
8796
})
8897

8998
t.Run("failed", func(t *testing.T) {
9099
vitessApi.Close() // kill the mock vitess API
91-
_, err := vtClient.ParseTablets(vitessApi.URL, "fail", "00", 0)
100+
_, err := ParseTablets(config.VitessConfigurationSettings{
101+
API: vitessApi.URL,
102+
Keyspace: "fail",
103+
Shard: "00",
104+
TimeoutSecs: 0.0,
105+
})
92106
if err == nil {
93107
t.Fatal("Expected error, got nil")
94108
}

0 commit comments

Comments
 (0)