-
Notifications
You must be signed in to change notification settings - Fork 69
/
bus.go
165 lines (142 loc) · 3.55 KB
/
bus.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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
package bus
import (
"fmt"
"sync"
"github.com/jhunt/go-log"
)
const (
ErrorEvent = "error"
UnlockCoreEvent = "unlock-core"
CreateObjectEvent = "create-object"
UpdateObjectEvent = "update-object"
DeleteObjectEvent = "delete-object"
TaskStatusUpdateEvent = "task-status-update"
TaskLogUpdateEvent = "task-log-update"
TenantInviteEvent = "tenant-invite"
TenantBanishEvent = "tenant-banish"
HealthUpdateEvent = "health-update"
)
type Event struct {
Event string `json:"event"`
Queue string `json:"queue"`
Type string `json:"type,omitempty"`
Data interface{} `json:"data"`
}
type Bus struct {
lock sync.Mutex
slots []slot
//slotMap maps unique identifiers given to clients to slot slice indices
slotMap map[int64]int
backlog int
lifetime, current, dropped struct {
connections int64
}
events map[string]int64
messages map[string]int64
}
type slot struct {
ch chan Event
id int64
mostQueued int
acl map[string]bool
}
func New(n, backlog int) *Bus {
b := Bus{
slots: make([]slot, n),
slotMap: make(map[int64]int, n),
backlog: backlog,
events: make(map[string]int64),
messages: make(map[string]int64),
}
return &b
}
func (b *Bus) Register(queues []string) (chan Event, int64, error) {
b.lock.Lock()
defer b.lock.Unlock()
for i := range b.slots {
if b.slots[i].ch == nil {
b.current.connections += 1
b.lifetime.connections += 1
b.slots[i].id = b.lifetime.connections
b.slotMap[b.lifetime.connections] = i
b.slots[i].ch = make(chan Event, b.backlog)
b.slots[i].acl = make(map[string]bool)
for _, q := range queues {
b.slots[i].acl[q] = true
}
return b.slots[i].ch, b.slots[i].id, nil
}
}
return nil, -1, fmt.Errorf("too many message bus clients")
}
//Unregister causes the bus to stop routing events to the handler with the given ID.
// The channel returned from the matching call to register is closed. Multiple calls
// to Unregister with the same id are idempotent.
func (b *Bus) Unregister(id int64) {
b.lock.Lock()
defer b.lock.Unlock()
b.unregister(id)
}
func (b *Bus) unregister(id int64) {
idx, found := b.slotMap[id]
if !found {
return //already closed. moving on.
}
delete(b.slotMap, id)
if idx < 0 || idx >= len(b.slots) {
log.Errorf("could not unregister channel #%d: index out of range", idx)
panic(fmt.Sprintf("could not unregister channel #%d: index out of range", idx))
}
b.current.connections -= 1
close(b.slots[idx].ch)
b.slots[idx].ch = nil
b.slots[idx].acl = nil
b.slots[idx].mostQueued = 0
b.slots[idx].id = -1
}
func (b *Bus) SendError(err error, queues ...string) {
b.SendEvent(queues, Event{
Event: ErrorEvent,
Data: map[string]interface{}{"error": err},
})
}
func (b *Bus) Send(event, typ string, thing interface{}, queues ...string) {
b.SendEvent(queues, Event{
Event: event,
Type: typ,
Data: marshal(thing),
})
}
func (b *Bus) SendEvent(queues []string, ev Event) {
b.lock.Lock()
defer b.lock.Unlock()
if _, ok := b.events[ev.Event]; !ok {
b.events[ev.Event] = 0
}
if _, ok := b.messages[ev.Event]; !ok {
b.messages[ev.Event] = 0
}
b.events[ev.Event] += 1
for i, s := range b.slots {
if s.ch == nil {
continue
}
for _, q := range queues {
if q == "*" || s.acl[q] || s.acl["*"] {
ev.Queue = q
select {
case s.ch <- ev:
queued := len(s.ch)
if queued > s.mostQueued {
b.slots[i].mostQueued = queued
}
b.messages[ev.Event] += 1
default:
b.unregister(s.id)
b.dropped.connections++
}
break
}
}
}
}