-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathelastic.go
57 lines (50 loc) · 1.07 KB
/
elastic.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
53
54
55
56
57
package main
import (
"fmt"
"io/ioutil"
"net/http"
"time"
)
var client = http.Client{Timeout: time.Second * 100}
func BulkInsert(table *Table) {
if table.Buffer.Len() == 0 {
log.Debugf("no data in buffer %s, continue...", table.Name)
return
}
table.Lock.Lock()
resp, err := client.Post(
fmt.Sprintf("%s/%s/_bulk", config.ElasticUrl, table.Name),
"application/x-ndjson",
&table.Buffer,
)
if err == nil && resp.StatusCode == 200 {
table.Buffer.Reset()
log.Infof("push data of %s success", table.Name)
}
table.Lock.Unlock()
if err != nil {
log.Error(err)
return
}
if resp.StatusCode != 200 {
//goland:noinspection GoUnhandledErrorResult
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
log.Error("Read body failed:", err)
return
}
log.Errorf("elastic search response error %s", body)
}
}
func StartTimer() {
ticker := time.NewTicker(time.Second * 60)
defer ticker.Stop()
for range ticker.C {
for _, db := range config.Schemas {
for _, table := range db.Tables {
go BulkInsert(table)
}
}
}
}