forked from raystack/optimus
-
Notifications
You must be signed in to change notification settings - Fork 1
/
handler.go
47 lines (37 loc) · 983 Bytes
/
handler.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
package moderator
import (
"github.com/goto/salt/log"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promauto"
)
var eventQueueCounter = promauto.NewCounter(prometheus.CounterOpts{
Name: "publisher_events_created_total",
Help: "Events created and to be sent to writer",
})
type Event interface {
Bytes() ([]byte, error)
}
type Handler interface {
HandleEvent(e Event)
}
type NoOpHandler struct{}
func (NoOpHandler) HandleEvent(_ Event) {}
type EventHandler struct {
messageChan chan<- []byte
logger log.Logger
}
func NewEventHandler(messageChan chan<- []byte, logger log.Logger) *EventHandler {
return &EventHandler{
messageChan: messageChan,
logger: logger,
}
}
func (e EventHandler) HandleEvent(event Event) {
bytes, err := event.Bytes()
if err != nil {
e.logger.Error("error converting event to bytes: %v", err)
return
}
go func() { e.messageChan <- bytes }()
eventQueueCounter.Inc()
}