Skip to content

Commit d95cc97

Browse files
Move http timeout to VitessConfigurationSettings
1 parent 917ddb5 commit d95cc97

File tree

5 files changed

+29
-28
lines changed

5 files changed

+29
-28
lines changed

conf/freno.conf.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,5 @@
22
"ListenPort": 8088,
33
"RaftBind": "127.0.0.1:10008",
44
"RaftDataDir": "/tmp",
5-
"RaftNodes": [],
6-
"VitessAPITimeoutSecs": 5
5+
"RaftNodes": []
76
}

pkg/config/config.go

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -108,23 +108,21 @@ type ConfigurationSettings struct {
108108
BackendMySQLPassword string
109109
MemcacheServers []string // if given, freno will report to aggregated values to given memcache
110110
MemcachePath string // use as prefix to metric path in memcache key, e.g. if `MemcachePath` is "myprefix" the key would be "myprefix/mysql/maincluster". Default: "freno"
111-
VitessAPITimeoutSecs int // timeout for Vitess vtctld HTTP API(s)
112111
Stores StoresSettings
113112
}
114113

115114
func newConfigurationSettings() *ConfigurationSettings {
116115
return &ConfigurationSettings{
117-
ListenPort: 8087,
118-
RaftBind: "127.0.0.1:10008",
119-
RaftDataDir: "",
120-
DefaultRaftPort: 0,
121-
RaftNodes: []string{},
122-
BackendMySQLHost: "",
123-
BackendMySQLSchema: "",
124-
BackendMySQLPort: 3306,
125-
MemcacheServers: []string{},
126-
MemcachePath: "freno",
127-
VitessAPITimeoutSecs: 5,
116+
ListenPort: 8087,
117+
RaftBind: "127.0.0.1:10008",
118+
RaftDataDir: "",
119+
DefaultRaftPort: 0,
120+
RaftNodes: []string{},
121+
BackendMySQLHost: "",
122+
BackendMySQLSchema: "",
123+
BackendMySQLPort: 3306,
124+
MemcacheServers: []string{},
125+
MemcachePath: "freno",
128126
//Debug: false,
129127
//ListenSocket: "",
130128
//AnExampleListOfStrings: []string{"*"},

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 float64
1112
}
1213

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

pkg/throttle/throttler.go

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -322,14 +322,12 @@ func (throttler *Throttler) refreshMySQLInventory() error {
322322

323323
if !clusterSettings.VitessSettings.IsEmpty() {
324324
if throttler.vitessClient == nil {
325-
throttler.vitessClient = vitess.New(
326-
time.Duration(config.Settings().VitessAPITimeoutSecs) * time.Second,
327-
)
325+
throttler.vitessClient = vitess.New(throttler.httpClient)
328326
}
329327
log.Debugf("getting vitess data from %s", clusterSettings.VitessSettings.API)
330328
keyspace := clusterSettings.VitessSettings.Keyspace
331329
shard := clusterSettings.VitessSettings.Shard
332-
tablets, err := throttler.vitessClient.ParseTablets(clusterSettings.VitessSettings.API, keyspace, shard)
330+
tablets, err := throttler.vitessClient.ParseTablets(clusterSettings.VitessSettings.API, keyspace, shard, clusterSettings.VitessSettings.TimeoutSecs)
333331
if err != nil {
334332
return log.Errorf("Unable to get vitess hosts from %s, %s/%s: %+v", clusterSettings.VitessSettings.API, keyspace, shard, err)
335333
}

pkg/vitess/api_client.go

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

11-
"github.com/github/freno/pkg/base"
1211
"vitess.io/vitess/go/vt/proto/topodata"
1312
)
1413

14+
const defaultTimeout = 5 * time.Second
15+
1516
// Tablet represents information about a running instance of vttablet.
1617
type Tablet struct {
1718
MysqlHostname string `json:"mysql_hostname,omitempty"`
@@ -24,7 +25,7 @@ func (t Tablet) IsValidReplica() bool {
2425
return t.Type == topodata.TabletType_REPLICA
2526
}
2627

27-
func constructAPIURL(api string, keyspace string, shard string) (url string) {
28+
func constructAPIURL(api, keyspace, shard string) (url string) {
2829
api = strings.TrimRight(api, "/")
2930
if !strings.HasSuffix(api, "/api") {
3031
api = fmt.Sprintf("%s/api", api)
@@ -50,15 +51,19 @@ type Client struct {
5051
}
5152

5253
// New returns a new Client
53-
func New(timeout time.Duration) *Client {
54-
return &Client{
55-
client: base.SetupHttpClient(timeout),
56-
}
54+
func New(client *http.Client) *Client {
55+
return &Client{client: client}
5756
}
5857

5958
// ParseTablets reads from vitess /api/ks_tablets/<keyspace>/[shard] and returns a
6059
// listing (mysql_hostname, mysql_port, type) of REPLICA tablets
61-
func (c *Client) ParseTablets(api, keyspace, shard string) (tablets []Tablet, err error) {
60+
func (c *Client) ParseTablets(api, keyspace, shard string, timeoutSecs float64) (tablets []Tablet, err error) {
61+
if timeoutSecs == 0 {
62+
c.client.Timeout = defaultTimeout
63+
} else {
64+
c.client.Timeout = time.Duration(timeoutSecs) * time.Second
65+
}
66+
6267
url := constructAPIURL(api, keyspace, shard)
6368
resp, err := c.client.Get(url)
6469
if err != nil {

0 commit comments

Comments
 (0)