Skip to content

Commit 98d0e04

Browse files
committed
Transport: Refactored the connection pool
This patch refactors the connection pool implementation to be cleaner and more aligned with the architecture of other official clients. It introduces the "Selector" component, which allows to customize the logic for selecting connections from the pool without replacing the entire connection pool implementation. It introduces the "ConnectionPoolFunc" configuration option, as a constructor function to be used when initializing a custom connection pool implementation. Many connection pool methods have been renamed to tighten up the semantics (eg. `Remove()` => `OnFailure()`, etc). The support for client metrics has been refactored as well, to better support custom connection pool implementations. Related: #95, #100 (cherry picked from commit dcd99f5)
1 parent 39e4c99 commit 98d0e04

13 files changed

+550
-401
lines changed

_examples/instrumentation/expvar.go

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ import (
1717
"time"
1818

1919
// Import the "expvar" and "pprof" package >>>>>>>>>>
20-
2120
"net/http"
2221
_ "net/http/pprof"
2322
// <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
@@ -121,10 +120,10 @@ func main() {
121120
log.Println("███", fmt.Sprintf("\x1b[1m%s\x1b[0m", "Metrics"), strings.Repeat("█", tWidth-12))
122121
log.Printf(
123122
""+
124-
" \x1b[2mRequests: \x1b[0m %d\n"+
125-
" \x1b[2mFailures: \x1b[0m %d\n"+
126-
" \x1b[2mLive nodes:\x1b[0m %s",
127-
m.Requests, m.Failures, m.Live)
123+
" \x1b[2mRequests: \x1b[0m %d\n"+
124+
" \x1b[2mFailures: \x1b[0m %d\n"+
125+
" \x1b[2mConnections:\x1b[0m %s",
126+
m.Requests, m.Failures, m.Connections)
128127
log.Println(strings.Repeat("─", tWidth))
129128
}
130129
}

elasticsearch.go

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,12 @@ type Config struct {
4747

4848
RetryBackoff func(attempt int) time.Duration // Optional backoff duration. Default: nil.
4949

50-
Transport http.RoundTripper // The HTTP transport object.
51-
Logger estransport.Logger // The logger object.
50+
Transport http.RoundTripper // The HTTP transport object.
51+
Logger estransport.Logger // The logger object.
52+
Selector estransport.Selector // The selector object.
53+
54+
// Optional constructor function for a custom ConnectionPool. Default: nil.
55+
ConnectionPoolFunc func([]*estransport.Connection, estransport.Selector) estransport.ConnectionPool
5256
}
5357

5458
// Client represents the Elasticsearch client.
@@ -135,8 +139,10 @@ func NewClient(cfg Config) (*Client, error) {
135139
EnableMetrics: cfg.EnableMetrics,
136140
EnableDebugLogger: cfg.EnableDebugLogger,
137141

138-
Transport: cfg.Transport,
139-
Logger: cfg.Logger,
142+
Transport: cfg.Transport,
143+
Logger: cfg.Logger,
144+
Selector: cfg.Selector,
145+
ConnectionPoolFunc: cfg.ConnectionPoolFunc,
140146
})
141147

142148
return &Client{Transport: tp, API: esapi.New(tp)}, nil

0 commit comments

Comments
 (0)