Skip to content
Oliver Eilhard edited this page Oct 15, 2015 · 2 revisions

In the normal use case, you create one client in your application on startup. You then share this client throughout your application, elastic will automatically mark nodes as healthy or dead etc.

However, in restricted environments like App Engine, one cannot create such a shared client. You must create a new client for every request. So we cannot afford to keep Goroutines around and must make the creation of a client as efficient as possible.

So if you now create a new client with the sniffer and healthchecker disabled (SetSniff(false) and SetHealthcheck(false)), the NewClient function will skip most of the sanity checks it does on normal startup. It will also not start any Goroutines.

So to use elastic with restricted environments like App Engine, be sure to disable sniffer and healthchecker via the options SetSniff(false) and SetHealthcheck(false).

Here's an example from an elastic user on App Engine that happens to also use HTTP Basic Authentication:

type AppEngineTransport struct {
    Username string
    Password string
    Transport http.RoundTripper
}

func (t *AppEngineTransport) RoundTrip(req *http.Request) (*http.Response, error) {
    rt := t.Transport
    if rt == nil {
        panic("transport must be supplied")
    }
    req.SetBasicAuth(t.Username, t.Password)
    return rt.RoundTrip(req)
}

transport := &AppEngineTransport{
    Username:  "username",
    Password:  "password",
    Transport: &urlfetch.Transport{Context: ctx, Deadline: time.Second * 30},
}
httpClient := &http.Client{Transport: transport}

client, err := elastic.NewClient(
    elastic.SetHttpClient(httpClient),
    elastic.SetURL(GetElasticIp(ctx)),
    elastic.SetSniff(false),
    elastic.SetHealthcheck(false),
    elastic.SetMaxRetries(1),
    elastic.SetHealthcheckTimeoutStartup(0),
)
Clone this wiki locally