I'm using Golang & elastic client .
Bellow is my client creation logic:
if client, err := elastic.NewClient(elastic.SetURL(ElasticsearchURL)); err != nil {
// Handle error
logger.Error.Println(err)
return nil
} else {
return client
}
Whats the correct approach to:
I am kind of confused between counterintuitive answers in below links:
That depends on the application.
In 99% of the use cases you have a normal, long-running application. Then you should create just one client with elastic.NewClient
. You can pass it around in your code and it should always work, even in different goroutines. This will create a long-running client which has several benefits. Eg it will run health checks in the background that will prevent Elastic from sending requests to unhealthy or dead nodes.
However, if you have a short-running application (something like AWS Lambda oder Cloud Functions) you might need a "connection" on a request level. In that specific case you can use elastic.NewSimpleClient
. It has a bit more overhead though as you're creating a new client every time. And it won't do any health checks and other things.
DO NOT create a new client with elastic.NewClient
for every request, as any call to NewClient
will create a set of goroutines and you'll quickly run out of resources if you do that.
Please read the documentation and the wiki for further details.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.