-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathes.go
52 lines (45 loc) · 961 Bytes
/
es.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
package eskeeper
import (
"fmt"
"time"
"github.com/cenkalti/backoff/v4"
"github.com/elastic/go-elasticsearch/v7"
)
type esclient struct {
client *elasticsearch.Client
verbose bool
}
func newEsClient(urls []string, user, pass string) (*esclient, error) {
retryBackoff := backoff.NewExponentialBackOff()
retryBackoff.InitialInterval = time.Second
conf := elasticsearch.Config{
Addresses: urls,
Username: user,
Password: pass,
RetryOnStatus: []int{408, 429, 502, 503, 504},
RetryBackoff: func(i int) time.Duration {
if i == 1 {
retryBackoff.Reset()
}
d := retryBackoff.NextBackOff()
return d
},
}
es, err := elasticsearch.NewClient(conf)
if err != nil {
return nil, err
}
return &esclient{
client: es,
}, nil
}
func (e *esclient) log(msg string) {
if e.verbose {
fmt.Println(msg)
}
}
func (e *esclient) logf(format string, a ...interface{}) {
if e.verbose {
fmt.Printf(format, a...)
}
}