Skip to content

Commit 408e904

Browse files
Merge pull request #120 from github/config-http-timeout
Allow override of Vitess HTTP timeout
2 parents 435c726 + 1e048ac commit 408e904

File tree

4 files changed

+46
-13
lines changed

4 files changed

+46
-13
lines changed

pkg/config/vitess_config.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,10 @@ package config
55
//
66

77
type VitessConfigurationSettings struct {
8-
API string
9-
Keyspace string
10-
Shard string
8+
API string
9+
Keyspace string
10+
Shard string
11+
TimeoutSecs uint
1112
}
1213

1314
func (settings *VitessConfigurationSettings) IsEmpty() bool {

pkg/throttle/throttler.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -323,7 +323,7 @@ func (throttler *Throttler) refreshMySQLInventory() error {
323323
log.Debugf("getting vitess data from %s", clusterSettings.VitessSettings.API)
324324
keyspace := clusterSettings.VitessSettings.Keyspace
325325
shard := clusterSettings.VitessSettings.Shard
326-
tablets, err := vitess.ParseTablets(clusterSettings.VitessSettings.API, keyspace, shard)
326+
tablets, err := vitess.ParseTablets(clusterSettings.VitessSettings)
327327
if err != nil {
328328
return log.Errorf("Unable to get vitess hosts from %s, %s/%s: %+v", clusterSettings.VitessSettings.API, keyspace, shard, err)
329329
}

pkg/vitess/api_client.go

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

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

15+
const defaultTimeout = time.Duration(5) * time.Second
16+
1417
// Tablet represents information about a running instance of vttablet.
1518
type Tablet struct {
1619
MysqlHostname string `json:"mysql_hostname,omitempty"`
@@ -24,15 +27,15 @@ func (t Tablet) IsValidReplica() bool {
2427
}
2528

2629
var httpClient = http.Client{
27-
Timeout: 1 * time.Second,
30+
Timeout: defaultTimeout,
2831
}
2932

30-
func constructAPIURL(api string, keyspace string, shard string) (url string) {
31-
api = strings.TrimRight(api, "/")
33+
func constructAPIURL(settings config.VitessConfigurationSettings) (url string) {
34+
api := strings.TrimRight(settings.API, "/")
3235
if !strings.HasSuffix(api, "/api") {
3336
api = fmt.Sprintf("%s/api", api)
3437
}
35-
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)
3639

3740
return url
3841
}
@@ -49,8 +52,14 @@ func filterReplicaTablets(tablets []Tablet) (replicas []Tablet) {
4952

5053
// ParseTablets reads from vitess /api/ks_tablets/<keyspace>/[shard] and returns a
5154
// listing (mysql_hostname, mysql_port, type) of REPLICA tablets
52-
func ParseTablets(api string, keyspace string, shard string) (tablets []Tablet, err error) {
53-
url := constructAPIURL(api, keyspace, shard)
55+
func ParseTablets(settings config.VitessConfigurationSettings) (tablets []Tablet, err error) {
56+
if settings.TimeoutSecs == 0 {
57+
httpClient.Timeout = defaultTimeout
58+
} else {
59+
httpClient.Timeout = time.Duration(settings.TimeoutSecs) * time.Second
60+
}
61+
62+
url := constructAPIURL(settings)
5463
resp, err := httpClient.Get(url)
5564
if err != nil {
5665
return tablets, err

pkg/vitess/api_client_test.go

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,9 @@ import (
66
"net/http"
77
"net/http/httptest"
88
"testing"
9+
"time"
910

11+
"github.com/github/freno/pkg/config"
1012
"vitess.io/vitess/go/vt/proto/topodata"
1113
)
1214

@@ -50,7 +52,12 @@ func TestParseTablets(t *testing.T) {
5052
defer vitessApi.Close()
5153

5254
t.Run("success", func(t *testing.T) {
53-
tablets, err := ParseTablets(vitessApi.URL, "test", "00")
55+
tablets, err := ParseTablets(config.VitessConfigurationSettings{
56+
API: vitessApi.URL,
57+
Keyspace: "test",
58+
Shard: "00",
59+
TimeoutSecs: 1,
60+
})
5461
if err != nil {
5562
t.Fatalf("Expected no error, got %q", err)
5663
}
@@ -62,22 +69,38 @@ func TestParseTablets(t *testing.T) {
6269
if tablets[0].MysqlHostname != "replica" {
6370
t.Fatalf("Expected hostname %q, got %q", "replica", tablets[0].MysqlHostname)
6471
}
72+
73+
if httpClient.Timeout != time.Second {
74+
t.Fatalf("Expected vitess client timeout of %v, got %v", time.Second, httpClient.Timeout)
75+
}
6576
})
6677

6778
t.Run("not-found", func(t *testing.T) {
68-
tablets, err := ParseTablets(vitessApi.URL, "not-found", "00")
79+
tablets, err := ParseTablets(config.VitessConfigurationSettings{
80+
API: vitessApi.URL,
81+
Keyspace: "not-found",
82+
Shard: "40-80",
83+
})
6984
if err != nil {
7085
t.Fatalf("Expected no error, got %q", err)
7186
}
7287

7388
if len(tablets) > 0 {
7489
t.Fatalf("Expected 0 tablets, got %d", len(tablets))
7590
}
91+
92+
if httpClient.Timeout != defaultTimeout {
93+
t.Fatalf("Expected vitess client timeout of %v, got %v", defaultTimeout, httpClient.Timeout)
94+
}
7695
})
7796

7897
t.Run("failed", func(t *testing.T) {
7998
vitessApi.Close() // kill the mock vitess API
80-
_, err := ParseTablets(vitessApi.URL, "fail", "00")
99+
_, err := ParseTablets(config.VitessConfigurationSettings{
100+
API: vitessApi.URL,
101+
Keyspace: "fail",
102+
Shard: "00",
103+
})
81104
if err == nil {
82105
t.Fatal("Expected error, got nil")
83106
}

0 commit comments

Comments
 (0)