Skip to content

Commit

Permalink
[provider] Add a timeout for pinging ES in case of no network access.
Browse files Browse the repository at this point in the history
  • Loading branch information
phillbaker committed Sep 17, 2021
1 parent 2cbf4b5 commit 22a9d6e
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
- [index] Add include_type_name for compatibility between ESv6/7

### Fixed
- [provider] Add a timeout for pinging ES in case of no network access.


## [2.0.0-beta.1] - 2021-08-30
Expand Down
11 changes: 10 additions & 1 deletion es/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,13 @@ import (
elastic6 "gopkg.in/olivere/elastic.v6"
)

const (
// DefaultVersionPingTimeout is the time the ping to check the cluster
// version waits for a response from Elasticsearch on startup, i.e. when
// creating a provider.
DefaultVersionPingTimeout = 5 * time.Second
)

var awsUrlRegexp = regexp.MustCompile(`([a-z0-9-]+).es.amazonaws.com$`)

type ProviderConf struct {
Expand Down Expand Up @@ -299,7 +306,9 @@ func getClient(conf *ProviderConf) (interface{}, error) {
// Use the v7 client to ping the cluster to determine the version if one was not provided
if conf.esVersion == "" {
log.Printf("[INFO] Pinging url to determine version %+v", conf.rawUrl)
info, httpStatus, err := client.Ping(conf.rawUrl).Do(context.TODO())
ctx, cancel := context.WithTimeout(context.Background(), DefaultVersionPingTimeout)
defer cancel()
info, httpStatus, err := client.Ping(conf.rawUrl).Do(ctx)
if httpStatus == http.StatusForbidden {
return nil, errors.New("HTTP 403 Forbidden: Permission denied. Please ensure that the correct credentials are being used to access the cluster.")
}
Expand Down

0 comments on commit 22a9d6e

Please sign in to comment.