forked from slackhq/go-audit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwriter.go
67 lines (54 loc) · 1.47 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
// +build linux
package audit
import (
"encoding/json"
"io"
"time"
)
// AuditWriter is the interface that implements different audit writers
type AuditWriter interface {
Write(msg *AuditMessageGroup) (err error)
}
// AuditWriterIO is an implementation of AuditWriter based on IO buffers and file system
type AuditWriterIO struct {
e *json.Encoder
w io.Writer
attempts int
}
func NewAuditWriterIO(w io.Writer, attempts int) *AuditWriterIO {
return &AuditWriterIO{
e: json.NewEncoder(w),
w: w,
attempts: attempts,
}
}
func (a *AuditWriterIO) 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
}
// AuditWriterChannel is an channel based implementation of the interface
type AuditWriterChannel struct {
c chan *AuditMessageGroup
}
// NewAuditWriterChannel creates a new audit writer with a provided channel
func NewAuditWriterChannel(c chan *AuditMessageGroup) *AuditWriterChannel {
return &AuditWriterChannel{
c: c,
}
}
// Write is the implementation of the Write function of the interface
func (a *AuditWriterChannel) Write(msg *AuditMessageGroup) (err error) {
a.c <- msg
return nil
}