Skip to content

Instantly share code, notes, and snippets.

@olivere
Last active August 29, 2015 14:18
Show Gist options
  • Save olivere/c76a110248b36d4aa135 to your computer and use it in GitHub Desktop.
Save olivere/c76a110248b36d4aa135 to your computer and use it in GitHub Desktop.
Connecting to Elasticsearch via Elastic (the infinite loop edition)
package main
import (
"fmt"
"time"
"runtime"
"github.com/olivere/elastic"
)
func main() {
var n int
for {
n++
// Create a client
u := elastic.SetURL("http://127.0.0.1:9200")
cl, err := elastic.NewClient(u)
if err != nil {
fmt.Printf("%v\n", err)
continue
} else {
fmt.Printf("%d\r", n)
}
time.Sleep(100 * time.Millisecond)
// Do something
_, err = cl.ElasticsearchVersion("http://127.0.0.1:9200")
if err != nil {
fmt.Printf("%v\n", err)
}
// Stop and GC the client
// (otherwise the server will continue running and sniffing)
cl.Stop()
cl = nil
runtime.GC()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment