Skip to content

Commit d87f0bd

Browse files
authored
Thread request context through to product check (#321)
This is important for ensuring request cancellation, as well as for tracing product check requests.
1 parent 6462d8b commit d87f0bd

File tree

1 file changed

+9
-8
lines changed

1 file changed

+9
-8
lines changed

elasticsearch.go

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,6 @@ type info struct {
124124
Tagline string `json:"tagline"`
125125
}
126126

127-
128127
// NewDefaultClient creates a new client with default options.
129128
//
130129
// It will use http://localhost:9200 as the default address.
@@ -292,7 +291,7 @@ func (c *Client) Perform(req *http.Request) (*http.Response, error) {
292291
// header validation. ResponseCheck path continues after original request.
293292
if !c.useResponseCheckOnly {
294293
// Launch product check for 7.x, request info, check header then payload.
295-
if err := c.doProductCheck(c.productCheck); err != nil {
294+
if err := c.doProductCheck(req.Context(), c.productCheck); err != nil {
296295
return nil, err
297296
}
298297
}
@@ -302,8 +301,10 @@ func (c *Client) Perform(req *http.Request) (*http.Response, error) {
302301

303302
// ResponseCheck path continues, we run the header check on the first answer from ES.
304303
if err == nil {
305-
checkHeader := func() error { return genuineCheckHeader(res.Header) }
306-
if err := c.doProductCheck(checkHeader); err != nil {
304+
checkHeader := func(context.Context) error {
305+
return genuineCheckHeader(res.Header)
306+
}
307+
if err := c.doProductCheck(req.Context(), checkHeader); err != nil {
307308
res.Body.Close()
308309
return nil, err
309310
}
@@ -313,7 +314,7 @@ func (c *Client) Perform(req *http.Request) (*http.Response, error) {
313314

314315
// doProductCheck calls f if there as not been a prior successful call to doProductCheck,
315316
// returning nil otherwise.
316-
func (c *Client) doProductCheck(f func() error) error {
317+
func (c *Client) doProductCheck(ctx context.Context, f func(context.Context) error) error {
317318
c.productCheckMu.RLock()
318319
productCheckSuccess := c.productCheckSuccess
319320
c.productCheckMu.RUnlock()
@@ -329,7 +330,7 @@ func (c *Client) doProductCheck(f func() error) error {
329330
return nil
330331
}
331332

332-
if err := f(); err != nil {
333+
if err := f(ctx); err != nil {
333334
return err
334335
}
335336

@@ -340,9 +341,9 @@ func (c *Client) doProductCheck(f func() error) error {
340341

341342
// productCheck runs an esapi.Info query to retrieve informations of the current cluster
342343
// decodes the response and decides if the cluster is a genuine Elasticsearch product.
343-
func (c *Client) productCheck() error {
344+
func (c *Client) productCheck(ctx context.Context) error {
344345
req := esapi.InfoRequest{}
345-
res, err := req.Do(context.Background(), c.Transport)
346+
res, err := req.Do(ctx, c.Transport)
346347
if err != nil {
347348
return err
348349
}

0 commit comments

Comments
 (0)