forked from timescale/tsbs
-
Notifications
You must be signed in to change notification settings - Fork 2
/
processor.go
48 lines (43 loc) · 1.05 KB
/
processor.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
package victoriametrics
import (
"bytes"
"github.com/benchant/tsbs/pkg/targets"
"log"
"net/http"
"time"
)
type processor struct {
url string
vmURLs []string
}
func (p *processor) Init(workerNum int, doLoad, hashWorkers bool) {
p.url = p.vmURLs[workerNum%len(p.vmURLs)]
}
func (p *processor) ProcessBatch(b targets.Batch, doLoad bool) (metricCount, rowCount uint64) {
batch := b.(*batch)
if !doLoad {
return batch.metrics, batch.rows
}
mc, rc := p.do(batch)
return mc, rc
}
func (p *processor) do(b *batch) (uint64, uint64) {
for {
r := bytes.NewReader(b.buf.Bytes())
req, err := http.NewRequest("POST", p.url, r)
if err != nil {
log.Fatalf("error while creating new request: %s", err)
}
resp, err := http.DefaultClient.Do(req)
if err != nil {
log.Fatalf("error while executing request: %s", err)
}
resp.Body.Close()
if resp.StatusCode == http.StatusNoContent {
b.buf.Reset()
return b.metrics, b.rows
}
log.Printf("server returned HTTP status %d. Retrying", resp.StatusCode)
time.Sleep(time.Millisecond * 10)
}
}