Skip to content

Commit dcd99f5

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: elastic#95, elastic#100
1 parent 8e2b609 commit dcd99f5

13 files changed

+550
-401
lines changed

_examples/instrumentation/expvar.go

+4-5
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

+10-4
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)