Use the singleton pattern to have a one per application elasticsearch connection
@@ -5,6 +5,7 @@ import (
"fmt"
"net/http"
"strings"
+ "sync"
"time"
"github.com/chef/automate/lib/grpc/auth_context"
@@ -29,6 +30,9 @@ type ReportingTransport struct {
ChefDeliveryToken string
}
+var once sync.Once
+var esClient *elastic.Client
+
var err error
const (
@@ -58,13 +62,15 @@ func (backend *ES2Backend) getHttpClient() *http.Client {
}
func (backend *ES2Backend) ES2Client() (*elastic.Client, error) {
- //logrus.Debugf("Creating ES client...")
- return elastic.NewClient(
+ //this is now a singleton as per best practice as outlined in the comment section of the elastic.NewClient
+ once.Do(func() {
+ esClient, err = elastic.NewClient(
elastic.SetHttpClient(backend.getHttpClient()),
elastic.SetURL(backend.ESUrl),
elastic.SetSniff(false),
- //elastic.SetTraceLog(logrus.New()), // logging from the elastic library. We could enable this when the log level is debug
)
+ })
+ return esClient, err
}
// Removes element with index i from array arr
