-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuffered_slack_logger.go
143 lines (124 loc) · 2.74 KB
/
buffered_slack_logger.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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
package goslacklog
import (
"strings"
"sync"
"time"
)
type action int
const (
exitAction action = iota
sendMessageAction
)
type bufferedSlackWriter struct {
messageSender MessageSender
queue chan string
size int
exitCh chan bool
messagesWg *sync.WaitGroup
ticker *time.Ticker
closed bool
closeOnce *sync.Once
actionCh chan action
}
func NewBufferedSlackLogger(hookURL string, bufferSize int, sendInBatches int, sendInterval time.Duration) *bufferedSlackWriter {
return NewBufferedSlackLoggerWithMessageSender(
&singleMessageSlackClient{
slackPostMessageUrl: hookURL,
},
bufferSize,
sendInBatches,
sendInterval,
)
}
func NewBufferedSlackLoggerWithMessageSender(messageSender MessageSender, bufferSize int, sendInBatches int, sendInterval time.Duration) *bufferedSlackWriter {
buffered := &bufferedSlackWriter{
messageSender: messageSender,
queue: make(chan string, bufferSize),
size: sendInBatches,
closed: false,
exitCh: make(chan bool),
messagesWg: &sync.WaitGroup{},
closeOnce: &sync.Once{},
actionCh: make(chan action),
ticker: time.NewTicker(sendInterval),
}
go buffered.sendTickAction()
go buffered.sender()
return buffered
}
func (s *bufferedSlackWriter) Write(p []byte) (n int, err error) {
if s.closed {
return len(p), nil
}
s.messagesWg.Add(1)
defer s.messagesWg.Done()
msg := string(p)
select {
case s.queue <- msg:
case <-s.exitCh:
}
// it shouldn't matter if it didn't write anything so no error will be sent
return len(p), nil
}
func (s *bufferedSlackWriter) Close() {
s.closeOnce.Do(func() {
// stop accepting new writes
s.closed = true
// kill ticker so no new events get sent
s.ticker.Stop()
// release all the ones that are currently waiting
close(s.exitCh)
// wait for them to exit
s.messagesWg.Wait()
// tell main loop to exit
s.actionCh <- exitAction
close(s.queue)
})
}
func (s *bufferedSlackWriter) sendTickAction() {
for {
_, ok := <-s.ticker.C
if !ok {
return
}
s.actionCh <- sendMessageAction
}
}
func (s *bufferedSlackWriter) sender() {
sb := strings.Builder{}
for act := range s.actionCh {
switch act {
case exitAction:
return
case sendMessageAction:
sb.Reset()
for msg := range readN(s.queue, s.size) {
sb.WriteString("```")
sb.WriteString(msg)
sb.WriteString("```\n")
}
str := sb.String()
if str != "" {
s.messageSender.SendMessage(sb.String())
}
}
}
}
func readN(chIn <-chan string, n int) <-chan string {
chOut := make(chan string)
go func() {
for i := 0; i < n; i++ {
select {
case str, ok := <-chIn:
if !ok {
break
}
chOut <- str
default:
break
}
}
close(chOut)
}()
return chOut
}