-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathcomposite_event_receiver.go
69 lines (58 loc) · 2.03 KB
/
composite_event_receiver.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
/*
Copyright © 2024 Acronis International GmbH.
Released under MIT license.
*/
package dbrutil
import (
"github.com/gocraft/dbr/v2"
)
// CompositeEventReceiver represents a composition of event receivers from dbr package
// and implements a Composite design pattern.
type CompositeEventReceiver struct {
Receivers []dbr.EventReceiver
}
// NewCompositeReceiver creates a new CompositeEventReceiver.
func NewCompositeReceiver(receivers []dbr.EventReceiver) *CompositeEventReceiver {
return &CompositeEventReceiver{receivers}
}
// Event receives a simple notification when various events occur.
func (r *CompositeEventReceiver) Event(eventName string) {
for _, recv := range r.Receivers {
recv.Event(eventName)
}
}
// EventKv receives a notification when various events occur along with
// optional key/value data.
func (r *CompositeEventReceiver) EventKv(eventName string, kvs map[string]string) {
for _, recv := range r.Receivers {
recv.EventKv(eventName, kvs)
}
}
// EventErr receives a notification of an error if one occurs.
func (r *CompositeEventReceiver) EventErr(eventName string, err error) error {
for _, recv := range r.Receivers {
_ = recv.EventErr(eventName, err)
}
return err
}
// EventErrKv receives a notification of an error if one occurs along with
// optional key/value data.
func (r *CompositeEventReceiver) EventErrKv(eventName string, err error, kvs map[string]string) error {
for _, recv := range r.Receivers {
_ = recv.EventErrKv(eventName, err, kvs)
}
return err
}
// Timing receives the time an event took to happen.
func (r *CompositeEventReceiver) Timing(eventName string, nanoseconds int64) {
for _, recv := range r.Receivers {
recv.Timing(eventName, nanoseconds)
}
}
// TimingKv is called when SQL query is executed. It receives the duration of how long the query takes,
// and calls TimingKv for each receiver in composition.
func (r *CompositeEventReceiver) TimingKv(eventName string, nanoseconds int64, kvs map[string]string) {
for _, recv := range r.Receivers {
recv.TimingKv(eventName, nanoseconds, kvs)
}
}