Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Feat] Include discover node timeout #25

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion elastictransport/discovery.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
package elastictransport

import (
"context"
"encoding/json"
"fmt"
"io/ioutil"
Expand Down Expand Up @@ -130,7 +131,17 @@ func (c *Client) getNodesInfo() ([]nodeInfo, error) {
scheme = c.urls[0].Scheme
)

req, err := http.NewRequest("GET", "/_nodes/http", nil)
var ctx context.Context
var cancel context.CancelFunc

if c.discoverNodeTimeout != nil {
ctx, cancel = context.WithTimeout(context.Background(), *c.discoverNodeTimeout)
defer cancel()
} else {
ctx = context.Background() // Use default context if no timeout is set
}

req, err := http.NewRequestWithContext(ctx, "GET", "/_nodes/http", nil)
if err != nil {
return out, err
}
Expand Down
6 changes: 6 additions & 0 deletions elastictransport/elastictransport.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ type Config struct {
Instrumentation Instrumentation

DiscoverNodesInterval time.Duration
DiscoverNodeTimeout *time.Duration

Transport http.RoundTripper
Logger Logger
Expand Down Expand Up @@ -130,6 +131,7 @@ type Client struct {
retryBackoff func(attempt int) time.Duration
discoverNodesInterval time.Duration
discoverNodesTimer *time.Timer
discoverNodeTimeout *time.Duration
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think a timeout with a negative value makes sense.
I would change this to not be a pointer, such that we can validate its value is greater than zero.


compressRequestBody bool
compressRequestBodyLevel int
Expand Down Expand Up @@ -241,6 +243,10 @@ func New(cfg Config) (*Client, error) {
instrumentation: cfg.Instrumentation,
}

if cfg.DiscoverNodeTimeout != nil {
client.discoverNodeTimeout = cfg.DiscoverNodeTimeout
}

if client.poolFunc != nil {
client.pool = client.poolFunc(conns, client.selector)
} else {
Expand Down