-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathwriter.go
78 lines (65 loc) · 1.74 KB
/
writer.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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
package main
import (
"bytes"
"log"
"net/http"
"time"
)
type Writer struct {
SendBufferQueue chan *Message
TargetURL string
RecvBufferQueue chan *Message
}
func (w *Writer) Run() {
transport := http.Transport{}
client := http.Client{
Transport: &transport,
Timeout: 2 * time.Second,
}
// Only stack allocation is allowed inside this loop.
// When calling functions that allocate and return pointers, ensure
// that golang's escape analysis can tell that the memory can
// actually be allocated on the stack.
// (Use -gcflags '-m -l' to prove this.)
for {
msg := <-w.SendBufferQueue
// We'll keep trying to send this message until we succeed
// or get tired of trying
tries := 0
for {
tries += 1
// we tried, really hard, but let's be serious...
if tries == *attemptLimit {
log.Println("gave up writing to backend after", tries, "attempts")
break
}
contentReader := bytes.NewReader(msg.Content)
resp, err := client.Post(
w.TargetURL, "application/octet-stream", contentReader,
)
if err != nil {
log.Println("error writing to backend:", err)
time.Sleep(2 * time.Second)
continue
}
// InfluxDB docs say that only 204 exactly is truly successful,
// and in fact 200 OK is not successful. Strange, but okay...
if resp.StatusCode != 204 {
log.Println("backend write returned", resp.Status)
if resp.StatusCode == 400 {
// invalid request; no amount of retries will
// reform malformed data
break
}
time.Sleep(2 * time.Second)
continue
}
// If we fell out here then we've succeeded.
break
}
// Put the buffer back in the recieve queue so it can be re-used
// for a future message.
msg.Empty()
w.RecvBufferQueue <- msg
}
}