We are currently creating an olivere elastic client in our API like so:
elasticClient, err = elastic.NewClient(
elastic.SetURL(elasticSearchUrl),
elastic.SetSniff(false),
)
if err != nil {
logger.ApplicationError(err)
time.Sleep(3 * time.Second)
} else {
logger.Information("ElasticSearch: connected")
return elasticClient
}
...
The single instance client is then used for all in-coming API requests.
However, after a new Elastic cluster is redeployed the connection seems to be still pointing to the old instance since that elasticSearchUrl
was only used once for the initial connection.
This creates a problem when are new Elastic Cluster is deployed because a new connection is required.
What would be the best way to achieve a re-connect to the new Elastic Search cluster using this library?
The reason for this is golang is using long-live connections by default. You can disable it by setting the DisableKeepAlives
in Transport.
var httpClient = &http.Client{
Timeout: 60 * time.Second,
Transport: &http.Transport{
// Disabel long-live connection
DisableKeepAlives: true,
// This is to avoid the time_wait in connections
MaxIdleConnsPerHost: -1,
},
}
elasticClient, err = elastic.NewClient(
elastic.SetURL(elasticSearchUrl),
elastic.SetSniff(false),
elastic.SetHttpClient(httpClient),
)
...