generated from benjivesterby/go-template-repo
/
events.go
62 lines (51 loc) · 1.13 KB
/
events.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
package amqp
type eventFunc func() interface{}
type errFunc func() error
// event is a helper function that indicates
// if the events channel is nil
func (r *rabbitmq) event(fn eventFunc) {
if r.events != nil {
select {
case <-r.ctx.Done():
return
case r.events <- fn():
}
}
}
// e is a helper function that indicates
// if the events channel is nil
func (r *rabbitmq) err(fn errFunc) {
if r.errors != nil {
select {
case <-r.ctx.Done():
return
case r.errors <- fn():
}
}
}
// Events creates a channel to receive events from the atomizer and
// return the channel for handling
func (r *rabbitmq) Events(buffer int) <-chan interface{} {
if buffer < 0 {
buffer = 0
}
r.eventsMu.Lock()
defer r.eventsMu.Unlock()
if r.events == nil {
r.events = make(chan interface{}, buffer)
}
return r.events
}
// Errors creates a channel to receive errors from the atomizer and
// return the channel for handling
func (r *rabbitmq) Errors(buffer int) <-chan error {
if buffer < 0 {
buffer = 0
}
r.errorsMu.Lock()
defer r.errorsMu.Unlock()
if r.errors == nil {
r.errors = make(chan error, buffer)
}
return r.errors
}