Skip to content

Commit b0ea5c2

Browse files
axwkarmi
authored andcommitted
estransport: remove enableMetrics
These fields are redundant, as you can check that metrics is non-nil. (cherry picked from commit 5ac2b99)
1 parent 5420bed commit b0ea5c2

File tree

3 files changed

+10
-21
lines changed

3 files changed

+10
-21
lines changed

estransport/connection.go

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,7 @@ type Connection struct {
4040
type singleConnectionPool struct {
4141
connection *Connection
4242

43-
metrics *metrics
44-
enableMetrics bool
43+
metrics *metrics
4544

4645
debugLogger DebuggingLogger
4746
}
@@ -54,8 +53,7 @@ type roundRobinConnectionPool struct {
5453
dead []*Connection // List of dead connections
5554
orig []*url.URL // List of original URLs, passed in during initialization
5655

57-
metrics *metrics
58-
enableMetrics bool
56+
metrics *metrics
5957

6058
debugLogger DebuggingLogger
6159
}
@@ -74,12 +72,7 @@ func newRoundRobinConnectionPool(u ...*url.URL) *roundRobinConnectionPool {
7472

7573
cp := roundRobinConnectionPool{live: conns, orig: u, curr: -1}
7674

77-
if cp.enableMetrics {
78-
cp.metrics.Lock()
79-
cp.metrics.live = cp.live
80-
cp.metrics.dead = cp.dead
81-
cp.metrics.Unlock()
82-
}
75+
// BUG the transport's metrics should be initialised with the live list
8376

8477
return &cp
8578
}
@@ -171,7 +164,7 @@ func (cp *roundRobinConnectionPool) Remove(c *Connection) error {
171164
copy(cp.live[index:], cp.live[index+1:])
172165
cp.live = cp.live[:len(cp.live)-1]
173166

174-
if cp.enableMetrics {
167+
if cp.metrics != nil {
175168
cp.metrics.Lock()
176169
cp.metrics.dead = cp.dead
177170
cp.metrics.live = cp.live
@@ -223,7 +216,7 @@ func (cp *roundRobinConnectionPool) resurrectLocked(c *Connection, removeDead bo
223216
}
224217
}
225218

226-
if cp.enableMetrics {
219+
if cp.metrics != nil {
227220
cp.metrics.Lock()
228221
cp.metrics.dead = cp.dead
229222
cp.metrics.live = cp.live

estransport/estransport.go

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -78,8 +78,7 @@ type Client struct {
7878
maxRetries int
7979
retryBackoff func(attempt int) time.Duration
8080

81-
enableMetrics bool
82-
metrics *metrics
81+
metrics *metrics
8382

8483
enableDebugLogger bool
8584
debugLogger DebuggingLogger
@@ -125,7 +124,6 @@ func New(cfg Config) *Client {
125124
maxRetries: cfg.MaxRetries,
126125
retryBackoff: cfg.RetryBackoff,
127126

128-
enableMetrics: cfg.EnableMetrics,
129127
enableDebugLogger: cfg.EnableDebugLogger,
130128

131129
transport: cfg.Transport,
@@ -137,12 +135,10 @@ func New(cfg Config) *Client {
137135
// FIXME(karmi): Type assertion to interface
138136
if pool, ok := client.pool.(*singleConnectionPool); ok {
139137
client.metrics = &metrics{responses: make(map[int]int)}
140-
pool.enableMetrics = true
141138
pool.metrics = client.metrics
142139
}
143140
if pool, ok := client.pool.(*roundRobinConnectionPool); ok {
144141
client.metrics = &metrics{responses: make(map[int]int)}
145-
pool.enableMetrics = true
146142
pool.metrics = client.metrics
147143
}
148144
}
@@ -171,7 +167,7 @@ func (c *Client) Perform(req *http.Request) (*http.Response, error) {
171167

172168
// Record metrics, when enabled
173169
//
174-
if c.enableMetrics {
170+
if c.metrics != nil {
175171
c.metrics.Lock()
176172
c.metrics.requests++
177173
c.metrics.Unlock()
@@ -243,7 +239,7 @@ func (c *Client) Perform(req *http.Request) (*http.Response, error) {
243239
if err != nil {
244240
// Record metrics, when enabled
245241
//
246-
if c.enableMetrics {
242+
if c.metrics != nil {
247243
c.metrics.Lock()
248244
c.metrics.failures++
249245
c.metrics.Unlock()
@@ -268,7 +264,7 @@ func (c *Client) Perform(req *http.Request) (*http.Response, error) {
268264
conn.Unlock()
269265
}
270266

271-
if res != nil && c.enableMetrics {
267+
if res != nil && c.metrics != nil {
272268
c.metrics.Lock()
273269
c.metrics.responses[res.StatusCode]++
274270
c.metrics.Unlock()

estransport/metrics.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ type metrics struct {
6767
// Metrics returns the transport metrics.
6868
//
6969
func (c *Client) Metrics() (Metrics, error) {
70-
if !c.enableMetrics {
70+
if c.metrics == nil {
7171
return Metrics{}, errors.New("transport metrics not enabled")
7272
}
7373
c.metrics.RLock()

0 commit comments

Comments
 (0)