-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrun.go
49 lines (45 loc) · 1.01 KB
/
run.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
package broker
import (
"time"
"github.com/sirupsen/logrus"
"github.com/streadway/amqp"
)
type doneErrCh struct {
event string
err error
}
func (eq *eventQueue) RunSubscribers() <-chan *amqp.Error {
ch := make(chan *amqp.Error)
go func() {
// Create errors channel list
errorsCh := []chan doneErrCh{}
for i := 0; i < len(eq.events); i++ {
errorsCh = append(errorsCh, make(chan doneErrCh))
}
// Run subscribers
go func() {
count := 0
for event, handle := range eq.events {
go eq.subscribe(event, handle, errorsCh[count])
count++
}
}()
// Listen errors
for _, ch := range errorsCh {
ch := ch
go func() {
for {
select {
case e := <-ch:
logrus.Errorf("subscriber to event %v died: %v", e.event, e.err)
logrus.Infof("waiting 10 seconds to re-run subscriber to %v event", e.event)
time.Sleep(10 * time.Second)
// Re-run subscriber
go eq.subscribe(e.event, eq.events[e.event], ch)
}
}
}()
}
}()
return eq.conn.NotifyClose(ch)
}