Skip to content

Commit 22a9d6e

Browse files
committed
[provider] Add a timeout for pinging ES in case of no network access.
1 parent 2cbf4b5 commit 22a9d6e

File tree

2 files changed

+11
-1
lines changed

2 files changed

+11
-1
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
- [index] Add include_type_name for compatibility between ESv6/7
88

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

1112

1213
## [2.0.0-beta.1] - 2021-08-30

es/provider.go

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,13 @@ import (
2626
elastic6 "gopkg.in/olivere/elastic.v6"
2727
)
2828

29+
const (
30+
// DefaultVersionPingTimeout is the time the ping to check the cluster
31+
// version waits for a response from Elasticsearch on startup, i.e. when
32+
// creating a provider.
33+
DefaultVersionPingTimeout = 5 * time.Second
34+
)
35+
2936
var awsUrlRegexp = regexp.MustCompile(`([a-z0-9-]+).es.amazonaws.com$`)
3037

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

0 commit comments

Comments
 (0)