-
Notifications
You must be signed in to change notification settings - Fork 165
/
writer.go
39 lines (33 loc) · 741 Bytes
/
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
package main
import (
"encoding/json"
"io"
"time"
)
type AuditWriter struct {
e *json.Encoder
w io.Writer
attempts int
}
func NewAuditWriter(w io.Writer, attempts int) *AuditWriter {
return &AuditWriter{
e: json.NewEncoder(w),
w: w,
attempts: attempts,
}
}
func (a *AuditWriter) Write(msg *AuditMessageGroup) (err error) {
for i := 0; i < a.attempts; i++ {
err = a.e.Encode(msg)
if err == nil {
break
}
if i != a.attempts {
// We have to reset the encoder because write errors are kept internally and can not be retried
a.e = json.NewEncoder(a.w)
el.Println("Failed to write message, retrying in 1 second. Error:", err)
time.Sleep(time.Second * 1)
}
}
return err
}